Browse Source

Issue #2845318 by leraa: drupal_token function does not support passing in the $data argument

8.x-1.x
Chi 8 years ago
parent
commit
050653f584
  1. 15
      src/TwigExtension.php
  2. 4
      tests/src/Functional/TwigTweakTest.php
  3. 1
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig
  4. 7
      tests/twig_tweak_test/twig_tweak_test.module

15
src/TwigExtension.php

@ -141,12 +141,23 @@ class TwigExtension extends \Twig_Extension {
* *
* @param string $token * @param string $token
* A replaceable 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 * @return string
* The token value. * The token value.
*
* @see \Drupal\Core\Utility\Token::replace()
*/ */
public function drupalToken($token) { public function drupalToken($token, array $data = [], array $options = []) {
return $this->token->replace("[$token]"); return $this->token->replace("[$token]", $data, $options);
} }
/** /**

4
tests/src/Functional/TwigTweakTest.php

@ -109,6 +109,10 @@ class TwigTweakTest extends BrowserTestBase {
$xpath = '//div[@class = "tt-token" and . = "Drupal"]'; $xpath = '//div[@class = "tt-token" and . = "Drupal"]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// Test token with context.
$xpath = '//div[@class = "tt-token-data" and . = "Beta"]';
$this->assertByXpath($xpath);
// Test config. // Test config.
$xpath = '//div[@class = "tt-config" and . = "Anonymous"]'; $xpath = '//div[@class = "tt-config" and . = "Anonymous"]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);

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

@ -11,6 +11,7 @@
<div class="tt-menu-depth">{{ drupal_menu('twig-tweak-test', 1, 1) }}</div> <div class="tt-menu-depth">{{ drupal_menu('twig-tweak-test', 1, 1) }}</div>
<div class="tt-block">{{ drupal_block('powered_by_drupal') }}</div> <div class="tt-block">{{ drupal_block('powered_by_drupal') }}</div>
<div class="tt-token">{{ drupal_token('site:name') }}</div> <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-config">{{ drupal_config('user.settings', 'anonymous') }}</div>
<div class="tt-token-replace">{{ 'Site name: [site:name]' | token_replace }}</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-preg-replace">{{ 'foo' | preg_replace('(foo)', '$1-bar') }}</div>

7
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() { function twig_tweak_test_theme() {
return ['twig_tweak_test' => ['variables' => []]]; 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');
}

Loading…
Cancel
Save