diff --git a/mailsystem.theme.inc b/mailsystem.theme.inc index 8a22b91..a170f2e 100644 --- a/mailsystem.theme.inc +++ b/mailsystem.theme.inc @@ -26,6 +26,10 @@ function mailsystem_theme_theme_registry_alter(&$theme_registry) { if (isset($themes[$mailsystem_theme])) { $theme = clone $themes[$mailsystem_theme]; if (isset($theme)) { + // Swap to the mailsystem theme. + $old_theme = $theme_key; + mailsystem_theme_swap_theme($mailsystem_theme); + // Establish variables for further processing. $base_theme = array(); if (isset($theme->base_themes)) { @@ -42,10 +46,10 @@ function mailsystem_theme_theme_registry_alter(&$theme_registry) { // Include template files to let _theme_load_registry add preprocess // functions. - include_once(drupal_get_path('theme', $theme->name) . '/template.php'); foreach ($base_theme as $base) { include_once(drupal_get_path('theme', $base->name) . '/template.php'); } + include_once(drupal_get_path('theme', $theme->name) . '/template.php'); // Get the theme_registry cache. $cache = _theme_load_registry($theme, $base_theme, $theme_engine); @@ -75,8 +79,46 @@ function mailsystem_theme_theme_registry_alter(&$theme_registry) { } } } + + // Swap back to the original theme. + mailsystem_theme_swap_theme($old_theme); } } } $recursion_prevention = FALSE; } + +/** + * Helper to swap themes safely for use by mailsystem_theme_theme_registry_alter(). + */ +function mailsystem_theme_swap_theme($new_theme) { + // Make sure the theme exists. + $themes = list_themes(); + if (empty($themes[$new_theme])) { + return; + } + + // Both theme/theme_key are set to the new theme. + global $theme, $theme_key; + $theme = $theme_key = $new_theme; + + // Create the base_theme_info. + global $base_theme_info; + $base_theme = array(); + $ancestor = $theme; + while ($ancestor && isset($themes[$ancestor]->base_theme)) { + $ancestor = $themes[$ancestor]->base_theme; + $base_theme[] = $themes[$ancestor]; + } + $base_theme_info = array_reverse($base_theme); + + // Some other theme related globals. + global $theme_engine, $theme_info, $theme_path; + $theme_engine = $themes[$theme]->engine; + $theme_info = $themes[$theme]; + $theme_path = dirname($themes[$theme]->filename); + + // We need to reset the drupal_alter and module_implements statics. + drupal_static_reset('drupal_alter'); + drupal_static_reset('module_implements'); +}