Drupal: Exposed

Peeking behind the curtain at what makes Drupal tick

Here at Lullabot, we've trained hundreds of new Drupalistas in public workshops and on-site private trainings over the years. One of the primary things that people new to Drupal struggle with, once they grasp the basic concept of the hook system and the theme system, is figuring out the "big picture" of what all they actually have to work with to customize their own Drupal sites.

This article will expose a couple of quick core hacks* that we sometimes use in class to help gain insight into all of the hooks, template files, and theme functions that Drupal makes available on a specific page request.

* Note: Under normal circumstances, we would never recommend that you "hack core" (modify core files). However, temporarily, on a local test site, and for educational purposes only, sometimes it's okay. Maybe. ;)

Hooks

The Drupal API site offers a comprehensive list of all core hooks that Drupal registers. But unless you have the very simplest of sites, chances are you have one or more contributed modules as well which aren't (currently) displayed on that site. Furthermore, a huge list of what hooks exist doesn't help you when you really want to know what hooks your particular Drupal site exposes so that you know where you can cut in and customize Drupal's behaviour.

Fortunately, all hooks in Drupal run through a function called module_implements(). So you can do a quick hack there to gain insight into what hooks fire on a given page.

In includes/module.inc, find the module_implements() function (around line 595 in Drupal 7, and 415 in Drupal 6). At the top of the function, just after the line:

  
function module_implements($hook, $sort = FALSE, $reset = FALSE) {
  

add:

  
drupal_set_message("hook_$hook");
  

This gives you output similar to this (click for full version):


Note that there are also hooks that fire after the page is displayed, which won't be visible until the next page load.

Theme system

The Theme Developer module lets you highlight any section of a Drupal page in a Firebug-esque manner and find out where it comes from. This is incredibly useful when you want to override one specific part of the page.

But sometimes, it can be useful to get a "bird's eye" picture of all of the template files and theme functions that make up a given page.

All output on the page is routed through a function called theme() before being presented to the browser. This makes it a nifty place to add some small hacks to gain insight as to what's going on when a page is rendered.

Template files

In includes/theme.inc in the theme() function, around line 925 (Drupal 7) or 730 (Drupal 6), change:

  
$output = $render_function($template_file, $variables);
  

to:

  
    $output = '' . $hook . $extension;
    $output .= $render_function($template_file, $variables);
    $output .= '';
  

This simple hack will give you a picture such as this when you reload the page (click for full version):


Theme functions

A similar hack can show you all the theme functions that create a page. This is not recommended to run with the previous hack or you will have a mess. :)

In includes/theme.inc in the Drupal 7 theme() function, around line 880, change:

  
      $output = $info['function']($variables);
  

to:

  
      $output = '' . $info['function'];
      $output .= $info['function']($variables);
      $output .= '';
  

The same hack in Drupal 6's theme() function is around line 655, change:

  
    $output = call_user_func_array($info['function'], $args);
  

to:

  
    $output = '' . $info['function'];
    $output .= call_user_func_array($info['function'], $args);
    $output .= '';
  

This gives you output similar to this (click for full version):


Other suggestions?

Are there any other quick hacks you can suggest for gaining insight into what's going on under Drupal's hood?

Get in touch with us

Tell us about your project or drop us a line. We'd love to hear from you!