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 />

We've also specified a pattern for that hook. After the theme registry retrieves the<br />

information from our retro_theme() function, it will try to auto-discover any<br />

template files whose names begin with the pattern that we specify. So the theme<br />

registry will search for any template beginning with wonder__ and will add an entry<br />

for it in the theme registry. When theme() is passed theme hook suggestions, it<br />

doesn't search the file directory, it searches the theme registry instead, so this<br />

pattern is essential to making theme hook suggestions work.<br />

Lastly in our wonder definition, we specified several variables with the 'variables'<br />

key/value set, namely, twin (a string), with_monkey (a boolean), and activations<br />

(an array), and gave them each a default value.<br />

Instead of a list of variables, the theme_austinite() function expects a single<br />

render element as its parameter, so we need to specify the name of the render<br />

element. We'll be able to access the render element of theme_austinite() from<br />

$variables['my_element']. In the preceding example, we used my_element to<br />

make it obvious that the value specified in render element is the key that needs to<br />

be used in $variables to access that element. However, by convention, <strong>Drupal</strong> core<br />

usually uses element or elements for that value.<br />

To be clear, a theme hook should either use 'render element' or 'variables' in its<br />

dataset. Note that even if you use 'variables', one of those variables can still be a<br />

render element.<br />

Finally, we used a file key to specify that we're storing our theme_austinite()<br />

function in a retro.texas.inc file. theme() will lazy load this file if it needs to<br />

use this theme hook on a particular page.<br />

Full documentation can be found in the DocBlock for the hook_theme() API at the<br />

following site:<br />

http://api.drupal.org/api/function/hook_theme/7<br />

hook_theme_registry_alter<br />

There are some extreme cases where a module may want to alter the registry<br />

entry for a particular theme hook. Fortunately, there's a hook for that. The<br />

hook_theme_registry_alter() hook can be implemented to alter the theme<br />

registry directly after it has been built with hook_theme() and the theme system's<br />

auto-discovery mechanisms. Let's suppose a Spook module wants to control<br />

whether <strong>Drupal</strong>'s status messages are displayed to the user. If you look at<br />

template_preprocess_page(), you'll see that the $show_messages variable<br />

controls whether the status messages are displayed. Unfortunately, if we tried<br />

[ 88 ]

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

Saved successfully!

Ooh no, something went wrong!