Exercise Identify Themable Functions

The goal of this exercise is to modify the function responsible for calling themable functions in such a way that allows you to see in the HTML output which function was called. To do this, you will wrap the output from each call to a themable function in HTML comments like this:

<!-- BEGIN: theme_foo --> result of the themable function <!-- END: theme_foo -->

This way, you can look at the source code of a generated page and know exactly which themable function to override in order to change the output. This will be a handy tool while designing the theme for your site, but you won't want to use it on a production site.

To begin, locate the includes/theme.inc file and make a backup copy, which you will use later to restore your Drupal installation to its original state when you're finished with this exercise. In theme.inc,find the theme() function around line 160. This is the function that is responsible for determining exactly which themable function to call. It looks first for a function specific to your theme, then to the theme engine, and finally to the default theme_foo function, calling the first one found. Replace the theme() function with the following code:

function theme() { global $theme, $theme_engine;

if ($theme === NULL) { // Initialize the enabled theme. $theme = init_theme();

$args = func_get_args(); $function = array_shift($args);

if (($theme != '') && function_exists($theme .'_'. $function)) { // call themable function $function = $theme .'_'. $function;

elseif (($theme != '') && isset($theme_engine) && function_exists($theme_engine .'_'. $function)) { // call engine function $function = $theme_engine .'_'. $function;

elseif (function_exists('theme_'. $function)){ // call Drupal function $function = 'theme_'. $function;

$output = call_user_func_array($function, $args);

if (trim($output)) { return "<!-- BEGIN: $function -->\n$output\n<!-- END: $function -->\n";

The beauty of Drupal's theme mechanism lies in the code call_user_func_array($function, $args), which executes the function $function (the dynamically chosen themable function) with the parameters found in the array $args. This code differs from the original mainly in that it stores the product of this call in a variable $output, and as long as $output is more than mere white space, wraps it in the HTML comments as discussed earlier.

The final result is that your site should look the same as before in the browser, but when you view the source code, every section of HTML will be nested in comments identifying the specific themable function that was responsible for that segment of the output. Here is an example:

<!-- BEGIN: phptemplate_block --> <div class="block block-user" id="block-user-1"> <h2 class="title">admin</h2> <div class="content"><div class="menu">

<!-- BEGIN: theme_menu_item -- > <li class="collapsed">

<!-- BEGIN: theme_menu_item_link --> <a href="?q=node/add" >create content</a>

<!-- END: theme_menu_item_link -- > </li>

This is an excerpt of the HTML that renders the main navigation menu using the Bluemarine theme. Notice how easy it is to identify which themable function generates which HTML. You can see, for example, that the create content link was generated by a function named theme_menu_item_link. Searching the codebase for function theme_menu_item_link will guide you to the includes/menu.inc file, where you can view and dissect how the HTML was made.

You can find functions starting with the name of the theme engine, phptemplate, either in the theme itself (in the form of a function in template.php or as a tpl.php file) or in the theme engine (as a function in phptemplate.engine and corresponding tpl.php file).To find the code that handles the call to phptemplate_block in the previous example, you must first look for a block.tpl.php file in the theme's folder, themes/bluemarine/block.tpl.php in this case. In its absence, look for a phptemplate_block function in the theme's template.php file. If that also doesn't exist, then you can be certain that the theme engine itself is handling the call in the form of a tpl.php file, themes/engines/phptemplate/ block.tpl.php.

Article Marketing Gold

Article Marketing Gold

Discover How You Can Use The Huge Power of Article Marketing To Drive Highly Targeted Traffic To Your Website and Instantly Become THE Person Your Prospects Want To Buy From.

Get My Free Ebook


Post a comment