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.

Chapter 3<br />

After the data has been retrieved and manipulated in the heart of your module (the<br />

business logic), it will need to provide the default theme implementation. Sometimes<br />

a particular theme will need to override your implementation in order for it to<br />

achieve a specific design goal; if the theme provides its own implementation, <strong>Drupal</strong><br />

will use the theme implementation instead of the module's default implementation.<br />

When building our first module in Chapter 2, we saw a brief example of this in action<br />

as follows:<br />

$variables = array('items' => $list, 'type' => 'ol');<br />

$content = theme('item_list', $variables);<br />

By calling the theme() function, we are delegating the responsibility of determining<br />

and using the proper theme implementation. We're saying:<br />

"Hey, theme()! I want to markup my data as an item_list. Can you do that for me?<br />

I don't need to know the details. kthxbye."<br />

Our module just needs to decide which theme hook it wants to use to markup its<br />

data. Should the data be displayed in an unordered list, a table, or a wordle?<br />

Hook crazy?<br />

In addition to API hooks, <strong>Drupal</strong> also has theme hooks. A theme<br />

hook is simply the name of a particular way to markup some data. For<br />

example, passing data to the item_list theme hook will result in<br />

different markup then passing data to the links theme hook. However,<br />

while normally every module's hook function will be called when <strong>Drupal</strong><br />

invokes an API hook, only one theme hook implementation will be<br />

invoked when <strong>Drupal</strong> invokes a theme hook.<br />

[ 63 ]

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

Saved successfully!

Ooh no, something went wrong!