Browse Source

Added drupal_title Twig function.

8.x-1.x
Chi 8 years ago
parent
commit
34e2363c5b
  1. 32
      src/TwigExtension.php
  2. 15
      tests/src/Functional/TwigTweakTest.php
  3. 1
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig
  4. 1
      twig_tweak.services.yml

32
src/TwigExtension.php

@ -8,8 +8,10 @@ use Drupal\Core\Controller\TitleResolverInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Utility\Token;
use Drupal\image\Entity\ImageStyle;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
@ -76,6 +78,13 @@ class TwigExtension extends \Twig_Extension {
*/
protected $formBuilder;
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* TwigExtension constructor.
*
@ -95,8 +104,10 @@ class TwigExtension extends \Twig_Extension {
* The title resolver.
* @param \Drupal\Core\Form\FormBuilderInterface $form_builder
* The form builder.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, Token $token, ConfigFactoryInterface $config_factory, RouteMatchInterface $route_match, MenuLinkTreeInterface $menu_tree, RequestStack $request_stack, TitleResolverInterface $title_resolver, FormBuilderInterface $form_builder) {
public function __construct(EntityTypeManagerInterface $entity_type_manager, Token $token, ConfigFactoryInterface $config_factory, RouteMatchInterface $route_match, MenuLinkTreeInterface $menu_tree, RequestStack $request_stack, TitleResolverInterface $title_resolver, FormBuilderInterface $form_builder, RendererInterface $renderer) {
$this->entityTypeManager = $entity_type_manager;
$this->token = $token;
$this->configFactory = $config_factory;
@ -105,6 +116,7 @@ class TwigExtension extends \Twig_Extension {
$this->requestStack = $request_stack;
$this->titleResolver = $title_resolver;
$this->formBuilder = $form_builder;
$this->renderer = $renderer;
}
/**
@ -126,6 +138,7 @@ class TwigExtension extends \Twig_Extension {
// Wrap drupal_set_message() because it returns some value which is not
// suitable for Twig template.
new \Twig_SimpleFunction('drupal_set_message', [$this, 'drupalSetMessage']),
new \Twig_SimpleFunction('drupal_title', [$this, 'drupalTitle']),
];
}
@ -391,6 +404,23 @@ class TwigExtension extends \Twig_Extension {
return $build;
}
/**
* Returns a title for the current route.
*
* @return array
* A render array to represent page title.
*/
public function drupalTitle() {
$title = $this->titleResolver->getTitle(
$this->requestStack->getCurrentRequest(),
$this->routeMatch->getRouteObject()
);
$build['#markup'] = is_string($title) || $title instanceof TranslatableMarkup ?
$title : $this->renderer->render($title);
$build['#cache']['contexts'] = ['url'];
return $build;
}
/**
* Replaces all tokens in a given string with appropriate values.
*

15
tests/src/Functional/TwigTweakTest.php

@ -38,6 +38,8 @@ class TwigTweakTest extends BrowserTestBase {
* Tests output produced by the Twig extension.
*/
public function testOutput() {
// Title block rendered through drupal_region() is cached by some reason.
\Drupal::service('cache_tags.invalidator')->invalidateTags(['block_view']);
$this->drupalGet('<front>');
// Test default views display.
@ -131,6 +133,10 @@ class TwigTweakTest extends BrowserTestBase {
$xpath = '//div[@class = "messages messages--warning" and contains(., "Hi!")]';
$this->assertByXpath($xpath);
// Test page title.
$xpath = '//div[@class = "tt-title" and text() = "Beta"]';
$this->assertByXpath($xpath);
// Test token replacement.
$xpath = '//div[@class = "tt-token-replace" and text() = "Site name: Drupal"]';
$this->assertByXpath($xpath);
@ -151,13 +157,4 @@ class TwigTweakTest extends BrowserTestBase {
$this->assertSession()->elementExists('xpath', $xpath);
}
/**
* {@inheritdoc}
*/
protected function drupalGet($path, array $options = [], array $headers = []) {
// Title block rendered through drupal_region() is cached by some reason.
\Drupal::service('cache_tags.invalidator')->invalidateTags(['block_view']);
return parent::drupalGet($path, $options, $headers);
}
}

1
tests/twig_tweak_test/templates/twig-tweak-test.html.twig

@ -33,6 +33,7 @@
<div class="tt-token-data">{{ drupal_token('node:title', {'node': node}) }}</div>
<div class="tt-config">{{ drupal_config('user.settings', 'anonymous') }}</div>
<div class="tt-status-message">{{ drupal_set_message('Hi!', 'warning') }}</div>
<div class="tt-title">{{ drupal_title() }}</div>
<div class="tt-token-replace">{{ 'Site name: [site:name]' | token_replace }}</div>
<div class="tt-preg-replace">{{ 'foo' | preg_replace('(foo)', '$1-bar') }}</div>
<div class="tt-image-style">{{ 'public://images/ocean.jpg' | image_style('thumbnail') }}</div>

1
twig_tweak.services.yml

@ -10,5 +10,6 @@ services:
- '@request_stack'
- '@title_resolver'
- '@form_builder'
- '@renderer'
tags:
- { name: twig.extension }

Loading…
Cancel
Save