Wednesday 28 December 2011

A trick to find unused PHP files (Drupal template files)


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);
}

No comments:

Post a Comment