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.

Theming a <strong>Module</strong><br />

}<br />

'variables' => array(<br />

'node' => NULL,<br />

),<br />

'template' => 'single-blog-block-item',<br />

),<br />

);<br />

I'll explain the variables array in the very next section, but let's quickly go over the<br />

other key now.<br />

Since this is a theme hook, and is to be implemented using a template instead of a<br />

theme function, we'll need to include the template key and specify the base name<br />

of the template file, single-blog-block-item. <strong>Drupal</strong> will automatically add the<br />

.tpl.php to the end of the base name when looking for the file, so we shouldn't<br />

include it.<br />

Variables versus render element<br />

In Chapter 3, we learned about the differences between using the variables key and<br />

using the render element key in your hook_theme(). One and only one of those keys<br />

must be present in each theme hook declaration. However it still can be somewhat<br />

confusing as to which to use when you are building your theme implementation.<br />

There is only one situation in which you could use the render element key: if your<br />

data could be represented by a single render element or by a single renderable array<br />

containing nested render elements. If that is not the case, then you must specify the<br />

variables key and specify the variables you will be passed to theme() and their<br />

default values.<br />

So does our data conform to the render element requirement above? Our $node<br />

variable is just a partial node object and not a render element, so we must use the<br />

variables key and specify the default values for all our variables.<br />

As a side note, if we instead look at the way we've built the data element in the<br />

second version of our module (a link #type render element), we can see that we<br />

could go ahead and use render element as the key if our second version of the<br />

module had a hook_theme() implementation.<br />

Since the node variable is an object, we set the default value to simply be the<br />

NULL value.<br />

[ 108 ]

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

Saved successfully!

Ooh no, something went wrong!