Trim Text Fields and Summaries using Smart Trim in Drupal

Don’t forget to subscribe to our YouTube channel to stay up-to-date.

The “Long text and summary” field has a formatter called “Summary or trimmed”. This formatter allows you to trim text to a specified set length which is important if you’re displaying a list of articles. The last thing you want is to have an article display 1000 words on the homepage.

However, this default “Summary or trimmed” formatter cannot trim the summary. If the author enters in lots of text into the summary it’ll display everything.

The Smart Trim module offers a field formatter for trimming text fields. Additional settings are available more than the default “Summary or trimmed” option – an improvement over the default formatter.

Smart Trim can also apply to Text (Plain) and Text (Plain, Long) fields which, by default, do not have the “Trimmed” option available.

Getting Started

Before we can begin, go download the Smart Trim module.

Using Composer:

Composer require drupal/smart_trim

Once downloaded go and install the module.

How to Use Smart Trimmed Formatter

1. Go to Structure > “Content types” and click on the “Manage display” on a content type row.

2. Under the “Format” of text fields, an option of “Smart trimmed” will be available.  Select this option.

3. After the “Smart trimmed” option is selected, click on the cogwheel on the right.  More configuration settings are available. (The Summary option is not available for text fields without summary.)

4. Configure the settings accordingly, then click Update and save.

Format Settings

Trim units

Trimming of text can be applied to characters or words. This takes care of word-boundary issues on trimming.

Suffix

Suffix can be appended to the trimmed text, such as “…”.

Display more link?

When enabled, a link of “More” will be added to the bottom of the page. This will allow the user an option to view the entire page.

Summary

This option is only available when using the “Text (formatted, long, with summary)” field. Three options are available:

  • Use summary if present, and do not trim
  • Use summary if present, and honor trim settings
  • Do not use summary

These options allow overriding of the summary.

Strip HTML

This option will Strip out any HTML when displayed. This is important if you’re truncating text because you could display an opening HTML tag but not close which could break the site styling.

Summary

The Smart Trim module provides a more powerful trimming formatter than what is offered out-of-the-box. Best of all, it can be used on any type of text field.

FAQ

Q:  Can I use this module for all text fields?

Yes, it can be applied to all default text fields, plain or formatted, short or long.

2 thoughts on “Trim Text Fields and Summaries using Smart Trim in Drupal”

Leave a Comment

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Media Management in Drupal

Download a FREE 8 part video course on managing media in Drupal.