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

into these lines:<br />

'data' => array(<br />

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

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

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

),<br />

system_element_info() defines the link element and we can see from its<br />

declaration and examining its #pre_render callbacks that render elements<br />

with a #type of link will be rendered into a link using l().<br />

Unfortunately, the theme_item_list() function does not expect a render element<br />

in its data key, so it will choke on these contents. Considering the push to make<br />

everything alterable with render arrays, this seems like an oversight. If you<br />

encounter a bug or inconsistency in <strong>Drupal</strong> (and you will!), you can contribute<br />

simply by making a note of it and creating an issue in <strong>Drupal</strong>'s issue queue. In fact, I<br />

just created a new issue for this problem at http://drupal.org/node/891112.<br />

In the meantime, we'll have to work around this problem rather then hacking <strong>Drupal</strong><br />

core. Fortunately, a close inspection of the render element properties available to us<br />

(again, see Chapter 3) indicate that we can use a #pre_render callback to alter our<br />

render element before it is passed to the element's theme function. Let's add one<br />

to our $block['content']['list'] child element:<br />

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

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

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

'#pre_render' => array('single_blog_item_list_child_render'),<br />

);<br />

Since drupal_render() will pass the entire child element to our #pre_render<br />

callback, we just need to make sure our callback modifies the data element of<br />

all our items.<br />

/**<br />

* Render the child elements of theme_item_list() before its<br />

* data is themed.<br />

*/<br />

function single_blog_item_list_child_render($elements) {<br />

foreach (array_keys($elements['#items']) AS $key) {<br />

// Take the renderable array that we set in<br />

// single_blog_block_view() and render it into the string<br />

// that theme_item_list() expects.<br />

[ 101 ]

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

Saved successfully!

Ooh no, something went wrong!