Browse Source

Remove drupal_set_message Twig function.

merge-requests/4/head 8.x-2.0-rc4
Chi 7 years ago
parent
commit
4277c76990
  1. 25
      src/TwigExtension.php
  2. 5
      tests/src/Functional/TwigTweakTest.php
  3. 1
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig

25
src/TwigExtension.php

@ -42,9 +42,6 @@ class TwigExtension extends \Twig_Extension {
new \Twig_SimpleFunction('drupal_config', [$this, 'drupalConfig']),
new \Twig_SimpleFunction('drupal_dump', [$this, 'drupalDump']),
new \Twig_SimpleFunction('dd', [$this, 'drupalDump']),
// 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']),
new \Twig_SimpleFunction('drupal_url', [$this, 'drupalUrl']),
new \Twig_SimpleFunction('drupal_link', [$this, 'drupalLink']),
@ -445,28 +442,6 @@ class TwigExtension extends \Twig_Extension {
$this->drupalDump($var);
}
/**
* Sets a message to display to the user.
*
* @param string|\Drupal\Component\Render\MarkupInterface $message
* (optional) The translated message to be displayed to the user.
* @param string $type
* (optional) The message's type. Defaults to 'status'.
* @param bool $repeat
* (optional) If this is FALSE and the message is already set, then the
* message will not be repeated. Defaults to FALSE.
*
* @return array
* A render array to disable caching.
*
* @see drupal_set_message()
*/
public function drupalSetMessage($message = NULL, $type = 'status', $repeat = FALSE) {
drupal_set_message($message, $type, $repeat);
$build['#cache']['max-age'] = 0;
return $build;
}
/**
* Returns a title for the current route.
*

5
tests/src/Functional/TwigTweakTest.php

@ -104,7 +104,6 @@ class TwigTweakTest extends BrowserTestBase {
// Test region.
$xpath = '//div[@class = "tt-region"]/div[@class = "region region-sidebar-first"]';
$xpath .= '/div[contains(@class, "block-page-title-block") and h1[@class="page-title" and text() = "Log in"]]';
$xpath .= '/following-sibling::div[@class="messages messages--warning" and contains(., "Hi!")]';
$xpath .= '/following-sibling::div[contains(@class, "block-system-powered-by-block")]/span[. = "Powered by Drupal"]';
$this->assertByXpath($xpath);
@ -181,10 +180,6 @@ class TwigTweakTest extends BrowserTestBase {
$xpath = '//div[@class = "tt-config" and text() = "Anonymous"]';
$this->assertByXpath($xpath);
// Test status message.
$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);

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

@ -40,7 +40,6 @@
<div class="tt-token">{{ drupal_token('site:name') }}</div>
<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-url">{{ drupal_url('node/1', {absolute: true}) }}</div>
<div class="tt-link">{{ drupal_link('Edit', 'node/1/edit', {absolute: true}) }}</div>

Loading…
Cancel
Save