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

Before we begin building the required single_blog_block_item theme hook, let's<br />

first add all the data we will need for the third version of our module. Looking back<br />

at how we generate the items for our list, we can see that all the data we want is in<br />

the $node variable.<br />

// Create links for each blog entry.<br />

$items = array();<br />

foreach ($result as $node) {<br />

$items[] = array(<br />

'data' => array(<br />

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

'#title' => $node->title,<br />

'#href' => 'node/' . $node->nid,<br />

),<br />

'class' => array('node-' . $node->nid),<br />

);<br />

}<br />

So, instead of creating a simple render element using bits of the $node variable, let's<br />

just pass that entire variable to our new theme hook:<br />

// Create links for each blog entry.<br />

$items = array();<br />

foreach ($result as $node) {<br />

$items[] = array(<br />

'data' => array(<br />

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

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

),<br />

'class' => array('node-' . $node->nid),<br />

);<br />

}<br />

hook_theme() implementations<br />

We'll need to create a single_blog_theme() implementation of hook_theme() and<br />

register a single_blog_block_item theme hook.<br />

/**<br />

* Implements hook_theme().<br />

*/<br />

function single_blog_theme($existing, $type, $theme, $path) {<br />

return array(<br />

'single_blog_block_item' => array(<br />

[ 107 ]

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

Saved successfully!

Ooh no, something went wrong!