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.

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

Now, if you were paying close attention to the form of the name, you'll also notice<br />

that these functions actually receive two parameters, namely, the $variables array<br />

and a $hook parameter. $hook, as the name suggests, contains the name of the<br />

actual theme hook currently being run. So, while a foo_preprocess(&$variables,<br />

$hook) function is run for every template file, it will still be able to tell which<br />

template is currently being requested. In fact, $hook is the second parameter for all<br />

preprocess functions, but $hook is only useful for multi-hook preprocess functions.<br />

For a good example of a multi-hook preprocess function, let's look at the function<br />

that the theme system uses to set up several variables common to all template<br />

files—the template_preprocess() function, which is as follows:<br />

function template_preprocess(&$variables, $hook) {<br />

// Tell all templates where they are located.<br />

$variables['directory'] = path_to_theme();<br />

}<br />

// Initialize html class attribute for the current hook.<br />

$variables['classes_array'] = array(drupal_html_class($hook));<br />

As you can see, this preprocess function creates a $directory variable which<br />

can be used to tell where the template file is located on the web server. In the<br />

$classes_array variable, it also starts to set up the CSS classes used in the<br />

outer-most wrapping div of the template.<br />

Process functions<br />

Obviously, inside our template file, when we print out our dynamically created list<br />

of classes, we'll need the variable to be a string. <br />

will, most unhelpfully print out "array". In earlier versions of <strong>Drupal</strong>, classes were<br />

dynamically created but were immediately concatenated into strings. So themes would<br />

see one long string with multiple classes in it, menu-block-wrapper menu-block-1<br />

menu-name-management, for example. This made removing or altering classes difficult<br />

as themers had to master PHP's string-manipulation functions or even (gasp!)<br />

regular expressions.<br />

In <strong>Drupal</strong> 7, this problem for themers has been solved using the new process<br />

functions. Process functions are an additional phase of variable processing<br />

functions that run after the initial preprocess functions. In all respects, process<br />

functions are exactly like preprocess functions; there are template_ prefixed process<br />

functions, multi-hook process functions, module-provided process functions, and<br />

theme-provided process functions. The only difference is that process functions are<br />

run after all preprocess functions have been run.<br />

[ 74 ]

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

Saved successfully!

Ooh no, something went wrong!