Drupal: Theme Forensics

Doing a back trace through Drupal code on exactly where an undesired activity is originating is a fairly well documented challenge. Once you've finally figured out that you want to look at something in a theming function, you still have to figure out which one. There are theme functions, more than one, in almost every module that displays something, and between core hooks and module hooks, you can pull a lot of hair out looking for the the one you need. There is a trick to making the search a little easier.

I want to be careful about advocating changes to one's core files, but this one is relatively benign, even on a production system. Crack open your theme.inc in the included directory, and search for the theme() function. About midway through it, you'll find this section:

  if (isset($info['function'])) {
    // The theme call is a function.
    //$output = call_user_func_array($info['function'], $args);
    $output = '<!-- currenttheme function: ' . $info['function'] . ' -->' . call_user_func_array($info['function'], $args) . '<!-- end ' . $info['function'] . ' -->' ;

Add the line beginning with "$output = '<!--", while commenting out the original, beginning with "$output = call_user_func_array". It will add a comment in the server rendered HTML at the beginning of every section being run through a theme function. If you view the HTML source on a rendered page, you will see something like this:

<!-- current theme function: theme_blocks --><!-- end theme_blocks -->

It would be nice to say "There's your answer" but you could guess that it isn't that simple. For one thing, there are nested theme functions that impact different parts of any block of rendered HTML, so you can easily encounter something like this:

<!-- end theme_form_element --><!-- end theme_textfield --><!-- current theme function: theme_submit --><!-- current theme function: theme_button --><input type="submit" name="op" id="edit-submit" value="Search"  class="form-submit" />
<!-- end theme_button --><!-- end theme_submit --><!-- current theme function: theme_hidden --><input type="hidden" name="form_build_id" id="form-a25cb3729b9a547ab59dc9427289148a" value="form-a25cb3729b9a547ab59dc9427289148a"  />
<!-- end theme_hidden --><!-- current theme function: theme_token --><!-- current theme function: theme_hidden --><input type="hidden" name="form_token" id="edit-search-block-form-form-token" value="406d7a6190fc9538041e4520e5b73897"  />
<!-- end theme_hidden --><!-- end theme_token --><!-- current theme function: theme_hidden --><input type="hidden" name="form_id" id="edit-search-block-form" value="search_block_form"  />
<!-- end theme_hidden -->

... but at least it's somewhere to start.

For more information on the topic, here's a good article on the Drupal site,