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

}<br />

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

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

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

'#pre_render' =><br />

array('single_blog_item_list_child_render'),<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 />

// Add a CSS file to style the block.<br />

$block['content']['#attached']['css'][] =<br />

drupal_get_path('module', 'single_blog') . '/single-blog.css';<br />

}<br />

}<br />

}<br />

return $block;<br />

Steps to build a default theme<br />

implementation<br />

Okay, now it's time to exorcise our lazy-developer habit and practice building<br />

our own theme hook. From Chapter 3, you should recall that we'll need to do the<br />

following things:<br />

1. Register the theme hook and define default variables.<br />

2. Build the default implementation of our theme hook.<br />

3. Re-build the theme registry.<br />

4. Build a render element to use the theme hook.<br />

Our current implementation of the Recent blog posts block simply shows a list of<br />

blog titles. But it would be nice to include the date of each post, as well as the author<br />

(if we have multiple people creating posts). So in this third and final version of our<br />

module, we're going to create a single-blog-block-item.tpl.php to render the<br />

contents of each item in our list of blog posts. By convention in <strong>Drupal</strong>, any CSS,<br />

JavaScript, or template files needed by a module should use dashes instead<br />

of underscores in their filenames.<br />

[ 106 ]

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

Saved successfully!

Ooh no, something went wrong!