background image

Content tagged with: devel

Eric's picture

The Devel module has a great feature called Theme Developer which makes theming a breeze. When enabled, you'll see a box in the bottom left of your browser window, themer info. This functionality helps answer the question: how do I change the way this piece of my theme looks? If you enable the themer info checkbox and moveover your theme, you'll noticed that various html structures will be highlighted in red, very much like the essential Firefox plugin, Firebug.

Let's say you wanted to redesign the appearance of the search form in your theme. If you click on search form using the themer info, you'll get a window full of useful information, similar to this:

This window tells you which functions and templates are used to create the html, and which functions and templates you can define to override the themable output. Check out the section called "File used". It should tell you that the search form uses it's own template file found here: modules/search/search-theme-form.tpl.php. In this case, we're in luck. You can simply copy that file into your theme directory and modify it as necessary.

In other situations, a function is used instead of a template tpl.php file. If you use the themer info tool and click on the navigation menu you'll notice a function was used (theme_menu_tree):

To modify the themable output, you'll have to create a function in your template.php called YOURTHEME_menu_tree. Before you go any further, read the API documentation for the function your are replacing. For this example, browse to: http://api.drupal.org/api/function/theme_menu_tree. Since Drupal will be passing a predefined set of arguments to our new function, it's best practice to use the same number and name of arguments that Drupal uses. Check out the sections in the API documentation called definition and code. You'll see that the theme_menu_tree function accepts one argument ($tree):

<?php
theme_menu_tree
($tree);
?>

So, in your template.php file, define your new function. The first thing you should do it get acquainted with the structure of the arguments and what the function returns (array, object, html, etc). In the code below, I'm using the PHP function print_r to show the structure of the argument. Now, you can modify to your heart's content.

<?php
function MYTHEME_menu_tree($tree) {
  print
"<pre>" . print_r($tree, true) . "</pre>";
}
?>

Now, you should be on your way to overriding the themable output of any piece of your site.

Eric's picture

Today, we were trying to theme a specific node type and ran into a few gotchas along the way.

Start by checking out the API documentation:
Working with template suggestions
Core templates and suggestions

First gotcha: The node type we were working with had an underscore in the name (for example: my_content_type). We assumed the node template file should be named: node-my-content-type.tpl.php. After some frustration, we realized the template file should be: node-mycontenttype.tpl.php.

We were able to figure this out by using the Devel module. This module implements the "Themer Information" functionality which is very useful when creating your theme. If you click on the div that contains your specific node type, it will provide you with information to determine how to override your themable output. In this case, the section "Candidate template files" pointed us in the right direction:

node-mycontenttype.tpl.php < node.tpl.php

Second gotcha: We copied the node.tpl.php from /modules/node/ into our theme directory and renamed it to: node-mycontenttype.tpl.php. We then put this line in the template.php file to reset the theme registry:

drupal_rebuild_theme_registry();

At this point, we expected our new theme file to be used and it was not. After some tinkering with hook_theme() and the theme registry, we realized node.tpl.php had to be in the theme directory as well. [to be continued...]