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.
Project information
Maintenance fixes only
Considered feature-complete by its maintainers.- Project categories: Site structure
25 sites report using this module
- Created by erutan on , updated
Stable releases for this project are covered by the security advisory policy.
Look for the shield icon below.
