Browse Source

Add drupal_messages Twig function.

merge-requests/4/head
Chi 7 years ago
parent
commit
c3791bd1f3
  1. 8
      src/TwigExtension.php
  2. 4
      tests/src/Functional/TwigTweakTest.php
  3. 1
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig
  4. 3
      tests/twig_tweak_test/twig_tweak_test.info.yml
  5. 1
      tests/twig_tweak_test/twig_tweak_test.module

8
src/TwigExtension.php

@ -45,6 +45,7 @@ class TwigExtension extends \Twig_Extension {
new \Twig_SimpleFunction('drupal_title', [$this, 'drupalTitle']),
new \Twig_SimpleFunction('drupal_url', [$this, 'drupalUrl']),
new \Twig_SimpleFunction('drupal_link', [$this, 'drupalLink']),
new \Twig_SimpleFunction('drupal_messages', [$this, 'drupalMessages']),
];
}
@ -507,6 +508,13 @@ class TwigExtension extends \Twig_Extension {
}
}
/**
* Displays status messages.
*/
public function drupalMessages() {
return ['#type' => 'status_messages'];
}
/**
* Replaces all tokens in a given string with appropriate values.
*

4
tests/src/Functional/TwigTweakTest.php

@ -195,6 +195,10 @@ class TwigTweakTest extends BrowserTestBase {
$xpath = '//div[@class = "tt-link"]';
self::assertEquals($link, trim($this->xpath($xpath)[0]->getHtml()));
// Test status messages.
$xpath = '//div[@class = "tt-messages"]/div[contains(@class, "messages--status") and contains(., "Hello world!")]';
$this->assertByXpath($xpath);
// Test protected link.
$xpath = '//div[@class = "tt-link-access"]';
self::assertEquals('', trim($this->xpath($xpath)[0]->getHtml()));

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

@ -43,6 +43,7 @@
<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>
<div class="tt-messages">{{ drupal_messages() }}</div>
<div class="tt-link-access">{{ drupal_link('Edit', 'node/1/edit', {absolute: true}, true) }}</div>
<div class="tt-token-replace">{{ 'Site name: [site:name]' | token_replace }}</div>
<div class="tt-preg-replace">{{ 'FOO' | preg_replace('/(foo)/i', '$1-bar') }}</div>

3
tests/twig_tweak_test/twig_tweak_test.info.yml

@ -1,8 +1,9 @@
name: Twig tweak test
type: module
description: Support module for Tweak twig testing.
description: Support module for Twig tweak testing.
package: Testing
core: 8.x
dependencies:
- drupal:system (>= 8.5.0)
- drupal:node
- drupal:twig_tweak

1
tests/twig_tweak_test/twig_tweak_test.module

@ -9,6 +9,7 @@
* Implements hook_page_bottom().
*/
function twig_tweak_test_page_bottom(array &$page_bottom) {
\Drupal::service('messenger')->addMessage('Hello world!');
$page_bottom['twig_tweak_test']['#theme'] = 'twig_tweak_test';
}

Loading…
Cancel
Save