diff --git a/core/includes/common.inc b/core/includes/common.inc index 8b9d9e3..527c2b2 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -5915,7 +5915,7 @@ function element_set_attributes(array &$element, array $map) { * - name: The real name of the theme for display purposes. * - description: Brief description. * - screenshot: Path to screenshot relative to the theme's .info.yml file. - * - engine: Theme engine; typically phptemplate. + * - engine: Theme engine; typically twig. * - base theme: Name of a base theme, if applicable. * - regions: Listed regions. * - features: Features available. diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 782b562..f129666 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -267,11 +267,8 @@ function _drupal_theme_initialize($theme, $base_theme = array(), $registry_callb include_once DRUPAL_ROOT . '/' . $theme->owner; } } - // Load twig as secondary always available engine. - // @todo Make twig the default engine and remove this. This is required - // because (by design) the theme system doesn't allow modules to register more - // than one type of extension. We need a temporary backwards compatibility - // layer to allow us to perform core-wide .tpl.php to .html.twig conversion. + + // Always include Twig as the default theme engine. include_once DRUPAL_ROOT . '/core/themes/engines/twig/twig.engine'; if (isset($registry_callback)) { @@ -512,28 +509,6 @@ function _theme_process_registry(&$cache, $name, $type, $theme, $path) { if (!isset($info['path'])) { $result[$hook]['template'] = $path . '/templates/' . $info['template']; } - if ($type == 'module' || $type == 'theme_engine') { - // Add two render engines for modules and theme engines. - // @todo Remove and make twig the default engine. - $render_engines = array( - '.html.twig' => 'twig', - '.tpl.php' => 'phptemplate' - ); - - // Find the best engine for this template. - foreach ($render_engines as $extension => $engine) { - // Render the output using the template file. - $template_file = $result[$hook]['template'] . $extension; - if (isset($info['path'])) { - $template_file = $info['path'] . '/' . $template_file; - } - if (file_exists($template_file)) { - $result[$hook]['template_file'] = $template_file; - $result[$hook]['engine'] = $engine; - break; - } - } - } } // Allow variable processors for all theming hooks, whether the hook is @@ -665,12 +640,6 @@ function _theme_build_registry($theme, $base_theme, $theme_engine) { _theme_process_registry($cache, $theme_engine, 'theme_engine', $theme->name, dirname($theme->filename)); } - if ($theme_engine == 'phptemplate') { - // Check for Twig templates if this is a PHPTemplate theme. - // @todo Remove this once all core themes are converted to Twig. - _theme_process_registry($cache, 'twig', 'theme_engine', $theme->name, dirname($theme->filename)); - } - // Finally, hooks provided by the theme itself. _theme_process_registry($cache, $theme->name, 'theme', $theme->name, dirname($theme->filename)); @@ -846,7 +815,7 @@ function drupal_find_base_themes($themes, $key, $used_keys = array()) { * theme_HOOK() (e.g., theme_taxonomy_term()) or via a template file named * according to the value of the 'template' key registered with the theme hook * (see hook_theme() for details). Default templates are implemented with the - * PHPTemplate rendering engine and are named the same as the theme hook, with + * Twig rendering engine and are named the same as the theme hook, with * underscores changed to hyphens, so for the 'taxonomy_term' theme hook, the * default template is 'taxonomy-term.tpl.php'. * @@ -858,9 +827,9 @@ function drupal_find_base_themes($themes, $key, $used_keys = array()) { * default implementation of the 'menu_tree' theme hook by implementing a * bartik_menu_tree() function), or by adding a template file within its folder * structure that follows the template naming structure used by the theme's - * rendering engine (for example, since the Bartik theme uses the PHPTemplate + * rendering engine (for example, since the Bartik theme uses the Twig * rendering engine, it overrides the default implementation of the 'page' theme - * hook by containing a 'page.tpl.php' file within its folder structure). + * hook by containing a 'page.html.twig' file within its folder structure). * * If the implementation is a template file, several functions are called * before the template file is invoked, to modify the $variables array. These @@ -1123,8 +1092,8 @@ function theme($hook, $variables = array()) { } else { // Default render function and extension. - $render_function = 'theme_render_template'; - $extension = '.tpl.php'; + $render_function = 'twig_render_template'; + $extension = '.html.twig'; // The theme engine may use a different extension and a different renderer. global $theme_engine; @@ -1138,13 +1107,6 @@ function theme($hook, $variables = array()) { $extension = $extension_function(); } } - // @todo Change this 'if' back to 'elseif' once Twig conversion is - // complete. Normally only modules have this key set. - if (isset($info['engine'])) { - if (function_exists($info['engine'] . '_render_template')) { - $render_function = $info['engine'] . '_render_template'; - } - } } // In some cases, a template implementation may not have had @@ -1183,11 +1145,6 @@ function theme($hook, $variables = array()) { $template_file = $info['path'] . '/' . $template_file; } - // Modules can override this. - if (isset($info['template_file'])) { - $template_file = $info['template_file']; - } - $output = $render_function($template_file, $variables); } @@ -1527,31 +1484,6 @@ function theme_settings_convert_to_config(array $theme_settings, Config $config) } /** - * Renders a system default template, which is essentially a PHP template. - * - * @param $template_file - * The filename of the template to render. - * @param $variables - * A keyed array of variables that will appear in the output. - * - * @return - * The output generated by the template. - */ -function theme_render_template($template_file, $variables) { - // Extract the variables to a local namespace - extract($variables, EXTR_SKIP); - - // Start output buffering - ob_start(); - - // Include the template file - include DRUPAL_ROOT . '/' . $template_file; - - // End buffering and return its contents - return ob_get_clean(); -} - -/** * Enables a given list of themes. * * @param $theme_list diff --git a/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeTest.php b/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeTest.php index 664a73b..a773922 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeTest.php @@ -154,10 +154,10 @@ function testListThemes() { $this->assertIdentical($themes['test_basetheme']->sub_themes, $sub_theme_list, 'Base theme\'s object includes list of subthemes.'); $this->assertIdentical($themes['test_subtheme']->base_themes, $base_theme_list, 'Subtheme\'s object includes list of base themes.'); // Check for theme engine in subtheme. - $this->assertIdentical($themes['test_subtheme']->engine, 'phptemplate', 'Subtheme\'s object includes the theme engine.'); + $this->assertIdentical($themes['test_subtheme']->engine, 'twig', 'Subtheme\'s object includes the theme engine.'); // Check for theme engine prefix. - $this->assertIdentical($themes['test_basetheme']->prefix, 'phptemplate', 'Base theme\'s object includes the theme engine prefix.'); - $this->assertIdentical($themes['test_subtheme']->prefix, 'phptemplate', 'Subtheme\'s object includes the theme engine prefix.'); + $this->assertIdentical($themes['test_basetheme']->prefix, 'twig', 'Base theme\'s object includes the theme engine prefix.'); + $this->assertIdentical($themes['test_subtheme']->prefix, 'twig', 'Subtheme\'s object includes the theme engine prefix.'); } /** @@ -232,7 +232,7 @@ public function testFindThemeTemplates() { _theme_process_registry($cache, $module, 'module', $module, drupal_get_path('module', $module)); } - $templates = drupal_find_theme_templates($cache, '.tpl.php', drupal_get_path('theme', 'test_theme')); - $this->assertEqual($templates['node__1']['template'], 'node--1', 'Template node--1.tpl.php was found in test_theme.'); + $templates = drupal_find_theme_templates($cache, '.html.twig', drupal_get_path('theme', 'test_theme')); + $this->assertEqual($templates['node__1']['template'], 'node--1', 'Template node--1.html.twig was found in test_theme.'); } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeTestTwig.php b/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeTestTwig.php index 461b5cb..7860511 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeTestTwig.php +++ b/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeTestTwig.php @@ -24,50 +24,14 @@ class ThemeTestTwig extends WebTestBase { public static function getInfo() { return array( 'name' => 'Twig Engine', - 'description' => 'Test theme functions with twig.', + 'description' => 'Test Twig-specific theme functionality.', 'group' => 'Theme', ); } function setUp() { parent::setUp(); - theme_enable(array('test_theme_twig')); - } - - /** - * Ensures a themes template is overrideable based on the 'template' filename. - */ - function testTemplateOverride() { - config('system.theme') - ->set('default', 'test_theme_twig') - ->save(); - $this->drupalGet('theme-test/template-test'); - $this->assertText('Success: Template overridden.', t('Template overridden by defined \'template\' filename.')); - } - - /** - * Tests drupal_find_theme_templates - */ - function testFindThemeTemplates() { - - $cache = array(); - - // Prime the theme cache - foreach (module_implements('theme') as $module) { - _theme_process_registry($cache, $module, 'module', $module, drupal_get_path('module', $module)); - } - - // Check for correct content - // @todo Remove this tests once double engine code is removed - - $this->assertEqual($cache['node']['template_file'], 'core/modules/node/templates/node.html.twig', 'Node is using node.html.twig as template file'); - $this->assertEqual($cache['node']['engine'], 'twig', 'Node is using twig engine'); - - $this->assertEqual($cache['theme_test_template_test']['template_file'], 'core/modules/system/tests/modules/theme_test/templates/theme_test.template_test.html.twig', 'theme_test is using theme_test.template_test.html.twig as template file'); - $this->assertEqual($cache['theme_test_template_test']['engine'], 'twig', 'theme_test is using twig as engine.'); - - $templates = drupal_find_theme_templates($cache, '.html.twig', drupal_get_path('theme', 'test_theme_twig')); - $this->assertEqual($templates['node__1']['template'], 'node--1', 'Template node--1.html.twig was found in test_theme_twig.'); + theme_enable(array('test_theme')); } /** @@ -75,7 +39,7 @@ function testFindThemeTemplates() { */ function testTwigVariableDataTypes() { config('system.theme') - ->set('default', 'test_theme_twig') + ->set('default', 'test_theme') ->save(); $this->drupalGet('twig-theme-test/php-variables'); foreach (_test_theme_twig_php_values() as $type => $value) { diff --git a/core/modules/system/lib/Drupal/system/Tests/Theme/TwigDebugMarkupTest.php b/core/modules/system/lib/Drupal/system/Tests/Theme/TwigDebugMarkupTest.php index 0bcd1a6..38e183b 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Theme/TwigDebugMarkupTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Theme/TwigDebugMarkupTest.php @@ -34,8 +34,8 @@ public static function getInfo() { */ function testTwigDebugMarkup() { $extension = twig_extension(); - theme_enable(array('test_theme_twig')); - config('system.theme')->set('default', 'test_theme_twig')->save(); + theme_enable(array('test_theme')); + config('system.theme')->set('default', 'test_theme')->save(); // Enable debug, rebuild the service container, and clear all caches. $this->settingsSet('twig_debug', TRUE); $this->rebuildContainer(); @@ -47,7 +47,7 @@ function testTwigDebugMarkup() { _theme_process_registry($cache, $module, 'module', $module, drupal_get_path('module', $module)); } // Create array of Twig templates. - $templates = drupal_find_theme_templates($cache, $extension, drupal_get_path('theme', 'test_theme_twig')); + $templates = drupal_find_theme_templates($cache, $extension, drupal_get_path('theme', 'test_theme')); $templates += drupal_find_theme_templates($cache, $extension, drupal_get_path('module', 'node')); // Create a node and test different features of the debug markup. diff --git a/core/modules/system/lib/Drupal/system/Tests/Theme/TwigSettingsTest.php b/core/modules/system/lib/Drupal/system/Tests/Theme/TwigSettingsTest.php index 2e84924..f50f110 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Theme/TwigSettingsTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Theme/TwigSettingsTest.php @@ -77,9 +77,9 @@ function testTwigDebugOverride() { */ function testTwigCacheOverride() { $extension = twig_extension(); - theme_enable(array('test_theme_twig')); + theme_enable(array('test_theme')); config('system.theme') - ->set('default', 'test_theme_twig') + ->set('default', 'test_theme') ->save(); $cache = array(); @@ -89,7 +89,7 @@ function testTwigCacheOverride() { } // Load array of Twig templates. - $templates = drupal_find_theme_templates($cache, $extension, drupal_get_path('theme', 'test_theme_twig')); + $templates = drupal_find_theme_templates($cache, $extension, drupal_get_path('theme', 'test_theme')); // Get the template filename and the cache filename for // theme_test.template_test.html.twig. diff --git a/core/modules/system/system.api.php b/core/modules/system/system.api.php index aa79df9..6d3d61e 100644 --- a/core/modules/system/system.api.php +++ b/core/modules/system/system.api.php @@ -1587,9 +1587,9 @@ function hook_permission() { * path, include it here. This path should be relative to the Drupal root * directory. * - template: If specified, this theme implementation is a template, and - * this is the template file without an extension. Do not put .tpl.php on + * this is the template file without an extension. Do not put .html.twig on * this file; that extension will be added automatically by the default - * rendering engine (which is PHPTemplate). If 'path', above, is specified, + * rendering engine (which is Twig). If 'path', above, is specified, * the template should also be in this path. * - function: If specified, this will be the function name to invoke for * this implementation. If neither 'template' nor 'function' is specified, @@ -1611,8 +1611,8 @@ function hook_permission() { * - preprocess functions: A list of functions used to preprocess this data. * Ordinarily this won't be used; it's automatically filled in. By default, * for a module this will be filled in as template_preprocess_HOOK. For - * a theme this will be filled in as phptemplate_preprocess and - * phptemplate_preprocess_HOOK as well as themename_preprocess and + * a theme this will be filled in as twig_preprocess and + * twig_preprocess_HOOK as well as themename_preprocess and * themename_preprocess_HOOK. * - override preprocess functions: Set to TRUE when a theme does NOT want * the standard preprocess functions to run. This can be used to give a diff --git a/core/modules/system/system.module b/core/modules/system/system.module index ce1b39b..a746fc7 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -3007,7 +3007,7 @@ function _system_rebuild_theme_data() { // Set defaults for theme info. $defaults = array( - 'engine' => 'phptemplate', + 'engine' => 'twig', 'regions' => array( 'sidebar_first' => 'Left sidebar', 'sidebar_second' => 'Right sidebar', 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 d610a19..4204077 100644 --- a/core/modules/system/tests/modules/theme_test/theme_test.module +++ b/core/modules/system/tests/modules/theme_test/theme_test.module @@ -30,7 +30,7 @@ function theme_test_system_theme_info() { $themes['test_theme'] = drupal_get_path('module', 'system') . '/tests/themes/test_theme/test_theme.info.yml'; $themes['test_basetheme'] = drupal_get_path('module', 'system') . '/tests/themes/test_basetheme/test_basetheme.info.yml'; $themes['test_subtheme'] = drupal_get_path('module', 'system') . '/tests/themes/test_subtheme/test_subtheme.info.yml'; - $themes['test_theme_twig'] = drupal_get_path('module', 'system') . '/tests/themes/test_theme_twig/test_theme_twig.info.yml'; + $themes['test_theme_phptemplate'] = drupal_get_path('module', 'system') . '/tests/themes/test_theme_phptemplate/test_theme_phptemplate.info.yml'; return $themes; } diff --git a/core/modules/system/tests/themes/test_theme/node--1.tpl.php b/core/modules/system/tests/themes/test_theme/node--1.tpl.php deleted file mode 100644 index 81974ce..0000000 --- a/core/modules/system/tests/themes/test_theme/node--1.tpl.php +++ /dev/null @@ -1,4 +0,0 @@ - -Node Content Dummy diff --git a/core/modules/system/tests/themes/test_theme/test_theme.theme b/core/modules/system/tests/themes/test_theme/test_theme.theme index 8275818..62b5abf 100644 --- a/core/modules/system/tests/themes/test_theme/test_theme.theme +++ b/core/modules/system/tests/themes/test_theme/test_theme.theme @@ -1,6 +1,18 @@ -Success: Template overridden. diff --git a/core/modules/system/tests/themes/test_theme_twig/node--1.html.twig b/core/modules/system/tests/themes/test_theme_twig/node--1.html.twig deleted file mode 100644 index 046c02e..0000000 --- a/core/modules/system/tests/themes/test_theme_twig/node--1.html.twig +++ /dev/null @@ -1,4 +0,0 @@ -{# - // node--1.html.twig - Dummy file for finding the template -#} -Node Content Dummy diff --git a/core/modules/system/tests/themes/test_theme_twig/template.php b/core/modules/system/tests/themes/test_theme_twig/template.php deleted file mode 100644 index a4abe2d..0000000 --- a/core/modules/system/tests/themes/test_theme_twig/template.php +++ /dev/null @@ -1,2 +0,0 @@ -theme_extension = '.tpl.php'; + $this->theme_extension = '.html.twig'; if (isset($theme_engine)) { $extension_function = $theme_engine . '_extension'; if (function_exists($extension_function)) { diff --git a/core/themes/engines/phptemplate/phptemplate.engine b/core/themes/engines/phptemplate/phptemplate.engine index 88c09c7..929d83d 100644 --- a/core/themes/engines/phptemplate/phptemplate.engine +++ b/core/themes/engines/phptemplate/phptemplate.engine @@ -23,3 +23,35 @@ function phptemplate_theme($existing, $type, $theme, $path) { $templates += drupal_find_theme_templates($existing, '.tpl.php', $path); return $templates; } + +/** + * Implements hook_extension(). + */ +function phptemplate_extension() { + return '.tpl.php'; +} + +/** + * Renders a system default template, which is essentially a PHP template. + * + * @param $template_file + * The filename of the template to render. + * @param $variables + * A keyed array of variables that will appear in the output. + * + * @return + * The output generated by the template. + */ +function phptemplate_render_template($template_file, $variables) { + // Extract the variables to a local namespace + extract($variables, EXTR_SKIP); + + // Start output buffering + ob_start(); + + // Include the template file + include DRUPAL_ROOT . '/' . $template_file; + + // End buffering and return its contents + return ob_get_clean(); +} \ No newline at end of file diff --git a/core/themes/engines/twig/twig.engine b/core/themes/engines/twig/twig.engine index 1ba60a5..b541cdc 100644 --- a/core/themes/engines/twig/twig.engine +++ b/core/themes/engines/twig/twig.engine @@ -10,7 +10,9 @@ * Implements hook_theme(). */ function twig_theme($existing, $type, $theme, $path) { - return drupal_find_theme_templates($existing, '.html.twig', $path); + $templates = drupal_find_theme_functions($existing, array($theme)); + $templates += drupal_find_theme_templates($existing, '.html.twig', $path); + return $templates; } /**