18.10.2016 Views

Drupal 7 Module Development

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Creating Your First <strong>Module</strong><br />

We are going to divide this task of building a new module into the three parts:<br />

• Create a new module folder and module files<br />

• Work with the Block Subsystem<br />

• Write automated tests using the SimpleTest framework included in <strong>Drupal</strong><br />

We are going to proceed in that order for the sake of simplicity. One might object<br />

that, following agile development processes, we ought to begin by writing our<br />

tests. This approach is called Test-driven <strong>Development</strong> (TDD), and is a justly<br />

popular methodology.<br />

Agile software development is a particular methodology designed to<br />

help teams of developers effectively and efficiently build software. While<br />

<strong>Drupal</strong> itself has not been developed using an agile process, it does<br />

facilitate many of the agile practices. To learn more about agile, visit<br />

http://agilemanifesto.org/.<br />

However, our goal here is not to exemplify a particular methodology, but to discover<br />

how to write modules. It is easier to learn module development by first writing the<br />

module, and then learn how to write unit tests. It is easier for two reasons:<br />

• SimpleTest (in spite of its name) is the least simple part of this chapter. It will<br />

have double the code-weight of our actual module.<br />

• We will need to become acquainted with the APIs we are going to use in<br />

development before we attempt to write tests that assume knowledge of<br />

those APIs.<br />

In regular module development, though, you may certainly choose to follow the<br />

TDD approach of writing tests first, and then writing the module.<br />

Let's now move on to the first step of creating a new module.<br />

Creating a new module<br />

Creating <strong>Drupal</strong> modules is easy. How easy? Easy enough that over 5,000 modules<br />

have been developed, and many <strong>Drupal</strong> developers are even PHP novices! In fact,<br />

the code in this chapter is an illustration of how easy module coding can be. We are<br />

going to create our first module with only one directory and two small files.<br />

[ 26 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!