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

After all, if you choose an existing theme hook, you only have to build the data.<br />

You don't have to worry about building the implementation, registering it with<br />

hook_theme, building any helper pre-render functions, deciding on the best HTML<br />

to use, and building any supporting CSS and jQuery. What's not to like about<br />

skipping extra work?<br />

However, laziness isn't the only reason you'll want to reuse existing theme hooks.<br />

Not only is it the easy way out, you'll also discover interesting integrations with<br />

other modules. As we learned in Chapter 3, <strong>Drupal</strong>'s Theme Layer, modules can alter<br />

the way a theme implementation works by using preprocess/process functions.<br />

Those alterations aren't linked to only <strong>Drupal</strong> core's use of the theme hook; they are<br />

run even when your module uses the theme hook.<br />

For example, when writing the menu_block module, I could have used my own<br />

theme hooks to theme the various parts of the menu trees that it displays. However,<br />

I decided to reuse core's menu tree-related theme hooks and just pump the data<br />

with lots of extra meta-data that core's usage lacked. I later discovered that another<br />

module was designed to modify core's menu trees and make them expand and<br />

collapse dynamically. That module did this by altering core's theme hooks. Since<br />

my menu_block module used the same hooks, the two modules were instantly<br />

interoperable. Neither module developer had to write any integration code. Score!<br />

The hardest part to reusing existing theme hooks is simply finding the right one<br />

for you. You can browse all of <strong>Drupal</strong> core's default theme implementations at:<br />

http://api.drupal.org/api/group/themeable/7<br />

Any hook implemented as a theme function is listed first. The template files are listed<br />

next, under the Files section.<br />

In <strong>Drupal</strong> 7, there are 184 theme hooks that you could use in your own code.<br />

Many of those theme hooks are specific to a core module's usage, like theming an<br />

administration form, but it's still useful to go through the entire list to find hooks<br />

you could reuse. To make it even easier, we've included a list of the most commonly<br />

reused theme hooks:<br />

Common theme hooks<br />

file_link Returns HTML for a link to a file.<br />

html_tag Returns HTML for a generic HTML tag with attributes. This can often be<br />

too generic a theme hook to use, but is really useful for adding a tag to<br />

the of a document or for theming a tag inside a render element.<br />

image<br />

Returns HTML for an image.<br />

image_style Returns HTML for an image using a specific image style.<br />

item_list Returns HTML for a list of items which can optionally be nested.<br />

[ 92 ]

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

Saved successfully!

Ooh no, something went wrong!