From c3791bd1f3637fb204843b26900d7012c0238708 Mon Sep 17 00:00:00 2001 From: Chi Date: Sat, 14 Apr 2018 11:42:32 +0500 Subject: [PATCH] Add drupal_messages Twig function. --- src/TwigExtension.php | 8 ++++++++ tests/src/Functional/TwigTweakTest.php | 4 ++++ tests/twig_tweak_test/templates/twig-tweak-test.html.twig | 1 + tests/twig_tweak_test/twig_tweak_test.info.yml | 3 ++- tests/twig_tweak_test/twig_tweak_test.module | 1 + 5 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/TwigExtension.php b/src/TwigExtension.php index 6ee73a3..fb80467 100644 --- a/src/TwigExtension.php +++ b/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. * diff --git a/tests/src/Functional/TwigTweakTest.php b/tests/src/Functional/TwigTweakTest.php index 634246b..9cc03e7 100644 --- a/tests/src/Functional/TwigTweakTest.php +++ b/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())); diff --git a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig index b60bfe5..a071ed9 100644 --- a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig +++ b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig @@ -43,6 +43,7 @@
{{ drupal_title() }}
{{ drupal_url('node/1', {absolute: true}) }}
+
{{ drupal_messages() }}
{{ 'Site name: [site:name]' | token_replace }}
{{ 'FOO' | preg_replace('/(foo)/i', '$1-bar') }}
diff --git a/tests/twig_tweak_test/twig_tweak_test.info.yml b/tests/twig_tweak_test/twig_tweak_test.info.yml index 0aafc7c..a484ca6 100644 --- a/tests/twig_tweak_test/twig_tweak_test.info.yml +++ b/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 diff --git a/tests/twig_tweak_test/twig_tweak_test.module b/tests/twig_tweak_test/twig_tweak_test.module index f7d00aa..aef0598 100644 --- a/tests/twig_tweak_test/twig_tweak_test.module +++ b/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'; }