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

The following is a screenshot of version one of our module's block:<br />

Render element and a theme hook suggestion<br />

Ok, let's first fix the bug we left in our first version of hook_block_view() by<br />

converting that incorrect theme() call into a render array. This is actually quite<br />

easy, we simply need to edit our single_blog_block_view() and replace:<br />

$block['content'] = theme('item_list', array('items' => $items));<br />

with the following code:<br />

$block['content'] = array(<br />

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

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

);<br />

As you can see from the new code, we just need to convert all the array keys passed<br />

in the $variables parameter to theme() into # prefixed key names. items becomes<br />

#items, and so on. We're also going to take this opportunity to add a hook theme<br />

suggestion as described in Chapter 3. theme() will check for a item_list__single_<br />

blog implementation before using the default item_list theme hook.<br />

[ 99 ]

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

Saved successfully!

Ooh no, something went wrong!