Theming Layers in Drupal
As you might already know, Drupal nodes are composed of multiple template files assembled together and displayed to the user.
An elegant and recommended solution to theme layers in Drupal is to use hook_theme(). This hook allows us to declare theme_hooks and return an array with the information about the hook, where the keys are the hook name and its values, plus general information about the hook. Here’s a simple example:
/*** Implements hook_theme()*/function mymodule_theme() { return array( 'mymodule_template' => array( // Theme hook name. 'template' => 'templates/mymodule-template', // Path to your template file. 'arguments' => array(''), // Passed default arguments. ), );}
We declare the hook name after the implementation type and give the full path to the PHPtemplate file (Drupal will look for the mymodule-template.tpl.php file in the template folder).
So how will we use this hook? Suppose we use hook_menu() to declare our custom callback. In menu callback, we will call our theme hook and pass some variables to be processed.
/*** Implements hook_menu()*/function mymodule_menu() { $items['mymodule/callback'] = array( 'page callback' => 'mymodule_callback', 'age arguments' => array(), 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); return $items;}
Now, let’s take a look at the menu callback. We will call the ($hook, $variables = array()) function to theme the output of the callback. The first argument is the theme’s hook name, and the second is an array of variables to be rendered.
/*** Menu callback.*/function mymodule_callback() { $output = theme('my_template', array('page_title' => 'Using theme hooks', 'page_text' => 'This is some text...')); return $output;}
Our callback will invoke the my_template hook and will render the variables passed as arguments. The result will be a page rendered with mymodule-template.tpl.php. With the content of PHPTemplate files, we can customize the html output by adding class attributes, changing markup order or print other variables.
<div class="custom-template"> <h2 class="title"><?php if ($page_title): print $page_title; endif; ?></h2> <div class="text"> <?php if ($page_text):?> <p><?php print $page_text; ?></p> <?php endif; ?> </div> <div class="alter-data"> <?php if ($alter_data): ?> <p><?php print $alter_data; ?></p> <?php endif; ?> </div></div>
You might be wondering how to override theme hooks. We can do this by using a function called THEME_HOOK(). We use it to modify variables, array structure, or alter variables before the template field is invoked with the TEMPLATE PREPROCESS function, in our case module_preprocess_HOOK(&$variables);
/*** Template_preprocess().*/function mymodule_preprocess_my_template(&$variables) { $variables['page_text'] .= ' This line was added from preprocess_my_template().';}
The theme layer approach is a clean an easy way to create and customize template files. Hope you find it useful!
Tags: DrupalDevelopmentthemingCheck this option to include this post in Planet Drupal aggregator: planetTopics: Tech & Development