From 3b1bb1e239f2eaef30c2f4d4aa0b2e8da4a5b37e Mon Sep 17 00:00:00 2001 From: Chi Date: Sat, 14 Apr 2018 15:16:23 +0500 Subject: [PATCH] Added truncate() Twig function. --- src/TwigExtension.php | 26 +++++++++++++++++++ tests/src/Functional/TwigTweakTest.php | 4 +++ .../templates/twig-tweak-test.html.twig | 1 + 3 files changed, 31 insertions(+) diff --git a/src/TwigExtension.php b/src/TwigExtension.php index 6c2e94e..99e810d 100644 --- a/src/TwigExtension.php +++ b/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. * diff --git a/tests/src/Functional/TwigTweakTest.php b/tests/src/Functional/TwigTweakTest.php index 54b812f..6de13da 100644 --- a/tests/src/Functional/TwigTweakTest.php +++ b/tests/src/Functional/TwigTweakTest.php @@ -227,6 +227,10 @@ class TwigTweakTest extends BrowserTestBase { $xpath = '//div[@class = "tt-check-markup"]'; self::assertEquals('bold 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"]]'; 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 de03b78..a371a5d 100644 --- a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig +++ b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig @@ -51,6 +51,7 @@
{{ 'public://images/ocean.jpg' | image_style('thumbnail') }}
{{ 'Привет!' | transliterate('ru') }}
{{ 'bold strong' | check_markup('twig_tweak_test') }}
+
{{ 'Hello world!'|truncate(10, true, true) }}
{{ node|view }}
{{ node.title|view }}
{{ node.title[0]|view }}