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

'menu_tree__' . $menu_name,<br />

'menu_tree',<br />

);<br />

theme($hook, $tree);<br />

This allows themers to provide either a THEME_menu_tree__menu_block__1()<br />

function to override the display of just the 1st configured menu block (with a<br />

delta ID of 1), or to override the display of all trees displaying the management<br />

menu with a THEME_menu_tree__menu_block__management(). Of course, if the<br />

theme provides none of those functions, theme() will continue to use the default<br />

implementation, theme_menu_tree(). To be clear about how this works, theme()<br />

takes the array of suggestions and checks them from left to right until it finds an<br />

actual implementation.<br />

If you look at the preceding figure again, you'll notice that after the preprocess and<br />

process functions have been run, theme() examines the $variables for additional<br />

theme hook suggestions. Of course, only the calling code can specify hook suggestions<br />

in the first parameter to theme(). In order to allow other modules and themes to add<br />

its own suggestions, theme() will examine two variables $theme_hook_suggestion<br />

and $theme_hook_suggestions. theme() will first check the singular form of that<br />

variable name. If the $theme_hook_suggestion string doesn't match an actual<br />

implementation, theme() will check the $theme_hook_suggestions array from<br />

left to right to find an implementation. If theme() doesn't find any implementations<br />

from those two variables, it will continue to use the theme hook it had previously<br />

been using.<br />

Since themes can completely override the value of $theme_hook_suggestion, it<br />

is recommended that modules stick to modifying the $theme_hook_suggestions<br />

array. This is how it is done:<br />

// Add our suggestions to the front of the list, since our<br />

// module is the most important one in the universe!<br />

array_unshift($variables['theme_hook_suggestions'],<br />

'links__retro__geocities', 'links__retro');<br />

Note that $theme_hook_suggestions and $theme_hook_suggestion do not<br />

take patterns. If you want theme() to look for links__retro__geocities<br />

and links__retro, you'll need to provide both of those strings in the<br />

$theme_hook_suggestions array.<br />

One last note, all of the above examples assumed theme functions, but the same is<br />

true for theme hook suggestions of template files. If you provide a node__blog__1<br />

pattern to theme(), it will search for a node--blog--1.tpl.php file and then for a<br />

node--blog.tpl.php file.<br />

[ 84 ]

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

Saved successfully!

Ooh no, something went wrong!