Browse Source

Added 'with' Twig filter.

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

19
src/TwigExtension.php

@ -63,6 +63,7 @@ class TwigExtension extends \Twig_Extension {
new \Twig_SimpleFilter('check_markup', [$this, 'checkMarkup']),
new \Twig_SimpleFilter('truncate', [$this, 'truncate']),
new \Twig_SimpleFilter('view', [$this, 'view']),
new \Twig_SimpleFilter('with', [$this, 'with']),
];
// PHP filter should be enabled in settings.php file.
if (Settings::get('twig_tweak_enable_php_filter')) {
@ -647,6 +648,24 @@ class TwigExtension extends \Twig_Extension {
return Unicode::truncate($string, $max_length, $wordsafe, $add_ellipsis, $min_wordsafe_length);
}
/**
* Adds new element to the array.
*
* @param array $build
* The renderable array to add the child item.
* @param int|string $key
* The key of the new element.
* @param mixed $element
* The element to add.
*
* @return array
* The modified array.
*/
public function with(array $build, $key, $element) {
$build[$key] = $element;
return $build;
}
/**
* Returns a render array for entity, field list or field item.
*

4
tests/src/Functional/TwigTweakTest.php

@ -231,6 +231,10 @@ class TwigTweakTest extends BrowserTestBase {
$xpath = '//div[@class = "tt-truncate" and text() = "Hello…"]';
$this->assertByXpath($xpath);
// Test 'with'.
$xpath = '//div[@class = "tt-with"]/b[text() = "Example"]';
$this->assertByXpath($xpath);
// Test node view.
$xpath = '//div[@class = "tt-node-view"]/article[contains(@class, "node--view-mode-default")]/h2[a/span[text() = "Beta"]]';
$xpath .= '/following-sibling::footer[//h4[text() = "Member for"]]';

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

@ -52,6 +52,7 @@
<div class="tt-transliterate">{{ 'Привет!' | transliterate('ru') }}</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-with">{{ {'#markup':'Example'}|with('#prefix', '<b>')|with('#suffix', '</b>') }}</div>
<div class="tt-node-view">{{ node|view }}</div>
<div class="tt-field-list-view">{{ node.title|view }}</div>
<div class="tt-field-item-view">{{ node.title[0]|view }}</div>

Loading…
Cancel
Save