Add a way to truncate HTML strings without counting or damaging HTML elements (and use it in Views) - Html::truncate()

Created on 4 June 2014, about 10 years ago
Updated 23 February 2023, over 1 year ago

Problem:

It doesn't make any sense to me that FieldPluginBase::trimText() in Views and Unicode::truncate() have slightly different but almost identical behaviour. I could understand this when Views was contrib and wanted to implement its own logic, and I understand it could be beneficial for the two functions to have different arguments, but now that Views is in Core, can we merge the two? We already have visibly diverging handling of truncation now that Views uses "..." and Unicode uses "…".

One difference that I can see is that Views handles HTML entities at the end of strings, whereas the Unicode truncate doesn't have a concept of HTML. If that's the only reason to keep the Views truncation kicking around then that functionality should probably be moved to \Drupal\Component\Utility\Html.

Solution:

- Introduce Html::truncate() that mirrors/wraps Unicode::truncate() but handles HTML entities, etc.
- FieldPluginBase::trimText() simply wraps Html::truncate() and/or Unicode::truncate()

✨ Feature request
Status

Active

Version

9.5

Component
MarkupΒ  β†’

Last updated 6 days ago

No maintainer
Created by

πŸ‡¦πŸ‡ΊAustralia thedavidmeister

Live updates comments and jobs are added and updated live.
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.

Production build 0.69.0 2024