forked from backdrop-contrib/mailsystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailsystem.theme.inc
124 lines (109 loc) · 4.2 KB
/
mailsystem.theme.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* @file
* The theme system, which controls the output of email messages.
*/
/**
* Implements hook_theme_registry_alter().
*/
function mailsystem_theme_theme_registry_alter(&$theme_registry) {
global $theme_key;
static $recursion_prevention = FALSE;
// Prevent recursive execution.
if ($recursion_prevention) {
return;
}
$recursion_prevention = TRUE;
$mailsystem_theme = mailsystem_get_mail_theme();
// Only take action if the mailsystem theme is not the current theme.
if ($mailsystem_theme != $theme_key) {
$themes = list_themes();
// Get the mailsystem theme to be used for rendering emails.
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)) {
foreach (array_keys($theme->base_themes) as $base) {
$base_theme[$base] = clone $themes[$base];
}
}
if (isset($theme->base_theme) && !isset($base_theme[$theme->base_theme])) {
$base_theme[$theme->base_theme] = clone $themes[$theme->base_theme];
}
if (isset($theme->engine)) {
$theme_engine = $theme->engine;
}
// Include template files to let _theme_load_registry add preprocess
// functions.
foreach ($base_theme as $base) {
include_once(backdrop_get_path('theme', $base->name) . '/template.php');
}
include_once(backdrop_get_path('theme', $theme->name) . '/template.php');
// Get the theme_registry cache.
$cache = _theme_load_registry($theme, $base_theme, $theme_engine);
// Change the registry for hooks with a 'mail theme' element.
foreach ($theme_registry as $name => $hook) {
if (!empty($hook['mail theme'])) {
if (isset($cache[$name])) {
$cache[$name]['includes'][] = backdrop_get_path('theme', $theme->name) . '/template.php';
foreach ($base_theme as $base) {
$cache[$name]['includes'][] = backdrop_get_path('theme', $base->name) . '/template.php';
}
// Change the current registry for the new record.
$theme_registry[$name] = $cache[$name];
}
// Look for template suggestions.
foreach ($cache as $cache_name => $cache_hook) {
if (strpos($cache_name, $name . '__') !== FALSE) {
$cache_hook['includes'][] = backdrop_get_path('theme', $theme->name) . '/template.php';
foreach ($base_theme as $base) {
$cache_hook['includes'][] = backdrop_get_path('theme', $base->name) . '/template.php';
}
// Change the current registry for the new record.
$theme_registry[$cache_name] = $cache_hook;
}
}
}
}
// 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 backdrop_alter and module_implements statics
backdrop_static_reset('backdrop_alter');
backdrop_static_reset('module_implements');
}