18. Introduction to Module Development


  • 18
  • Chapters 18, 19, and 20 form one unit, originally written as one chapter, covering everything you need to know to get started writing your own modules.

    Drupal is a powerful and modular system— we've heard that before. Indeed, much of Drupal's power is in its modules, dynamos of pluggable functionality that can build on Drupal's base system and on one another to do wonderful things.

    How do we tap into this power to add our own original features? We can write a module. You can write a module. All you need to do is make two files. The first file is not even code; it only tells Drupal about the module. The second file can have as little as three lines of code in it. The next page of this chapter has the entire code for a working module.We have two goals. First, to show that making a module is something that anyone can do. Second, to cover the many, but mostly simple, rules and tools to follow and use. These two purposes go together: Understanding the technical foundation is important to confidence.

    This chapter provides a solid grounding in module building in four sections:

    1. Basics of a module and how Drupal uses hooks in nearly everything it does to allow modules to extend and modify Drupal. (We will also explain what hooks are.)
    2. Overview of technical skills required to develop a module, including:
      • PHP basics.
      • Drupal coding standards.
    3. Instruction and examples for using the hooks and functions provided by Drupal. These are covered in the course of building a complete module, and include:
      • Altering forms.
      • Making modules themeable and styling your module.
      • Localization (providing a translatable user interface).
      • Creating a page with hook_menu().
      • Using permissions.
      • Retrieving and storing data using the database abstraction layer.
      • Creating a configuration page and settings form.
    4. Refinement of a Drupal.org-worthy module, including fixing errors and reviewing for coding standards and security.

    Throughout, there is an emphasis on on showing ways to discover new information as you need it— learning how to learn, so to speak. You will know things not merely because you read them in the book, but because you know how they got there (and you will be ready to write the Drupal 8 definitive guide!)

  • Author(s)

  • Benjamin Melançon