Add a unified way of creating twig filters and functions to core

Created on 25 March 2016, over 9 years ago
Updated 15 July 2025, about 18 hours ago

Drupal Core imho is lacking a clean way of adding a TWIG filter or function to a custom module.

It would be really nice if this could be done with the new found services / annotations system, and fortunally enough Erik Seifert β†’ created such a system in a contrib module called Twig Extender β†’

For example adding a |truncate filter could easily become one class doing :

namespace Drupal\twig_extender\Plugin\TwigPlugin;

use Drupal\twig_extender\Plugin\Twig\TwigPluginBase;
use Drupal\Component\Utility\Unicode;

/**
 * Example plugin for truncate
 *
 *
 * @TwigPlugin(
 *   id = "twig_extender_truncate",
 *   label = @Translation("Truncate string"),
 *   type = "filter",
 *   name = "truncate",
 *   function = "truncate"
 * )
 */

class Truncate extends TwigPluginBase {
  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);
  }
}

source : http://cgit.drupalcode.org/twig_extender/tree/src

I personally believe adding this to core instead of adding a dependency to contrib for every project that wants to extend TWIG. This could save a lot of modules, including one of my own, in re-inventing the wheel on adding Twig extension to a project. Also having a unified way to adding TWIG filters and functions will help DX a lot.

At this moment 8.1.x is in beta, so i guess it's to late to add it to that version, however the impact could be minimal, so it can be added to point release without any issues. Would be nice to convert the core filters and functions, but it's not trivial for a release (IMHO!)

I did not create a patch, because I wanted to check with core devs first if this idea is feasible.

✨ Feature request
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component

theme system

Created by

πŸ‡³πŸ‡±Netherlands renΓ© bakx Netherlands

Live updates comments and jobs are added and updated live.
  • stale-issue-cleanup

    To track issues in the developing policy for closing stale issues, [Policy, no patch] closing older issues

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thank you for sharing your idea for improving Drupal.

    We are working to decide if this proposal meets the Criteria for evaluating proposed changes. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or there is no community support. Your thoughts on this will allow a decision to be made.

    Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

    Thanks!

Production build 0.71.5 2024