18.10.2016 Views

Drupal 7 Module Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Drupal</strong>’s Theme Layer<br />

This book is mostly focused on building modules that encapsulate discrete chunks<br />

of functionality. However, since we're ultimately building a web application,<br />

everything outputted by your functionality will need to be marked up with HTML.<br />

<strong>Drupal</strong> calls the process of wrapping your data in HTML and CSS as theming.<br />

For the next two chapters, we will discuss how your module should integrate<br />

with the theme layer. Chapter 3 will talk about the architecture of the system,<br />

theme functions, templates, render elements, and the theme registry. Chapter 4<br />

will use these newly acquired concepts to integrate an example module with<br />

the theming layer.<br />

Business logic versus presentation logic<br />

So what would be the best way to get our data and functionality marked up? Do we<br />

simply wrap each piece of data in HTML and return the whole as a giant string? Like<br />

the following example:<br />

return '' . $data . '';<br />

Fortunately, we don't. Like all other well-designed applications, <strong>Drupal</strong> separates its<br />

business logic from its presentation logic. Traditionally, the primary motivations for<br />

this separation of concerns are as follows:<br />

1. To make the code easier to maintain.<br />

2. To make it possible to easily swap out one layer's implementation without<br />

having to re-write the other layers.<br />

As we shall see, <strong>Drupal</strong> takes the "swap-ability" aspect to the extreme.<br />

As we mentioned in the introduction of this chapter, the default theme selected on<br />

the Appearance page is the most obvious part of the theme layer. Also, you might<br />

think that the theme is responsible for applying the HTML and CSS for the website.<br />

However, there are thousands of contributed modules on drupal.org. Should the<br />

theme be responsible for marking up all of those modules' data? Obviously not.<br />

Since a module is most intimately familiar with its own data and functionality,<br />

it's the module's responsibility to provide the default theme implementation.<br />

As long as the module uses the theme system properly, a theme will be able to<br />

override any HTML and CSS by hot-swapping its own implementation for the<br />

module's implementation.<br />

[ 62 ]

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

Saved successfully!

Ooh no, something went wrong!