By james, 6 December, 2023
Day 6 door showing the Smart Trim light bulb logo

Welcome back to day 6 of the Drupal Advent calendar. Behind today’s door is the Smart Trim module, and here’s Mark Casias (markie) to tell us all about it.

Smart Trim allows you to control the length of your content by trimming it intelligently. It is designed to be a focused, lightweight improvement over Drupal core’s current formatter trimming capabilities. The maintainers’ focus is stability and ease-of-use. Customizations to the module are encouraged with template overrides and Smart Trim hook implementations.

After installing and enabling Smart Trim, you will see a “Smart trimmed” option in the format dropdown for your text fields. For content types, this is located on the “Manage Display” page.

Screenshot showing the Manage display settings
Managing the display settings to select Smart trim

Once the “Smart trimmed” formatter is selected, the following configurations can be controlled: 

  • The trim length
  • Whether the trim length is measured in characters or words
  • Appending an optional suffix at the trim point
Screenshot showing basic settings for Smart trimming a field
Basic settings for Smart trimming a field

The Summary option is to determine how the formatter will act if there is summary data present in the field. You can either use the summary as is, trim the summary based on the settings provided, or skip the summary all together and just use the trim settings against the field value.

There are additional settings available which help troubleshoot 

  • Stripping out HTML tags from the field
  • Still apply output if there is a zero length trim
  • Replace any tokens before trimming.
Screenshot showing additional options
Additional Smart trim options

There is an option to wrap the trimmed content in its own container, however this is deprecated. A template file is provided which can be overridden. This template file can also be used in place of the “More Link” settings, seen below. However if one does not want to use the template file there are options one can set in the administration UI

Screenshot of more link options
More link options

In conclusion, the Smart Trim module is a useful tool for controlling the length of your content in Drupal. For more information, there is full documentation and as always, any questions or issues can be addressed in its issue queue.

Photo of Mark CasiasMark Casias has been developing and maintaining Drupal sites for many years, and is an active part of the community, adding core commits and a few of his own modules. works at Kanopi Studios as a Drupal Tech Lead. Outside of his home office in Albuquerque New Mexico, Mark plays bass in The Randomatics and enjoys ice hockey.

Comments

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.