From 07cd3c992253e9d8d7d1819b5fbcb7c35c650f66 Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Tue, 4 Mar 2014 12:18:08 +0000
Subject: [PATCH] Issue #2188721 by Cottser, aspilicious: Theme suggestions
 don't load necessary include files.

---
 core/includes/theme.inc                        | 15 ++++++++-------
 .../Tests/Theme/ThemeSuggestionsAlterTest.php  | 18 ++++++++++++++++++
 .../theme_suggestions_test.inc                 | 13 +++++++++++++
 .../theme_suggestions_test.module              | 18 ++++++++++++++++++
 .../Drupal/theme_test/ThemeTestController.php  |  8 ++++++++
 .../tests/modules/theme_test/theme_test.module | 10 ++++++++++
 .../modules/theme_test/theme_test.routing.yml  |  7 +++++++
 7 files changed, 82 insertions(+), 7 deletions(-)
 create mode 100644 core/modules/system/tests/modules/theme_suggestions_test/theme_suggestions_test.inc

diff --git a/core/includes/theme.inc b/core/includes/theme.inc
index 7ad60e5c67ef..c152f721455e 100644
--- a/core/includes/theme.inc
+++ b/core/includes/theme.inc
@@ -569,13 +569,6 @@ function _theme($hook, $variables = array()) {
   // point path_to_theme() to the currently used theme path:
   $theme_path = $info['theme path'];
 
-  // Include a file if the theme function or variable preprocessor is held
-  // elsewhere.
-  if (!empty($info['includes'])) {
-    foreach ($info['includes'] as $include_file) {
-      include_once DRUPAL_ROOT . '/' . $include_file;
-    }
-  }
 
   // If a renderable array is passed as $variables, then set $variables to
   // the arguments expected by the theme function.
@@ -648,6 +641,14 @@ function _theme($hook, $variables = array()) {
     }
   }
 
+  // Include a file if the theme function or variable preprocessor is held
+  // elsewhere.
+  if (!empty($info['includes'])) {
+    foreach ($info['includes'] as $include_file) {
+      include_once DRUPAL_ROOT . '/' . $include_file;
+    }
+  }
+
   // Invoke the variable preprocessors, if any.
   if (isset($info['base hook'])) {
     $base_hook = $info['base hook'];
diff --git a/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeSuggestionsAlterTest.php b/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeSuggestionsAlterTest.php
index 390687da907f..a987e1f61e62 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeSuggestionsAlterTest.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeSuggestionsAlterTest.php
@@ -138,6 +138,24 @@ function testThemeFunctionSuggestionsAlter() {
     $this->assertText('Theme function overridden based on new theme suggestion provided by a module.');
   }
 
+  /**
+   * Tests that theme suggestion alter hooks work with theme hook includes.
+   */
+  public function testSuggestionsAlterInclude() {
+    // Check the original theme output.
+    $this->drupalGet('theme-test/suggestion-alter-include');
+    $this->assertText('Original function before altering theme suggestions.');
+
+    // Enable theme_suggestions_test module and make two requests to make sure
+    // the include file is always loaded. The file will always be included for
+    // the first request because the theme registry is being rebuilt.
+    \Drupal::moduleHandler()->install(array('theme_suggestions_test'));
+    $this->drupalGet('theme-test/suggestion-alter-include');
+    $this->assertText('Function suggested via suggestion alter hook found in include file.', 'Include file loaded for initial request.');
+    $this->drupalGet('theme-test/suggestion-alter-include');
+    $this->assertText('Function suggested via suggestion alter hook found in include file.', 'Include file loaded for second request.');
+  }
+
   /**
    * Tests execution order of theme suggestion alter hooks.
    *
diff --git a/core/modules/system/tests/modules/theme_suggestions_test/theme_suggestions_test.inc b/core/modules/system/tests/modules/theme_suggestions_test/theme_suggestions_test.inc
new file mode 100644
index 000000000000..efc0144551a1
--- /dev/null
+++ b/core/modules/system/tests/modules/theme_suggestions_test/theme_suggestions_test.inc
@@ -0,0 +1,13 @@
+<?php
+
+/**
+ * @file
+ * Include file for testing theme suggestion hooks.
+ */
+
+/**
+ * Returns HTML for a theme function include test.
+ */
+function theme_theme_suggestions_test_include($variables) {
+  return 'Function suggested via suggestion alter hook found in include file.';
+}
diff --git a/core/modules/system/tests/modules/theme_suggestions_test/theme_suggestions_test.module b/core/modules/system/tests/modules/theme_suggestions_test/theme_suggestions_test.module
index 1b19fee5edcb..e963c2fd6510 100644
--- a/core/modules/system/tests/modules/theme_suggestions_test/theme_suggestions_test.module
+++ b/core/modules/system/tests/modules/theme_suggestions_test/theme_suggestions_test.module
@@ -5,6 +5,17 @@
  * Support module for testing theme suggestions.
  */
 
+/**
+ * Implements hook_theme().
+ */
+function theme_suggestions_test_theme() {
+  $items['theme_suggestions_test_include'] = array(
+    'file' => 'theme_suggestions_test.inc',
+    'variables' => array(),
+  );
+  return $items;
+}
+
 /**
  * Implements hook_theme_suggestions_alter().
  */
@@ -36,3 +47,10 @@ function theme_suggestions_test_theme_suggestions_theme_test_function_suggestion
 function theme_suggestions_test_theme_suggestions_theme_test_specific_suggestions_alter(array &$suggestions, array $variables) {
   $suggestions[] = 'theme_test_specific_suggestions__' . 'variant__foo';
 }
+
+/**
+ * Implements hook_theme_suggestions_HOOK_alter().
+ */
+function theme_suggestions_test_theme_suggestions_theme_test_suggestions_include_alter(array &$suggestions, array $variables, $hook) {
+  $suggestions[] = 'theme_suggestions_test_include';
+}
diff --git a/core/modules/system/tests/modules/theme_test/lib/Drupal/theme_test/ThemeTestController.php b/core/modules/system/tests/modules/theme_test/lib/Drupal/theme_test/ThemeTestController.php
index 3684793637ab..31b25a0b387e 100644
--- a/core/modules/system/tests/modules/theme_test/lib/Drupal/theme_test/ThemeTestController.php
+++ b/core/modules/system/tests/modules/theme_test/lib/Drupal/theme_test/ThemeTestController.php
@@ -127,4 +127,12 @@ function functionSuggestionAlter() {
     return array('#theme' => 'theme_test_function_suggestions');
   }
 
+
+  /**
+   * Menu callback for testing includes with suggestion alter hooks.
+   */
+  function suggestionAlterInclude() {
+    return array('#theme' => 'theme_test_suggestions_include');
+  }
+
 }
diff --git a/core/modules/system/tests/modules/theme_test/theme_test.module b/core/modules/system/tests/modules/theme_test/theme_test.module
index 1c6fceb88826..b6f13c81ffc9 100644
--- a/core/modules/system/tests/modules/theme_test/theme_test.module
+++ b/core/modules/system/tests/modules/theme_test/theme_test.module
@@ -33,6 +33,9 @@ function theme_test_theme($existing, $type, $theme, $path) {
   $items['theme_test_function_suggestions'] = array(
     'variables' => array(),
   );
+  $items['theme_test_suggestions_include'] = array(
+    'variables' => array(),
+  );
   $items['theme_test_foo'] = array(
     'variables' => array('foo' => NULL),
   );
@@ -138,3 +141,10 @@ function theme_test_theme_suggestions_alter(array &$suggestions, array $variable
 function theme_test_theme_suggestions_theme_test_suggestions_alter(array &$suggestions, array $variables) {
   drupal_set_message(__FUNCTION__ . '() executed.');
 }
+
+/**
+ * Returns HTML for a theme function include test.
+ */
+function theme_theme_test_suggestions_include($variables) {
+  return 'Original function before altering theme suggestions.';
+}
diff --git a/core/modules/system/tests/modules/theme_test/theme_test.routing.yml b/core/modules/system/tests/modules/theme_test/theme_test.routing.yml
index 19f2b8c8fa47..73ed6b8cb1be 100644
--- a/core/modules/system/tests/modules/theme_test/theme_test.routing.yml
+++ b/core/modules/system/tests/modules/theme_test/theme_test.routing.yml
@@ -92,3 +92,10 @@ function_suggestion_alter:
     _content: '\Drupal\theme_test\ThemeTestController::functionSuggestionAlter'
   requirements:
     _access: 'TRUE'
+
+suggestion_alter_include:
+  path: '/theme-test/suggestion-alter-include'
+  defaults:
+    _content: '\Drupal\theme_test\ThemeTestController::suggestionAlterInclude'
+  requirements:
+    _access: 'TRUE'
-- 
GitLab