From d0fb1ad515d93a96473777e4802055ea67e0bf74 Mon Sep 17 00:00:00 2001
From: Nathaniel <catch@35733.no-reply.drupal.org>
Date: Thu, 27 Oct 2011 12:52:26 +0900
Subject: [PATCH] Issue #342350 by Deciphered: Fixed Override template files
 based on template filename as well as hook name.

---
 includes/theme.inc                            | 21 +++++++++++++----
 modules/simpletest/tests/theme.test           |  9 ++++++++
 modules/simpletest/tests/theme_test.module    | 23 +++++++++++++++++++
 .../tests/theme_test.template_test.tpl.php    |  2 ++
 .../theme_test.template_test.tpl.php          |  2 ++
 5 files changed, 53 insertions(+), 4 deletions(-)
 create mode 100644 modules/simpletest/tests/theme_test.template_test.tpl.php
 create mode 100644 themes/tests/test_theme/theme_test.template_test.tpl.php

diff --git a/includes/theme.inc b/includes/theme.inc
index 7fad5f388dcb..21543426b0cc 100644
--- a/includes/theme.inc
+++ b/includes/theme.inc
@@ -1199,10 +1199,10 @@ function drupal_find_theme_templates($cache, $extension, $path) {
     if (strpos($file->uri, str_replace($subtheme_paths, '', $file->uri)) !== 0) {
       continue;
     }
-    // Chop off the remaining extensions if there are any. $template already
-    // has the rightmost extension removed, but there might still be more,
-    // such as with .tpl.php, which still has .tpl in $template at this point.
-    if (($pos = strpos($template, '.')) !== FALSE) {
+    // Chop off the remaining '.tpl' extension. $template already has the
+    // rightmost extension removed, but there might still be more, such as with
+    // .tpl.php, which still has .tpl in $template at this point.
+    if (($pos = strpos($template, '.tpl')) !== FALSE) {
       $template = substr($template, 0, $pos);
     }
     // Transform - in filenames to _ to match function naming scheme
@@ -1214,6 +1214,19 @@ function drupal_find_theme_templates($cache, $extension, $path) {
         'path' => dirname($file->uri),
       );
     }
+
+    // Match templates based on the 'template' filename.
+    foreach ($cache as $hook => $info) {
+      if (isset($info['template'])) {
+        $template_candidates = array($info['template'], str_replace($info['theme path'] . '/', '', $info['template']));
+        if (in_array($template, $template_candidates)) {
+          $implementations[$hook] = array(
+            'template' => $template,
+            'path' => dirname($file->uri),
+          );
+        }
+      }
+    }
   }
 
   // Find templates that implement possible "suggestion" variants of registered
diff --git a/modules/simpletest/tests/theme.test b/modules/simpletest/tests/theme.test
index 7c6898965994..0cecec0d2859 100644
--- a/modules/simpletest/tests/theme.test
+++ b/modules/simpletest/tests/theme.test
@@ -101,6 +101,15 @@ class ThemeUnitTest extends DrupalWebTestCase {
     $this->drupalGet('theme-test/suggestion');
     variable_set('preprocess_css', 0);
   }
+
+  /**
+   * Ensures a themes template is overrideable based on the 'template' filename.
+   */
+  function testTemplateOverride() {
+    variable_set('theme_default', 'test_theme');
+    $this->drupalGet('theme-test/template-test');
+    $this->assertText('Success: Template overridden.', t('Template overridden by defined \'template\' filename.'));
+  }
 }
 
 /**
diff --git a/modules/simpletest/tests/theme_test.module b/modules/simpletest/tests/theme_test.module
index 160d192dd82a..e95f62214aed 100644
--- a/modules/simpletest/tests/theme_test.module
+++ b/modules/simpletest/tests/theme_test.module
@@ -1,5 +1,16 @@
 <?php
 
+/**
+ * Implements hook_theme().
+ */
+function theme_test_theme($existing, $type, $theme, $path) {
+  $items['theme_test_template_test'] = array(
+    'template' => 'theme_test.template_test',
+  );
+
+  return $items;
+}
+
 /**
  * Implements hook_menu().
  */
@@ -23,6 +34,11 @@ function theme_test_menu() {
     'access callback' => TRUE,
     'type' => MENU_CALLBACK,
   );
+  $items['theme-test/template-test'] = array(
+    'page callback' => 'theme_test_template_test_page_callback',
+    'access callback' => TRUE,
+    'type' => MENU_CALLBACK,
+  );
   return $items;
 }
 
@@ -69,6 +85,13 @@ function theme_test_hook_init_page_callback() {
   return $GLOBALS['theme_test_output'];
 }
 
+/**
+ * Menu callback for testing template overridding based on filename.
+ */
+function theme_test_template_test_page_callback() {
+  return theme('theme_test_template_test');
+}
+
 /**
  * Custom theme callback.
  */
diff --git a/modules/simpletest/tests/theme_test.template_test.tpl.php b/modules/simpletest/tests/theme_test.template_test.tpl.php
new file mode 100644
index 000000000000..cde8faadd3c3
--- /dev/null
+++ b/modules/simpletest/tests/theme_test.template_test.tpl.php
@@ -0,0 +1,2 @@
+<!-- Output for Theme API test -->
+Fail: Template not overridden.
diff --git a/themes/tests/test_theme/theme_test.template_test.tpl.php b/themes/tests/test_theme/theme_test.template_test.tpl.php
new file mode 100644
index 000000000000..d4409bf16e0a
--- /dev/null
+++ b/themes/tests/test_theme/theme_test.template_test.tpl.php
@@ -0,0 +1,2 @@
+<!-- Output for Theme API test -->
+Success: Template overridden.
-- 
GitLab