Twig Extensions
Adds additional Twig filters to Drupal for array manipulation, date formatting, internationalization, and text processing.
twig_extensions
Install
composer require 'drupal/twig_extensions:8.x-2.5'
composer require 'drupal/twig_extensions:8.x-2.1'
Overview
The Twig Extensions module brings the functionality of the abandoned twig/extensions package to Drupal, providing common additional features for Twig that do not directly belong in Drupal core. This module is particularly useful for theme developers who need additional text and date manipulation capabilities directly within Twig templates.
The module provides seven Twig filters organized into four categories: array manipulation (shuffle), date formatting (time_diff), internationalization (localizeddate, localizednumber, localizedcurrency), and text processing (truncate, wordwrap). These filters integrate seamlessly with Drupal's Twig environment and respect Drupal's translation system for proper localization.
The internationalization filters utilize PHP's intl extension to provide locale-aware formatting for dates, numbers, and currencies, making them essential for multilingual sites that need proper number and date formatting according to different regional standards.
Features
- Shuffle filter to randomize the order of array elements, useful for displaying random content or randomized lists in templates
- Time difference filter that converts timestamps to human-readable relative time strings like '2 hours ago' or 'in 3 days', similar to Facebook and Twitter's time display, fully translatable through Drupal's translation system
- Localized date filter that formats dates using PHP's IntlDateFormatter with support for multiple format presets (none, short, medium, long, full), custom locale, timezone, and calendar type (Gregorian or traditional)
- Localized number filter that formats numbers according to locale with support for different styles including decimal, currency, percent, scientific, spellout, ordinal, and duration, with configurable number types
- Localized currency filter that formats monetary values as properly localized currency strings with the appropriate currency symbol and number formatting
- Truncate filter that shortens text to a specified length with an optional separator (default '...'), with the ability to preserve whole words when truncating
- Wordwrap filter that wraps text at a specified line length with a configurable separator, with optional preservation of original line breaks
Use Cases
Displaying relative timestamps on content
Use the time_diff filter to show human-friendly relative times like '5 minutes ago' or '2 days ago' instead of absolute dates. This is particularly useful for social features, comments, activity feeds, or any content where recency is important. Example: {{ node.created.value|time_diff }} will display 'Posted 3 hours ago' style output that updates dynamically.
Randomizing content display order
Use the shuffle filter to randomize the order of items in a list. This is useful for displaying random testimonials, featured products, or any content where you want variety on each page load. Example: {% for item in items|shuffle %} will iterate through items in a random order each time.
Multilingual number and currency formatting
Use localizednumber and localizedcurrency filters to properly format numbers and prices according to the visitor's locale. For example, the number 1234.56 would display as '1,234.56' in English but '1.234,56' in German. Currency formatting ensures proper symbol placement and decimal handling: {{ price|localizedcurrency('EUR', 'de_DE') }} displays '1.234,56 €'.
Locale-aware date formatting
Use localizeddate to format dates according to different cultural conventions. Dates are displayed using IntlDateFormatter with full locale support. Example: {{ event_date|localizeddate('full', 'short', 'ja_JP') }} would display a date in Japanese format with full date and short time.
Creating text excerpts
Use the truncate filter to create preview text or excerpts from longer content. The preserve option ensures words are not cut in the middle. Example: {{ body|truncate(150, true) }} creates a 150-character excerpt that ends at a word boundary.
Formatting text for display
Use wordwrap to format text for fixed-width displays or to add HTML line breaks at specific intervals. Useful for preformatted text, code display, or creating visual poetry layouts. Example: {{ preformatted_text|wordwrap(80, '<br>') }}
Tips
- The time_diff filter automatically uses Drupal's translation system, so relative time strings like '2 hours ago' are translatable and will appear in the user's language
- For the internationalization filters (localizeddate, localizednumber, localizedcurrency), ensure your PHP installation has the intl extension enabled, or these filters will throw a runtime error
- The truncate filter with preserve=true will find the next space after the length limit, potentially making output longer than the specified length - use without preserve for strict length limits
- When using localizedcurrency, always specify both the currency code and locale for consistent formatting across different environments
- The shuffle filter converts Traversable objects to arrays, which may have performance implications for large datasets
- All text manipulation filters (truncate, wordwrap) are multibyte-safe and will correctly handle UTF-8 characters including emoji and non-Latin scripts