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.

Theming a <strong>Module</strong><br />

Theming a <strong>Drupal</strong> block<br />

So, now that we know how to get all our data, we need to decide how<br />

we're going to theme it. We're actually going to build three versions of our<br />

single_blog_block_view(). This will allow us to try out several different methods<br />

of theming and to apply all the aspects of the theme layer we learned in Chapter 3.<br />

Looking back at the previous table, Common theme hooks, will help us decide which<br />

existing theme hooks would be good candidates for our data. Initially, we're creating<br />

a list of links to our blog posts, so theme_links() would actually be a perfect match.<br />

However, in the next iteration of our hook_block_view() function we're going to<br />

create more then a simple list of links, so let's look again. The item_list hook will<br />

allow us to create a list that contains arbitrary data.<br />

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

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

By looking at the documentation for theme_item_list() (available at<br />

http://api.drupal.org/api/function/theme_item_list/7) we can see that it<br />

expects as a parameter an items array. Each list item can be a simple string or the<br />

string can be placed in the data key of an array in which the other keys are treated<br />

as attributes for the list item:<br />

$items[] = array(<br />

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

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

);<br />

On the surface, this construct looks similar to the Render API, but this is simply the<br />

convention used by this theme function; it doesn't use drupal_render() to convert<br />

the array to a string.<br />

The internal path to a node is always "node/[node ID]". Fortunately, even if the<br />

author gives the blog post a URL alias, we don't have to figure out the alias as the<br />

l() function will automatically rewrite it to use the proper URL. So with the l()<br />

function, we're taking each node's title and node ID and constructing a simple link<br />

to the node and placing it in the list item's data element. theme_item_list() treats<br />

the class element as an attribute for the wrapping element.<br />

Okay. We've finished our first draft of our module. Looking at the following<br />

screenshot, you can see that our block displays as a simple unordered list of node<br />

titles. If this were a <strong>Drupal</strong> 6 theme implementation, we'd be done! However, in<br />

<strong>Drupal</strong> 7, all hook_block_view() and all page callbacks (the functions that return<br />

the main contents of a page) should return a renderable array instead of a string.<br />

So, while our code works (since <strong>Drupal</strong> 7 considers a plain string to be a degenerate<br />

renderable array), we'll need to fix that minor flaw in our second draft.<br />

[ 98 ]

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

Saved successfully!

Ooh no, something went wrong!