Browse Source

Support nested values in 'with' filter

merge-requests/4/head
Chi 6 years ago
parent
commit
cde0be4a1e
  1. 8
      src/TwigExtension.php
  2. 4
      tests/src/Functional/TwigTweakTest.php
  3. 1
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig

8
src/TwigExtension.php

@ -2,6 +2,7 @@
namespace Drupal\twig_tweak; namespace Drupal\twig_tweak;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\Unicode;
use Drupal\Component\Uuid\Uuid; use Drupal\Component\Uuid\Uuid;
use Drupal\Core\Block\BlockPluginInterface; use Drupal\Core\Block\BlockPluginInterface;
@ -720,7 +721,7 @@ class TwigExtension extends \Twig_Extension {
* *
* @param array $build * @param array $build
* The renderable array to add the child item. * The renderable array to add the child item.
* @param int|string $key * @param mixed $key
* The key of the new element. * The key of the new element.
* @param mixed $element * @param mixed $element
* The element to add. * The element to add.
@ -729,7 +730,12 @@ class TwigExtension extends \Twig_Extension {
* The modified array. * The modified array.
*/ */
public function with(array $build, $key, $element) { public function with(array $build, $key, $element) {
if (is_array($key)) {
NestedArray::setValue($build, $key, $element);
}
else {
$build[$key] = $element; $build[$key] = $element;
}
return $build; return $build;
} }

4
tests/src/Functional/TwigTweakTest.php

@ -309,6 +309,10 @@ class TwigTweakTest extends BrowserTestBase {
$xpath = '//div[@class = "tt-with"]/b[text() = "Example"]'; $xpath = '//div[@class = "tt-with"]/b[text() = "Example"]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// Test nested 'with'.
$xpath = '//div[@class = "tt-with-nested" and text() = "{alpha:{beta:{gamma:456}}}"]';
$this->assertByXpath($xpath);
// Test 'children'. // Test 'children'.
$xpath = '//div[@class = "tt-children" and text() = "doremi"]'; $xpath = '//div[@class = "tt-children" and text() = "doremi"]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);

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

@ -59,6 +59,7 @@
<div class="tt-check-markup">{{ '<b>bold</b> <strong>strong</strong>' | check_markup('twig_tweak_test') }}</div> <div class="tt-check-markup">{{ '<b>bold</b> <strong>strong</strong>' | check_markup('twig_tweak_test') }}</div>
<div class="tt-truncate">{{ 'Hello world!'|truncate(10, true, true) }}</div> <div class="tt-truncate">{{ 'Hello world!'|truncate(10, true, true) }}</div>
<div class="tt-with">{{ {'#markup':'Example'}|with('#prefix', '<b>')|with('#suffix', '</b>') }}</div> <div class="tt-with">{{ {'#markup':'Example'}|with('#prefix', '<b>')|with('#suffix', '</b>') }}</div>
<div class="tt-with-nested">{{ {alpha: {beta: {gamma: 123}}}|with(['alpha', 'beta', 'gamma'], 456)|json_encode|replace({'"':''}) }}</div>
<div class="tt-children"> <div class="tt-children">
{%- {%-
set build = { set build = {

Loading…
Cancel
Save