Commit 3a699a6a authored by alexpott's avatar alexpott

Issue #1503464 by plopesc, larowlan, joelpittet, svenaas: Automatically add...

Issue #1503464 by plopesc, larowlan, joelpittet, svenaas: Automatically add theme hook suggestion for node view mode.
parent a167eca3
<?php
/**
* @file
* Contains of \Drupal\node\Tests\NodeTemplateSuggestionsTest.
*/
namespace Drupal\node\Tests;
/**
* Checks node template suggestions.
*/
class NodeTemplateSuggestionsTest extends NodeTestBase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'Node template suggestions',
'description' => 'Tests the node template suggestions.',
'group' => 'Node',
);
}
/**
* Tests if template_preprocess_node() generates the correct suggestions.
*/
function testNodeThemeHookSuggestions() {
// Create node to be rendered.
$node = $this->drupalCreateNode();
$view_mode = 'full';
// Simulate theming of the node.
$build = \Drupal::entityManager()->getViewBuilder('node')->view($node, $view_mode);
$variables['elements'] = $build;
$suggestions = \Drupal::moduleHandler()->invokeAll('theme_suggestions_node', array($variables));
$this->assertEqual($suggestions, array('node__full', 'node__page', 'node__page__full', 'node__' . $node->id(), 'node__' . $node->id() . '__full'), 'Found expected node suggestions.');
// Change the view mode.
$view_mode = 'node.my_custom_view_mode';
$build = \Drupal::entityManager()->getViewBuilder('node')->view($node, $view_mode);
$variables['elements'] = $build;
$suggestions = \Drupal::moduleHandler()->invokeAll('theme_suggestions_node', array($variables));
$this->assertEqual($suggestions, array('node__node_my_custom_view_mode', 'node__page', 'node__page__node_my_custom_view_mode', 'node__' . $node->id(), 'node__' . $node->id() . '__node_my_custom_view_mode'), 'Found expected node suggestions.');
}
}
......@@ -582,9 +582,13 @@ function node_preprocess_block(&$variables) {
function node_theme_suggestions_node(array $variables) {
$suggestions = array();
$node = $variables['elements']['#node'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'node__' . $sanitized_view_mode;
$suggestions[] = 'node__' . $node->bundle();
$suggestions[] = 'node__' . $node->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = 'node__' . $node->id();
$suggestions[] = 'node__' . $node->id() . '__' . $sanitized_view_mode;
return $suggestions;
}
......
......@@ -52,7 +52,7 @@ function testTwigDebugMarkup() {
$output = drupal_render($build);
$this->assertTrue(strpos($output, '<!-- THEME DEBUG -->') !== FALSE, 'Twig debug markup found in theme output when debug is enabled.');
$this->assertTrue(strpos($output, "CALL: _theme('node')") !== FALSE, 'Theme call information found.');
$this->assertTrue(strpos($output, 'x node--1' . $extension . PHP_EOL . ' * node--page' . $extension . PHP_EOL . ' * node' . $extension) !== FALSE, 'Suggested template files found in order and node ID specific template shown as current template.');
$this->assertTrue(strpos($output, '* node--1--full' . $extension . PHP_EOL . ' x node--1' . $extension . PHP_EOL . ' * node--page--full' . $extension . PHP_EOL . ' * node--page' . $extension . PHP_EOL . ' * node--full' . $extension . PHP_EOL . ' * node' . $extension) !== FALSE, 'Suggested template files found in order and node ID specific template shown as current template.');
$template_filename = $templates['node__1']['path'] . '/' . $templates['node__1']['template'] . $extension;
$this->assertTrue(strpos($output, "BEGIN OUTPUT from '$template_filename'") !== FALSE, 'Full path to current template file found.');
......@@ -61,7 +61,7 @@ function testTwigDebugMarkup() {
$node2 = $this->drupalCreateNode();
$build = node_view($node2);
$output = drupal_render($build);
$this->assertTrue(strpos($output, '* node--2' . $extension . PHP_EOL . ' * node--page' . $extension . PHP_EOL . ' x node' . $extension) !== FALSE, 'Suggested template files found in order and base template shown as current template.');
$this->assertTrue(strpos($output, '* node--2--full' . $extension . PHP_EOL . ' * node--2' . $extension . PHP_EOL . ' * node--page--full' . $extension . PHP_EOL . ' * node--page' . $extension . PHP_EOL . ' * node--full' . $extension . PHP_EOL . ' x node' . $extension) !== FALSE, 'Suggested template files found in order and base template shown as current template.');
// Create another node and make sure the template suggestions shown in the
// debug markup are correct.
......@@ -70,7 +70,7 @@ function testTwigDebugMarkup() {
$build += node_view($node3);
$output = drupal_render($build);
$this->assertTrue(strpos($output, "CALL: _theme('node__foo__bar')") !== FALSE, 'Theme call information found.');
$this->assertTrue(strpos($output, '* node--foo--bar' . $extension . PHP_EOL . ' * node--foo' . $extension . PHP_EOL . ' * node--3' . $extension . PHP_EOL . ' * node--page' . $extension . PHP_EOL . ' x node' . $extension) !== FALSE, 'Suggested template files found in order and base template shown as current template.');
$this->assertTrue(strpos($output, '* node--foo--bar' . $extension . PHP_EOL . ' * node--foo' . $extension . PHP_EOL . ' * node--3--full' . $extension . PHP_EOL . ' * node--3' . $extension . PHP_EOL . ' * node--page--full' . $extension . PHP_EOL . ' * node--page' . $extension . PHP_EOL . ' * node--full' . $extension . PHP_EOL . ' x node' . $extension) !== FALSE, 'Suggested template files found in order and base template shown as current template.');
// Disable debug, rebuild the service container, and clear all caches.
$this->settingsSet('twig_debug', FALSE);
......
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