/** * Tests debug markup added to Twig template output. */ function testTwigDebugMarkup() { /** @var \Drupal\Core\Render\RendererInterface $renderer */ $renderer = $this->container->get('renderer'); $extension = twig_extension(); \Drupal::service('theme_handler')->install(array('test_theme')); \Drupal::service('theme_handler')->setDefault('test_theme'); $this->drupalCreateContentType(array('type' => 'page')); // Enable debug, rebuild the service container, and clear all caches. $parameters = $this->container->getParameter('twig.config'); $parameters['debug'] = TRUE; $this->setContainerParameter('twig.config', $parameters); $this->rebuildContainer(); $this->resetAll(); $cache = $this->container->get('theme.registry')->get(); // Create array of Twig templates. $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. $node = $this->drupalCreateNode(); $build = node_view($node); $output = $renderer->renderRoot($build); $this->assertTrue(strpos($output, '<!-- THEME DEBUG -->') !== FALSE, 'Twig debug markup found in theme output when debug is enabled.'); $this->setRawContent($output); $this->assertTrue(strpos($output, "THEME HOOK: 'node'") !== FALSE, 'Theme call information found.'); $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.'); $this->assertEscaped('node--<script type="text/javascript">alert(\'yo\');</script>'); $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.'); // Create another node and make sure the template suggestions shown in the // debug markup are correct. $node2 = $this->drupalCreateNode(); $build = node_view($node2); $output = $renderer->renderRoot($build); $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. $node3 = $this->drupalCreateNode(); $build = array('#theme' => 'node__foo__bar'); $build += node_view($node3); $output = $renderer->renderRoot($build); $this->assertTrue(strpos($output, "THEME HOOK: 'node__foo__bar'") !== FALSE, 'Theme call information found.'); $this->assertTrue(strpos($output, '* node--foo--bar' . $extension . PHP_EOL . ' * node--foo' . $extension . PHP_EOL . ' * node--<script type="text/javascript">alert('yo');</script>' . $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. $parameters = $this->container->getParameter('twig.config'); $parameters['debug'] = FALSE; $this->setContainerParameter('twig.config', $parameters); $this->rebuildContainer(); $this->resetAll(); $build = node_view($node); $output = $renderer->renderRoot($build); $this->assertFalse(strpos($output, '<!-- THEME DEBUG -->') !== FALSE, 'Twig debug markup not found in theme output when debug is disabled.'); }
/** * Add conditional stylesheets: * For more information: http://msdn.microsoft.com/en-us/library/ms537512.aspx * template.php implementation based on Genesis Theme by jmburnz. */ function kerberos_theme(&$existing, $type, $theme, $path) { // Compute the conditional stylesheets. if (!module_exists('conditional_styles')) { include_once $base_path . drupal_get_path('theme', 'kerberos') . '/includes/template.conditional-styles.inc'; // _conditional_styles_theme() only needs to be run once. if ($theme == 'kerberos') { _conditional_styles_theme($existing, $type, $theme, $path); } } $templates = drupal_find_theme_functions($existing, array('phptemplate', $theme)); $templates += drupal_find_theme_templates($existing, '.tpl.php', $path); return $templates; }
/** * Implements hook_theme_registry_alter(). */ function exampletheme_theme_registry_alter(&$theme_registry) { // Defined path to the current module. $module_path = drupal_get_path('theme', 'jigsawtheme'); // Find all .tpl.php files in this module's folder recursively. $template_file_objects = drupal_find_theme_templates($theme_registry, '.tpl.php', $module_path); // Iterate through all found template file objects. foreach ($template_file_objects as $key => $template_file_object) { // If the template has not already been overridden by a theme. if (!isset($theme_registry[$key]['theme path']) || !preg_match('#/themes/#', $theme_registry[$key]['theme path'])) { // Alter the theme path and template elements. $theme_registry[$key]['theme path'] = $module_path; $theme_registry[$key] = array_merge($theme_registry[$key], $template_file_object); $theme_registry[$key]['type'] = 'theme'; } } }
/** * Tests debug markup added to Twig template output. */ function testTwigDebugMarkup() { $extension = twig_extension(); theme_enable(array('test_theme')); \Drupal::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(); $this->resetAll(); $cache = $this->container->get('theme.registry')->get(); // Create array of Twig templates. $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. $node = $this->drupalCreateNode(); $build = node_view($node); $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, '* 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.'); // Create another node and make sure the template suggestions shown in the // debug markup are correct. $node2 = $this->drupalCreateNode(); $build = node_view($node2); $output = drupal_render($build); $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. $node3 = $this->drupalCreateNode(); $build = array('#theme' => 'node__foo__bar'); $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--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); $this->rebuildContainer(); $this->resetAll(); $build = node_view($node); $output = drupal_render($build); $this->assertFalse(strpos($output, '<!-- THEME DEBUG -->') !== FALSE, 'Twig debug markup not found in theme output when debug is disabled.'); }
/** * Tests drupal_find_theme_templates(). */ public function testFindThemeTemplates() { $registry = $this->container->get('theme.registry')->get(); $templates = drupal_find_theme_templates($registry, '.html.twig', drupal_get_path('theme', 'test_theme')); $this->assertEqual($templates['node__1']['template'], 'node--1', 'Template node--1.tpl.twig was found in test_theme.'); }