You are here

function themekey_init in ThemeKey 6.2

Same name and namespace in other branches
  1. 6.4 themekey.module \themekey_init()
  2. 6 themekey.module \themekey_init()
  3. 6.3 themekey.module \themekey_init()

Implements hook_init().

Here happens all the magic of ThemeKey. ThemeKey detects if any Theme Switching Rule matches the current request and sets the global variable $custom_theme.

File

./themekey.module, line 130
ThemeKey is meant to be a generic theme switching module.

Code

function themekey_init() {
  global $theme, $custom_theme;
  define('THEMEKEY_PAGECACHE_UNSUPPORTED', 0);
  define('THEMEKEY_PAGECACHE_SUPPORTED', 1);
  define('THEMEKEY_PAGECACHE_TIMEBASED', 2);
  if (isset($theme)) {
    if (variable_get('themekey_debug_trace_rule_switching', FALSE)) {
      themekey_set_debug_message('Skipped rule checking because another module already initialized the theme engine. $theme has been set to %theme.<br />This seems to be a bug. Visit !link and help us improving other modules.', array(
        '%theme' => $theme,
        '!link' => l('drupal.org/node/754970', 'http://drupal.org/node/754970'),
      ));
    }
  }
  elseif (strpos($_GET['q'], 'admin/build/block') !== 0 && strpos($_SERVER['SCRIPT_NAME'], 'cron.php') === FALSE) {

    // don't change theme when another module already set a $custom_theme like system.module does (administration theme) until administrator turned on this feature
    if (!$custom_theme || variable_get('themekey_override_custom_theme', 0)) {
      require_once drupal_get_path('module', 'themekey') . '/themekey_base.inc';
      $theme_candidate = themekey_match_rules();

      // If no theme has been triggered but a theme
      // is in the user's session, use that theme.
      if (!$theme_candidate && !empty($_SESSION['themekey_theme']) && (!$custom_theme || $custom_theme == variable_get('theme_default', 'garland'))) {
        $theme_candidate = $_SESSION['themekey_theme'];
        if (variable_get('themekey_debug_trace_rule_switching', FALSE)) {
          themekey_set_debug_message('ThemeKey Debug: No rule triggered a different theme. Reusing last theme from users session: %custom_theme', array(
            '%custom_theme' => $theme_candidate,
          ));
        }
      }

      // We have a theme, apply it
      if (!empty($theme_candidate) && $theme_candidate != 'default') {
        if (user_is_logged_in() && variable_get('themekey_theme_maintain', 0) || !user_is_logged_in() && variable_get('themekey_theme_maintain_anonymous', 0)) {
          $_SESSION['themekey_theme'] = $theme_candidate;
        }
        else {
          unset($_SESSION['themekey_theme']);
        }
        $custom_theme = $theme_candidate;
        if (variable_get('themekey_debug_trace_rule_switching', FALSE)) {
          themekey_set_debug_message('Switching theme to %custom_theme.', array(
            '%custom_theme' => $custom_theme,
          ));
        }
      }
      elseif (variable_get('themekey_debug_trace_rule_switching', FALSE)) {
        if ($custom_theme) {

          // static rules set $theme_candidate to 'default and $cutom_theme directly
          themekey_set_debug_message('$custom_theme has been set to %custom_theme during rule matching.', array(
            '%custom_theme' => $custom_theme,
          ));
        }
        else {
          themekey_set_debug_message('Using default theme.');
        }
      }
    }
    elseif (variable_get('themekey_debug_trace_rule_switching', FALSE)) {
      if ($custom_theme) {
        themekey_set_debug_message('Skipped rule checking because $custom_theme already set to %custom_theme by another module.', array(
          '%custom_theme' => $custom_theme,
        ));
      }
    }
  }
  elseif (variable_get('themekey_debug_trace_rule_switching', FALSE)) {
    if (strpos($_GET['q'], 'admin/build/block') !== 0) {
      themekey_set_debug_message('Rule checking disabled on block configuration.');
    }
  }
  if (variable_get('themekey_debug_show_property_values', FALSE) && module_exists('themekey_debug')) {
    themekey_debug_properties();
  }
}