Commit 673d85fc authored by webchick's avatar webchick

Issue #2143557 by Cottser, webflo, chr.fritsch: Add modules and themes as twig namespaces.

parent b030ad56
......@@ -134,6 +134,8 @@ public static function registerTwig(ContainerBuilder $container) {
'debug' => settings()->get('twig_debug', FALSE),
'auto_reload' => settings()->get('twig_auto_reload', NULL),
))
->addArgument(new Reference('module_handler'))
->addArgument(new Reference('theme_handler'))
->addMethodCall('addExtension', array(new Definition('Drupal\Core\Template\TwigExtension')))
// @todo Figure out what to do about debugging functions.
// @see http://drupal.org/node/1804998
......
......@@ -8,6 +8,8 @@
namespace Drupal\Core\Template;
use Drupal\Component\PhpStorage\PhpStorageFactory;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
/**
* A class that defines a Twig environment for Drupal.
......@@ -32,10 +34,23 @@ class TwigEnvironment extends \Twig_Environment {
* Constructs a TwigEnvironment object and stores cache and storage
* internally.
*/
public function __construct(\Twig_LoaderInterface $loader = NULL, $options = array()) {
public function __construct(\Twig_LoaderInterface $loader = NULL, $options = array(), ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
// @todo Pass as arguments from the DIC.
$this->cache_object = cache();
// Set twig path namespace for themes and modules.
$namespaces = $module_handler->getModuleList();
foreach ($theme_handler->listInfo() as $theme => $info) {
$namespaces[$theme] = $info->filename;
}
foreach ($namespaces as $name => $filename) {
$templatesDirectory = dirname($filename) . '/templates';
if (file_exists($templatesDirectory)) {
$loader->addPath($templatesDirectory, $name);
}
}
$this->templateClasses = array();
parent::__construct($loader, $options);
......
<?php
/**
* @file
* Contains \Drupal\system\Tests\Theme\TwigNamespaceTest.
*/
namespace Drupal\system\Tests\Theme;
use Drupal\simpletest\WebTestBase;
/**
* Tests Twig namespaces.
*/
class TwigNamespaceTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('twig_theme_test', 'twig_namespace_a', 'twig_namespace_b', 'node');
/**
* @var \Drupal\Core\Template\TwigEnvironment
*/
protected $twig;
public static function getInfo() {
return array(
'name' => 'Twig Namespaces',
'description' => 'Tests Twig namespaces.',
'group' => 'Theme',
);
}
function setUp() {
parent::setUp();
theme_enable(array('test_theme', 'bartik'));
$this->twig = \Drupal::service('twig');
}
/**
* Checks to see if a value is a twig template.
*/
public function assertTwigTemplate($value, $message = '', $group = 'Other') {
$this->assertTrue($value instanceof \Twig_Template, $message, $group);
}
/**
* Tests template discovery using namespaces.
*/
public function testTemplateDiscovery() {
// Tests resolving namespaced templates in modules.
$this->assertTwigTemplate($this->twig->resolveTemplate('@node/node.html.twig'), 'Found node.html.twig in node module.');
// Tests resolving namespaced templates in themes.
$this->assertTwigTemplate($this->twig->resolveTemplate('@bartik/page.html.twig'), 'Found page.html.twig in Bartik theme.');
}
/**
* Tests template extension and includes using namespaces.
*/
public function testTwigNamespaces() {
// Test twig @extends and @include in template files.
$test = array('#theme' => 'twig_namespace_test');
$this->drupalSetContent(drupal_render($test));
$this->assertText('This line is from twig_namespace_a/templates/test.html.twig');
$this->assertText('This line is from twig_namespace_b/templates/test.html.twig');
}
}
This line is from twig_namespace_a/templates/test.html.twig
{% block content %}
This text is in a block.
{% endblock %}
name: 'Twig namespace test: Module A'
type: module
description: 'Support module for Twig namespace testing.'
package: Testing
version: VERSION
core: 8.x
hidden: true
name: 'Twig namespace test: Module A'
type: module
description: 'Support module for Twig namespace testing.'
package: Testing
version: VERSION
core: 8.x
hidden: true
{% extends "@twig_namespace_a/test.html.twig" %}
{% block content %}
{% include "@twig_namespace_b/test.html.twig" %}
{% endblock %}
......@@ -11,6 +11,10 @@ function twig_theme_test_theme($existing, $type, $theme, $path) {
'variables' => array(),
'template' => 'twig_theme_test.trans',
);
$items['twig_namespace_test'] = array(
'variables' => array(),
'template' => 'twig_namespace_test',
);
return $items;
}
......
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