mailsystem.theme.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * @file
  4. * The theme system, which controls the output of email messages.
  5. */
  6. /**
  7. * Implements hook_theme_registry_alter().
  8. */
  9. function mailsystem_theme_theme_registry_alter(&$theme_registry) {
  10. global $theme_key;
  11. static $recursion_prevention = FALSE;
  12. // Prevent recursive execution.
  13. if ($recursion_prevention) {
  14. return;
  15. }
  16. $recursion_prevention = TRUE;
  17. $mailsystem_theme = mailsystem_get_mail_theme();
  18. // Only take action if the mailsystem theme is not the current theme.
  19. if ($mailsystem_theme != $theme_key) {
  20. $themes = list_themes();
  21. // Get the mailsystem theme to be used for rendering emails.
  22. if (isset($themes[$mailsystem_theme])) {
  23. $theme = clone $themes[$mailsystem_theme];
  24. if (isset($theme)) {
  25. // Swap to the mailsystem theme.
  26. $old_theme = $theme_key;
  27. mailsystem_theme_swap_theme($mailsystem_theme);
  28. // Establish variables for further processing.
  29. $base_theme = array();
  30. if (isset($theme->base_themes)) {
  31. foreach (array_keys($theme->base_themes) as $base) {
  32. $base_theme[$base] = clone $themes[$base];
  33. }
  34. }
  35. if (isset($theme->base_theme) && !isset($base_theme[$theme->base_theme])) {
  36. $base_theme[$theme->base_theme] = clone $themes[$theme->base_theme];
  37. }
  38. if (isset($theme->engine)) {
  39. $theme_engine = $theme->engine;
  40. }
  41. // Include template files to let _theme_load_registry add preprocess
  42. // functions.
  43. foreach ($base_theme as $base) {
  44. include_once drupal_get_path('theme', $base->name) . '/template.php';
  45. }
  46. include_once drupal_get_path('theme', $theme->name) . '/template.php';
  47. // Get the theme_registry cache.
  48. $cache = _theme_load_registry($theme, $base_theme, $theme_engine);
  49. // Change the registry for hooks with a 'mail theme' element.
  50. foreach ($theme_registry as $name => $hook) {
  51. if (!empty($hook['mail theme'])) {
  52. if (isset($cache[$name])) {
  53. $cache[$name]['includes'][] = drupal_get_path('theme', $theme->name) . '/template.php';
  54. foreach ($base_theme as $base) {
  55. $cache[$name]['includes'][] = drupal_get_path('theme', $base->name) . '/template.php';
  56. }
  57. // Change the current registry for the new record.
  58. $theme_registry[$name] = $cache[$name];
  59. }
  60. // Look for template suggestions.
  61. foreach ($cache as $cache_name => $cache_hook) {
  62. if (strpos($cache_name, $name . '__') !== FALSE) {
  63. $cache_hook['includes'][] = drupal_get_path('theme', $theme->name) . '/template.php';
  64. foreach ($base_theme as $base) {
  65. $cache_hook['includes'][] = drupal_get_path('theme', $base->name) . '/template.php';
  66. }
  67. // Change the current registry for the new record.
  68. $theme_registry[$cache_name] = $cache_hook;
  69. }
  70. }
  71. }
  72. }
  73. // Swap back to the original theme.
  74. mailsystem_theme_swap_theme($old_theme);
  75. }
  76. }
  77. }
  78. $recursion_prevention = FALSE;
  79. }
  80. /**
  81. * Helper to swap themes safely for use by mailsystem_theme_theme_registry_alter().
  82. */
  83. function mailsystem_theme_swap_theme($new_theme) {
  84. // Make sure the theme exists.
  85. $themes = list_themes();
  86. if (empty($themes[$new_theme])) {
  87. return;
  88. }
  89. // Both theme/theme_key are set to the new theme.
  90. global $theme, $theme_key;
  91. $theme = $theme_key = $new_theme;
  92. // Create the base_theme_info.
  93. global $base_theme_info;
  94. $base_theme = array();
  95. $ancestor = $theme;
  96. while ($ancestor && isset($themes[$ancestor]->base_theme)) {
  97. $ancestor = $themes[$ancestor]->base_theme;
  98. $base_theme[] = $themes[$ancestor];
  99. }
  100. $base_theme_info = array_reverse($base_theme);
  101. // Some other theme related globals.
  102. global $theme_engine, $theme_info, $theme_path;
  103. $theme_engine = $themes[$theme]->engine;
  104. $theme_info = $themes[$theme];
  105. $theme_path = dirname($themes[$theme]->filename);
  106. // We need to reset the drupal_alter and module_implements statics.
  107. drupal_static_reset('drupal_alter');
  108. drupal_static_reset('module_implements');
  109. }