views:

74

answers:

1

I am looking for a trick to find included files that are not in use. Preferrably not by going trough them by hand. This project has over 400 of such files, 100 are probably unused by now.

They are Drupal template files (tpl.php) and were placed in the theme/template during development and -as always- were never removed when obsoleted.

Things I have thought of: * maintain a register in the database or a log, and spider the site. All files that don't appear in the log are candidates for removal, and need manual checking. * use a file-profiling tool such as cachegrind to render call-stacks: the files should appear in there somehow. I have no idea, however, how to get this done.

Problem in Drupal templates, is that they are very dynamic, so simply grepping for include_once() and the likes does not work.

How do you avoid template-lint in Drupal?

+4  A: 

Drupal registers every single theme implementation, including template files, in the theme registry. You could create a custom module and implement hook_theme_registry_alter() to inspect the theme registry to find what templates are being used by your theme. From there, you could compare your theme folder with the list you generated.

Example implementation:

function mymodule_theme_registry_alter(&$theme_registry) {
  global $theme_path;

  $templates_used = array();

  foreach ($theme_registry as $theme) {
    if (!empty($theme['template']) && $theme['path'] === $theme_path) {
      $templates_used[] = $theme['template'] . '.tpl.php';
    }
  }

  // Display the list (requires Devel module)
  dsm($templates_used);
}

Edit

If you don't want to implement hook_theme_registry_alter(), you could use theme_get_registry() to get the theme registry array and use the above technique to check for template files.

Mark Trapp
Thanks, that is what I was looking for. I tried with _phptemplate_variables() but it picked up far too much :)
berkes