Skip to content

Instantly share code, notes, and snippets.

@rudiedirkx
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rudiedirkx/256a9c0f4b60d368984a to your computer and use it in GitHub Desktop.
Save rudiedirkx/256a9c0f4b60d368984a to your computer and use it in GitHub Desktop.
<?php
/**
* Wrapper for theme() that takes the override theme name to render this hook in.
*/
function _x_theme($hook, $vars, $override_theme) {
_x_start_theme_override($override_theme);
$return = theme($hook, $vars);
_x_end_theme_override();
return $return;
}
/**
* Start a global theme override. Starting now, everything theme related will use the given theme.
*/
function _x_start_theme_override($override_theme) {
global $theme, $theme_key, $theme_info, $theme_path;
global $original_theme;
$original_theme = $theme;
// Keep resetting the static theme registry cache...
$registry = &drupal_static('theme_get_registry');
_x_drupal_theme_reinitialize($override_theme);
$registry = NULL;
}
/**
* End the global theme override: back to the original theme.
*/
function _x_end_theme_override() {
global $original_theme;
if (!$original_theme) {
return;
}
global $theme, $theme_key, $theme_info, $theme_path;
// Keep resetting the static theme registry cache...
$registry = &drupal_static('theme_get_registry');
_x_drupal_theme_reinitialize($original_theme);
$registry = NULL;
$original_theme = NULL;
}
/**
* Do as little as possible of a theme initialize (no collecting JS/CSS).
*/
function _x_drupal_theme_reinitialize($theme) {
$themes = list_themes();
// Find all our ancestor themes and put them in an array.
$base_theme = array();
$ancestor = $theme;
while ($ancestor && isset($themes[$ancestor]->base_theme)) {
$ancestor = $themes[$ancestor]->base_theme;
$base_theme[] = $themes[$ancestor];
}
$theme = $themes[$theme];
global $theme_info, $base_theme_info, $theme_engine, $theme_path;
$theme_info = $theme;
$base_theme_info = $base_theme;
$theme_path = dirname($theme->filename);
$GLOBALS['theme'] = $GLOBALS['theme_key'] = $theme->name;
$theme_engine = NULL;
// Initialize the theme.
if (isset($theme->engine)) {
// Include the engine.
include_once DRUPAL_ROOT . '/' . $theme->owner;
$theme_engine = $theme->engine;
if (function_exists($theme_engine . '_init')) {
foreach ($base_theme as $base) {
call_user_func($theme_engine . '_init', $base);
}
call_user_func($theme_engine . '_init', $theme);
}
}
else {
// include non-engine theme files
foreach ($base_theme as $base) {
// Include the theme file or the engine.
if (!empty($base->owner)) {
include_once DRUPAL_ROOT . '/' . $base->owner;
}
}
// and our theme gets one too.
if (!empty($theme->owner)) {
include_once DRUPAL_ROOT . '/' . $theme->owner;
}
}
_theme_registry_callback('_theme_load_registry', array($theme, $base_theme, $theme_engine));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment