Skip to main content
drupalreleases

Use direct release data with your AI assistant.

Learn more

Substring (Bidirectional Trim) Field Formatter

Security covered Maintenance fixes only 26 sites

This module provides a field formatter to trim text content from the left or right, based on a specified offset and an optional length. It allows you to define how many characters to remove from the beginning or end of a string, or to set a fixed length for the displayed text.

About

Trims content from the left or right from a set offset and an optional length as a full on Drupal field formatter.

I hadn't found a good way to trim from the left using the Drupal interface. The
PHP substr function seemed a good way to go about it, and there's some extra functionality to boot! For more advanced functionality and examples, read the official PHP documentation linked above. :)

Usage

There are two input fields, the first is the offset parameter and the second is length.

Offset Parameter

A negative value will trim all characters from the start of the string until
the specified character count from the end of the string (trimming from the
left), a positive value will do the opposite and trim from the right. For the
string "abcdef", 2 would output "cdef" and -2 would output "ef"'.

Length Parameter

Sets the string length to a fixed number of characters. For the string "abcdef"
, with a start/offset position of 2, a trim length of 1 would output "c" and a
trim length of -1 would output "cde".

Aside

For views you could use Twig 2's Slice Filter in a rewrite or global text field, there are instances where using a field formatter works smoother (passing information to Charts, Simple Math, etc). If this works for you you'd have one less contrib module installed, though things could get a little clunky!

I was playing around with having radio buttons for end/left, start/right direction, then having logic multiply by -1 to an absolute value of start - it might be a little more intuitive from a user standpoint, but it ended up being simpler just keeping it the same structure as the PHP function. If there's enough interest that could warrant an extra branch.

Drupal 8–11

Ask your assistant about Substring (Bidirectional Trim) Field Formatter

Check compatibility with your Drupal and PHP version, maintenance and security coverage, from current release data. How it works

Depends on

Dependencies of the latest stable release

  • text Drupal core

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
3
Tracked since
Jan 2023
Latest release
2 years ago
Releases (12 mo)
0
Maintenance
Slowing

Release Timeline

Releases

Version Type Core PHP Release date
1.2.0 Stable 8–11 Sep 17, 2024
1.1.0 Stable 8–10 Feb 1, 2023
1.0.3 Stable 8–9 Jan 31, 2023