diff --git a/includes/mimemail.admin.inc b/includes/mimemail.admin.inc
index d7ad8b8..da63d2e 100644
--- a/includes/mimemail.admin.inc
+++ b/includes/mimemail.admin.inc
@@ -78,6 +78,40 @@ function mimemail_admin_settings() {
'#attributes' => array('class' => array('filter-list')),
);
+ // Generate a list of options of which theme use to render the email
+ // current - Theme currently used by the user who runs drupal_mail
+ // default - Default theme, obtained via variable theme_default
+ // domain - Theme obtained via domain_theme module
+ // plus all the rest of active themes
+ $theme_options = array();
+ $theme_options['current'] = t('Current theme');
+ $theme_options['default'] = t('Default theme');
+ if (module_exists('domain_theme')) {
+ $theme_options['domain'] = t('Domain theme');
+ }
+ $theme_list = list_themes();
+ foreach ($theme_list as $position => $item) {
+ if ($item->status == 1) {
+ $theme_options[$position] = $item->info['name'];
+ }
+ }
+ $theme_description = '
' . t('Select the theme that wil be used to render the email:') . '
';
+ $theme_description .= '';
+ $theme_description .= '- '. t('Current theme: Currently used by the user who runs drupal_mail.') . '
';
+ $theme_description .= '- '. t('Default theme: Obtained via variable theme_default.') . '
';
+ if (module_exists('domain_theme')) {
+ $theme_description .= '- '. t('Domain theme: Obtained via domain_theme module.') . '
';
+ }
+ $theme_description .= '- '. t('Any active theme.') . '
';
+ $theme_description .= '
';
+ $form['mimemail']['mimemail_theme'] = array(
+ '#type' => 'select',
+ '#title' => t('Theme'),
+ '#description' => $theme_description,
+ '#options' => $theme_options,
+ '#default_value' => variable_get('mimemail_theme', 'current'),
+ );
+
$form['mimemail']['advanced'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced settings'),
@@ -137,5 +171,13 @@ function mimemail_admin_settings() {
drupal_set_message(t('Please choose a mail engine.'), 'error');
}
- return system_settings_form($form);
+ $form = system_settings_form($form);
+ // We need to run mimemail_admin_settings_submit after variable set
+ $form['#submit'][] = 'mimemail_admin_settings_submit';
+ return $form;
}
+
+
+function mimemail_admin_settings_submit($form, &$form_state) {
+ drupal_theme_rebuild();
+}
\ No newline at end of file
diff --git a/mimemail.module b/mimemail.module
index 9b4924b..47af1f0 100644
--- a/mimemail.module
+++ b/mimemail.module
@@ -449,3 +449,90 @@ function mimemail_prepare_message($message) {
return $message;
}
+
+/**
+* Implements hook_theme_registry_alter().
+*/
+function mimemail_theme_registry_alter(&$theme_registry) {
+ global $theme_key;
+ static $executed = array();
+
+ // Preventing double execution
+ if (isset($executed[$theme_key])) {
+ return;
+ }
+
+ $executed[$theme_key] = TRUE;
+ foreach ($theme_registry as $position => $item) {
+ if ($position == 'mimemail_message') {
+ $mimemail_theme = _mimemail_get_theme();
+ // We don't have to change anything in case the render theme is the same as the current render
+ if ($mimemail_theme != $theme_key) {
+ $themes = list_themes();
+ // Getting render theme
+ $theme = isset($themes[$mimemail_theme]) ? clone $themes[$mimemail_theme] : NULL;
+ if ($theme != NULL) {
+ // Disabling stylesheets and scripts to prevent from loading in _drupal_theme_initialize
+ $theme->stylesheets = array();
+ $theme->scripts = array();
+
+ // Stablishing variables for further process
+ $base_theme = array();
+ if (isset($theme->base_themes)) {
+ foreach (array_keys($theme->base_themes) as $base) {
+ $base_theme[$base] = clone $themes[$base];
+ // Disabling stylesheets and scripts to prevent from loading in _drupal_theme_initialize
+ $base_theme[$base]->stylesheets = array();
+ $base_theme[$base]->scripts = array();
+ }
+ }
+ $theme_engine = isset($theme->engine) ? $theme->engine : NULL;
+
+ _drupal_theme_initialize($theme, $base_theme, NULL);
+ $cache = _theme_load_registry($theme, $base_theme, $theme_engine);
+
+ // Changing current registry for the new record for
+ if (isset($cache[$position])) {
+ unset($theme_registry[$position]);
+ $theme_registry[$position] = $cache[$position];
+ }
+ }
+ }
+ }
+ }
+}
+
+
+/**
+ * Retrieves the theme key configured in admin/config/system/mimemail
+ * used to render emails.
+ * @return key of the theme configured to render emails
+ */
+function _mimemail_get_theme() {
+ global $theme_key;
+
+ $theme = variable_get('mimemail_theme', 'current');
+ switch ($theme) {
+ case 'default':
+ $theme = variable_get('theme_default', NULL);
+ break;
+ case 'current':
+ $theme = $theme_key;
+ break;
+ case 'domain_theme':
+ // Fetch the theme for the current domain.
+ if (module_exists('domain_theme')) {
+ // Assign the selected theme, based on the active domain.
+ global $_domain;
+ $domain_theme = domain_theme_lookup($_domain['domain_id']);
+ // The above returns -1 on failure.
+ if ($domain_theme != -1) {
+ return $domain_theme['theme'];
+ }
+ }
+ $theme = $theme_key;
+ break;
+ }
+
+ return $theme;
+}
\ No newline at end of file
diff --git a/theme/mimemail.theme.inc b/theme/mimemail.theme.inc
index 7285f51..d29e6f5 100644
--- a/theme/mimemail.theme.inc
+++ b/theme/mimemail.theme.inc
@@ -4,7 +4,7 @@
* @file
* The theme system, which controls the output of the messages.
*/
-
+
function mimemail_theme_theme() {
$path = drupal_get_path('module', 'mimemail') . '/theme';
@@ -31,18 +31,7 @@ function mimemail_theme_theme() {
* @see theme/mimemail-message.tpl.php for additional variables.
*/
function template_preprocess_mimemail_message(&$variables) {
- $theme = variable_get('theme_default', NULL);
-
- // Fetch the theme for the current domain.
- if (module_exists('domain_theme')) {
- // Assign the selected theme, based on the active domain.
- global $_domain;
- $domain_theme = domain_theme_lookup($_domain['domain_id']);
- // The above returns -1 on failure.
- if ($domain_theme != -1) {
- $theme = $domain_theme['theme'];
- }
- }
+ $theme = _mimemail_get_theme();
$themepath = drupal_get_path('theme', $theme);