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

Converting a theme call to a render element was rather painless, no? Let's add<br />

some more content to our block. <strong>Drupal</strong> core's blog module provides a page (and<br />

menu callback) that displays teasers of recent blog posts at the path /blog. Since<br />

menu callbacks aren't part of the theme system, we're going to leave that page<br />

unimplemented, but let's pretend that we did implement it. In that case, our block<br />

should provide a more link that goes to the blog page.<br />

Adding additional content to a render element is easy. Since our $block['content']<br />

is a single render element, we first need to move that existing render element as a child<br />

element, which we can do by moving it to $block['content']['list']. The list<br />

name is just an arbitrary label that we are giving to a child element; as long as it doesn't<br />

start with a #, it doesn't matter much what we call it. We'll add our new more link as<br />

a sibling to the list child:<br />

// Theme the list of blog entries.<br />

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

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

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

);<br />

// Add a link to the full list of blog entries.<br />

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

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

'#url' => 'blog',<br />

'#title' => t('Read the latest blog entries.'),<br />

);<br />

Glancing back at our common theme hooks list, you should have noticed the<br />

more_link theme hook which we used for our block's more link. Again we just need<br />

to examine the documentation for theme_more_link to determine how to structure the<br />

child element. See http://api.drupal.org/api/function/theme_more_link/7.<br />

Creating a pre_render function<br />

Now looking at our content, you'll probably notice that there is one piece of content<br />

that is still locked up in a string instead of being modifiable in a render element:<br />

the data element of each of the $items passed to our item_list render element.<br />

Converting the l() call to a render element is again straightforward; just change<br />

the following line:<br />

'data' => l($node->title, 'node/' . $node->nid),<br />

[ 100 ]

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

Saved successfully!

Ooh no, something went wrong!