From 050653f584cbe86a9e3c5efe887fc53f1135d50a Mon Sep 17 00:00:00 2001 From: Chi Date: Fri, 20 Jan 2017 19:00:47 +0300 Subject: [PATCH] Issue #2845318 by leraa: drupal_token function does not support passing in the $data argument --- src/TwigExtension.php | 15 +++++++++++++-- tests/src/Functional/TwigTweakTest.php | 4 ++++ .../templates/twig-tweak-test.html.twig | 1 + tests/twig_tweak_test/twig_tweak_test.module | 7 +++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/TwigExtension.php b/src/TwigExtension.php index c604341..fd12cbb 100644 --- a/src/TwigExtension.php +++ b/src/TwigExtension.php @@ -141,12 +141,23 @@ class TwigExtension extends \Twig_Extension { * * @param string $token * A replaceable token. + * @param array $data + * (optional) An array of keyed objects. For simple replacement scenarios + * 'node', 'user', and others are common keys, with an accompanying node or + * user object being the value. Some token types, like 'site', do not + * require any explicit information from $data and can be replaced even if + * it is empty. + * @param array $options + * (optional) A keyed array of settings and flags to control the token + * replacement process. * * @return string * The token value. + * + * @see \Drupal\Core\Utility\Token::replace() */ - public function drupalToken($token) { - return $this->token->replace("[$token]"); + public function drupalToken($token, array $data = [], array $options = []) { + return $this->token->replace("[$token]", $data, $options); } /** diff --git a/tests/src/Functional/TwigTweakTest.php b/tests/src/Functional/TwigTweakTest.php index e8b63bc..e277f4f 100644 --- a/tests/src/Functional/TwigTweakTest.php +++ b/tests/src/Functional/TwigTweakTest.php @@ -109,6 +109,10 @@ class TwigTweakTest extends BrowserTestBase { $xpath = '//div[@class = "tt-token" and . = "Drupal"]'; $this->assertByXpath($xpath); + // Test token with context. + $xpath = '//div[@class = "tt-token-data" and . = "Beta"]'; + $this->assertByXpath($xpath); + // Test config. $xpath = '//div[@class = "tt-config" and . = "Anonymous"]'; $this->assertByXpath($xpath); 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 0a7b45f..bf4d882 100644 --- a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig +++ b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig @@ -11,6 +11,7 @@
{{ drupal_menu('twig-tweak-test', 1, 1) }}
{{ drupal_block('powered_by_drupal') }}
{{ drupal_token('site:name') }}
+
{{ drupal_token('node:title', {'node': node}) }}
{{ drupal_config('user.settings', 'anonymous') }}
{{ 'Site name: [site:name]' | token_replace }}
{{ 'foo' | preg_replace('(foo)', '$1-bar') }}
diff --git a/tests/twig_tweak_test/twig_tweak_test.module b/tests/twig_tweak_test/twig_tweak_test.module index cabd649..69d5703 100644 --- a/tests/twig_tweak_test/twig_tweak_test.module +++ b/tests/twig_tweak_test/twig_tweak_test.module @@ -18,3 +18,10 @@ function twig_tweak_test_page_bottom(array &$page_bottom) { function twig_tweak_test_theme() { return ['twig_tweak_test' => ['variables' => []]]; } + +/** + * Prepares variables for twig-tweak-test template. + */ +function template_preprocess_twig_tweak_test(&$vars) { + $vars['node'] = $node = Drupal::routeMatch()->getParameter('node'); +}