Commit 6e95cc60 authored by catch's avatar catch

Issue #1806478 by Cottser, joelpittet, kevinquillen: Make twig the default...

Issue #1806478 by Cottser, joelpittet, kevinquillen: Make twig the default engine once all modules templates are converted from .tpl.php to .html.twig.
parent 0d3c9679
......@@ -5671,7 +5671,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.
......
......@@ -268,11 +268,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)) {
......@@ -513,28 +510,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
......@@ -666,12 +641,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));
......@@ -847,7 +816,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'.
*
......@@ -859,9 +828,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, it overrides the default implementation of the 'page' theme
* hook by containing a 'page.tpl.php' file within its folder structure).
* 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.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,9 +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;
......@@ -1139,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,12 +1144,6 @@ function theme($hook, $variables = array()) {
if (isset($info['path'])) {
$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 +1482,6 @@ function theme_settings_convert_to_config(array $theme_settings, Config $config)
return $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.
*
......
......@@ -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.');
}
}
<?php
/**
* @file
* Contains \Drupal\system\Tests\Theme\ThemeTestPhpTemplate.
*/
namespace Drupal\system\Tests\Theme;
use Drupal\simpletest\WebTestBase;
/**
* Tests theme functions and templates with the PHPTemplate engine.
*/
class ThemeTestPhpTemplate extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('theme_test');
public static function getInfo() {
return array(
'name' => 'PHPTemplate Engine',
'description' => 'Test theme functions with PHPTemplate.',
'group' => 'Theme',
);
}
function setUp() {
parent::setUp();
theme_enable(array('test_theme_phptemplate'));
}
/**
* Ensures a theme's template is overrideable based on the 'template' filename.
*/
function testTemplateOverride() {
config('system.theme')
->set('default', 'test_theme_phptemplate')
->save();
$this->drupalGet('theme-test/template-test');
$this->assertText('Success: Template overridden with PHPTemplate theme.', t('Template overridden by PHPTemplate file.'));
}
}
......@@ -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) {
......
......@@ -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.
......
......@@ -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.
......
......@@ -1614,8 +1614,8 @@ function hook_permission() {
* - 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 file; that extension will be added automatically by the default
* rendering engine (which is PHPTemplate). If 'path', above, is specified,
* the template should also be in this path.
* 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,
* a default function name will be assumed. For example, if a module
......@@ -1636,8 +1636,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
......
......@@ -2993,7 +2993,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',
......
......@@ -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;
}
......
<?php
/**
* @file
* Theme to help test the Twig engine.
*/
/**
* Implements THEME_preprocess_twig_theme_test_php_variables().
*/
function test_theme_preprocess_twig_theme_test_php_variables(&$variables) {
$variables['php_values'] = _test_theme_twig_php_values();
}
/**
* Tests a theme overriding a suggestion of a base theme hook.
*/
......
name: 'Test theme PHPTemplate'
type: theme
description: 'Theme for testing the theme system with the PHPTemplate engine'
core: 8.x
engine: phptemplate
hidden: true
<!-- Output for Theme API test -->
Success: Template overridden.
<?php print 'Success: Template overridden with PHPTemplate theme.'; ?>
name: 'Test theme for Twig'
type: theme
description: 'Theme for testing the theme system with twig engine'
engine: twig
core: 8.x
hidden: true
<?php
/**
* @file
* Theme to help test the Twig engine.
*/
/**
* Implements THEME_preprocess_twig_theme_test_php_variables().
*/
function test_theme_twig_preprocess_twig_theme_test_php_variables(&$variables) {
$variables['php_values'] = _test_theme_twig_php_values();
}
......@@ -39,12 +39,8 @@
* default implementations of theme hooks are named theme_HOOK. Default
* template implementations are stored in the module directory.
*
* Drupal's default template renderer is a simple PHP parsing engine that
* includes the template and stores the output. Drupal's theme engines
* can provide alternate template engines, such as XTemplate, Smarty and
* PHPTal. The most common template engine is PHPTemplate (included with
* Drupal and implemented in phptemplate.engine, which uses Drupal's default
* template renderer.
* Drupal's default template renderer is Twig. Drupal's theme engines can
* provide alternate template engines, such as XTemplate, Smarty and PHPTal.
*
* In order to create theme-specific implementations of these hooks, themes can
* implement their own version of theme hooks, either as functions or templates.
......@@ -53,10 +49,9 @@
* its own version of hook_theme() to tell Drupal what it is implementing;
* themes utilizing an engine will have their well-named theming functions
* automatically registered for them. While this can vary based upon the theme
* engine, the standard set by phptemplate is that theme functions should be
* named THEMENAME_HOOK. For example, for Drupal's default theme (Bartik) to
* implement the 'table' hook, the phptemplate.engine would find
* bartik_table().
* engine, the standard is that theme functions should be named THEMENAME_HOOK.
* For example, for Drupal's default theme (Bartik) to implement the 'table'
* hook, the theme function should be called bartik_table().
*
* The theme system is described and defined in theme.inc.
*
......
......@@ -1778,7 +1778,7 @@ public function buildOptionsForm(&$form, &$form_state) {
// If there's a theme engine involved, we also need to know its extension
// so we can give the proper filename.
$this->theme_extension = '.tpl.php';
$this->theme_extension = '.html.twig';
if (isset($theme_engine)) {
$extension_function = $theme_engine . '_extension';
if (function_exists($extension_function)) {
......
......@@ -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();
}
......@@ -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;
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment