truncate() filter causes mb_strlen() deprecation warning if given a NULL value

Created on 7 July 2025, 5 days ago

Problem/Motivation

If you pass a field value to the truncate() filter, like this:

            {{ featured_stories[2]['description']|truncate(100, true, '...') }}

and that field is not set, you get a deprecation warning:

> Deprecated function: mb_strlen(): Passing null to parameter #1 ($string) of type string is deprecated in Drupal\Component\Utility\Unicode::truncate() (line 310 of core/lib/Drupal/Component/Utility/Unicode.php).

This is because Unicode::truncate() expects its parameter to be a string, and so doesn't check before passing it on to mb_strlen().

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

3.4

Component

Code

Created by

🇬🇧United Kingdom joachim

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @joachim
  • 🇮🇳India kalpanajaiswal
    new TwigFilter('truncate', [Unicode::class, 'truncate']),
    

    Twig Tweak is not implementing its own truncate() logic, but is delegating to:

    \Drupal\Component\Utility\Unicode::truncate()
    

    we can wrap it in custom method and safely cast or normalize input before it goes into Unicode::truncate().

  • Merge request !69Update file TwigTweakExtension.php → (Open) created by kalpanajaiswal
  • Pipeline finished with Failed
    4 days ago
    Total: 171s
    #541748
  • Pipeline finished with Failed
    3 days ago
    Total: 209s
    #542667
  • Pipeline finished with Failed
    3 days ago
    Total: 265s
    #542677
Production build 0.71.5 2024