From 4bee9c0ae34d810a82f121192fb9d1ddc39d7985 Mon Sep 17 00:00:00 2001 From: Stuart Clark Date: Tue, 25 Oct 2011 13:50:59 +1100 Subject: [PATCH] Issue #342350: Fixed issue where template files not overriden by filename. --- includes/theme.inc | 22 +++++++++++++++--- modules/simpletest/tests/theme.test | 9 +++++++ modules/simpletest/tests/theme_test.module | 23 ++++++++++++++++++++ .../tests/theme_test.template_test.tpl.php | 2 + .../test_theme/theme_test.template_test.tpl.php | 2 + 5 files changed, 54 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 7fad5f3..d8b3bfc 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,20 @@ function drupal_find_theme_templates($cache, $extension, $path) { 'path' => dirname($file->uri), ); } + + // Match templates by designated template file name instead of restricting + // to templates named after the theme funciton hook alone. + 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 7c68989..0cecec0 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 160d192..e95f622 100644 --- a/modules/simpletest/tests/theme_test.module +++ b/modules/simpletest/tests/theme_test.module @@ -1,6 +1,17 @@ 'theme_test.template_test', + ); + + return $items; +} + +/** * Implements hook_menu(). */ function theme_test_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; } @@ -70,6 +86,13 @@ function theme_test_hook_init_page_callback() { } /** + * 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. */ function _theme_custom_theme() { 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 0000000..cde8faa --- /dev/null +++ b/modules/simpletest/tests/theme_test.template_test.tpl.php @@ -0,0 +1,2 @@ + +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 0000000..d4409bf --- /dev/null +++ b/themes/tests/test_theme/theme_test.template_test.tpl.php @@ -0,0 +1,2 @@ + +Success: Template overridden. -- 1.7.4.4