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.

Chapter 3<br />

Theme registry<br />

So all of the wonderful things that theme() does, take a considerable amount of<br />

work. It's extremely inefficient for theme() to determine all of this information<br />

about theme hooks on the fly each time it's called. So to help its load and improve<br />

performance, <strong>Drupal</strong> uses a theme registry.<br />

The theme registry holds a list of all the theme hooks known by the system, and for<br />

each theme hook, it also stores the following:<br />

• Whether it's a theme function or a template<br />

• The list of preprocess functions to run<br />

• The list of process functions to run<br />

• The path to the template file (which includes whether the original module<br />

template file is used or a theme version of it.)<br />

• The theme function name (which indicates if it's the original module theme<br />

function or one overridden by a theme.)<br />

• The list of variables that should be passed to theme() when this theme hook<br />

is called and a default value for each variable<br />

• Whether, instead of a list of variables, the theme function expects a<br />

single render element as its parameter, and what that render element<br />

should be named<br />

While this theme registry does improve performance for the website user, it does<br />

cause some inconvenience for module and theme developers. Since <strong>Drupal</strong> is caching<br />

data about theme hooks, if you are actively writing or altering a theme hook, you'll<br />

need to make sure that you rebuild the theme registry before testing your changes.<br />

Fortunately, this can easily by accomplished by clicking the Clear all caches button<br />

on the Performance page found in the Configuration admin (admin/config/<br />

development/performance). The devel module also has a handy Rebuild the<br />

theme registry on every page load option in its settings.<br />

Variable default values<br />

Earlier when we talked about theme functions and the single $variables parameter,<br />

you may have noticed one short-coming of that parameter. When you have a normal<br />

list of parameters in a function definition, you specify the default values for parameters<br />

that are optional. This allows code to only list a few parameters when calling the<br />

function and have the additional parameters just get sensible default values.<br />

[ 85 ]

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

Saved successfully!

Ooh no, something went wrong!