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, that was an extremely simple example, but when we start looking at more<br />

complex render elements, we'll see that each array key in a render element can be<br />

one of the following three things:<br />

1. A render element property. These are prefixed by #.<br />

2. A child element. All array keys not prefixed by # are considered to be<br />

a child elements.<br />

3. A variable to be passed to a theme function. In the render element these<br />

variable's names are prefixed with # (just like properties are), but theme()<br />

will strip the # from the name before sending it on to the actual theme<br />

implementation.<br />

Taking these slightly mush rules, we can examine the following render element:<br />

$element = array(<br />

'#prefix' => '',<br />

'#suffix' => '!',<br />

'kitten' => array(<br />

'#type' => 'link',<br />

'#title' => t('Kill me'),<br />

'#href' => 'admin/core/hack',<br />

),<br />

'separator' => array(<br />

'#markup' => '',<br />

),<br />

'domo' => array(<br />

'#theme' => 'username',<br />

'#account' => $account,<br />

),<br />

);<br />

First, we should identify the children since they are the simplest to spot. kitten,<br />

separator, and domo are the child elements of our render element. The separator<br />

child element is another example of a simple #markup render element.<br />

Looking at the domo element, we see that its #theme property is set to username.<br />

drupal_render() will take that child element and pass it to theme() with a theme<br />

hook of username; meaning that theme('username', $element['domo']) will be<br />

called and theme() will strip the # characters from the front of all of the variables<br />

before passing the data to theme_username().<br />

[ 78 ]

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

Saved successfully!

Ooh no, something went wrong!