Browse Source

Added truncate() Twig function.

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

26
src/TwigExtension.php

@ -2,6 +2,7 @@
namespace Drupal\twig_tweak;
use Drupal\Component\Utility\Unicode;
use Drupal\Component\Uuid\Uuid;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Block\TitleBlockPluginInterface;
@ -60,6 +61,7 @@ class TwigExtension extends \Twig_Extension {
new \Twig_SimpleFilter('image_style', [$this, 'imageStyle']),
new \Twig_SimpleFilter('transliterate', [$this, 'transliterate']),
new \Twig_SimpleFilter('check_markup', [$this, 'checkMarkup']),
new \Twig_SimpleFilter('truncate', [$this, 'truncate']),
new \Twig_SimpleFilter('view', [$this, 'view']),
];
// PHP filter should be enabled in settings.php file.
@ -621,6 +623,30 @@ class TwigExtension extends \Twig_Extension {
return check_markup($text, $format_id, $langcode, $filter_types_to_skip);
}
/**
* Truncates a UTF-8-encoded string safely to a number of characters.
*
* @param string $string
* The string to truncate.
* @param int $max_length
* An upper limit on the returned string length, including trailing ellipsis
* if $add_ellipsis is TRUE.
* @param bool $wordsafe
* (Optional) If TRUE, attempt to truncate on a word boundary.
* @param bool $add_ellipsis
* (Optional) If TRUE, add '...' to the end of the truncated string.
* @param int $min_wordsafe_length
* (Optional) If TRUE, the minimum acceptable length for truncation.
*
* @return string
* The truncated string.
*
* @see Unicode::truncate()
*/
public function truncate($string, $max_length, $wordsafe = FALSE, $add_ellipsis = FALSE, $min_wordsafe_length = 1) {
return Unicode::truncate($string, $max_length, $wordsafe, $add_ellipsis, $min_wordsafe_length);
}
/**
* Returns a render array for entity, field list or field item.
*

4
tests/src/Functional/TwigTweakTest.php

@ -227,6 +227,10 @@ class TwigTweakTest extends BrowserTestBase {
$xpath = '//div[@class = "tt-check-markup"]';
self::assertEquals('<b>bold</b> strong', trim($this->xpath($xpath)[0]->getHtml()));
// Test truncation.
$xpath = '//div[@class = "tt-truncate" and text() = "Hello…"]';
$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

@ -51,6 +51,7 @@
<div class="tt-image-style">{{ 'public://images/ocean.jpg' | image_style('thumbnail') }}</div>
<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-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