From 746625036f4be88e83af06ac286a5ba6d02a2ef6 Mon Sep 17 00:00:00 2001
From: Jonathan Jordan <jonathan@metaltoad.com>
Date: Mon, 9 Jun 2014 10:59:29 -0700
Subject: [PATCH] Issue #943616 by mkalkbrenner: Fixed hook_custom_theme() does
 not work as expected.

---
 includes/common.inc | 4 ++--
 includes/menu.inc   | 8 ++++++++
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/includes/common.inc b/includes/common.inc
index e1a1673..ce40ceb 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -2429,7 +2429,7 @@ function drupal_attributes(array $attributes = array()) {
  * @see url()
  */
 function l($text, $path, array $options = array()) {
-  global $language_url;
+  global $language_url, $theme_initialize;
   static $use_theme = NULL;
 
   // Merge in defaults.
@@ -2460,7 +2460,7 @@ function l($text, $path, array $options = array()) {
   if (!isset($use_theme) && function_exists('theme')) {
     // Allow edge cases to prevent theme initialization and force inline link
     // rendering.
-    if (variable_get('theme_link', TRUE)) {
+    if (variable_get('theme_link', TRUE) && $theme_initialize) {
       drupal_theme_initialize();
       $registry = theme_get_registry(FALSE);
       // We don't want to duplicate functionality that's in theme(), so any
diff --git a/includes/menu.inc b/includes/menu.inc
index fa5a71e..14bef00 100644
--- a/includes/menu.inc
+++ b/includes/menu.inc
@@ -1767,7 +1767,15 @@ function menu_get_custom_theme($initialize = FALSE) {
  * Sets a custom theme for the current page, if there is one.
  */
 function menu_set_custom_theme() {
+  global $theme_initialize;
+  // Set a global variable to let other functions know that we are not ready to
+  // initialize the theme yet. Initialization should wait until after we have
+  // set the custom theme, otherwise calling drupal_theme_initialize() will call
+  // menu_get_custom_theme() will return NULL and use the default theme.
+  // See http://drupal.org/node/943616 for more info.
+  $theme_initialize = FALSE;
   menu_get_custom_theme(TRUE);
+  $theme_initialize = TRUE;
 }
 
 /**
-- 
1.8.5.2 (Apple Git-48)

