Updated for Drupal 8+: Trim Text Fields and Summaries using Smart Trim in Drupal
The “Long text and summary” field has a pretty handy formatter called “Summary or trimmed”. This will display a summary, if one is supplied, or Drupal will simply trim the text and display it.
The problem with this formatter is that you can’t trim the summary. For example, if an editor adds three paragraphs into the summary section, then the whole summary is shown. But sometimes you may need control over how much of the summary is displayed. This is especially true if your design requires the teaser to have a consistent height.
What’s the best way of offering a summary to your editors that also trims it? Enter Smart Trim.
The Smart Trim module is an improved version of the “Summary or Trimmed” formatter and a whole lot more. It does a lot of useful stuff, but the one we want to discuss is the ability to trim summaries.
Getting Started
To get going simply download and install the Smart Trim module. No other modules are required just enable it and you’re good to go.
If you use Drush, run the following command:
drush dl smart_trim drush en smart_trim
How to Control Summaries
Let’s first examine the problem. It’ll make more sense if you can see it in action.
Go ahead and create an article with a lot of text in the summary section.
Now, if you view the article teaser, you’ll see the whole summary is being displayed.
For most websites this is fine. An editor can add a summary if needed, if no summary is supplied then Drupal will generate its own by trimming the body.
But what if you want to trim the text in the summary and not display the whole thing? This is where Smart Trim can help.
How to Use Smart Trim
Let’s configure the article teaser so it trims the summary to the first 600 characters, this will make the size of the teaser more predictable. An editor can not accidentally enter in five paragraphs and then wonder why the teaser listing is so long.
1. Go to Structure, “Content types” and click on “manage display” on the Article row.
2. Because we want to change the teaser, click on Teaser in the top right corner.
3. From the Format drop-down on the Body row select “Smart trimmed”.
4. Click on the cogwheel and from the Summary drop-down list, select “Use summary if present, honor trim settings”, then click on Update. Don’t forget to click on Save on the Teaser page.
We configured the formatter to apply the trim settings on a summary if present. This is exactly what we want.
5. Now if you view the teaser, it’ll be trimmed after 600 characters.
Trim Units
Another useful feature that Smart Trim offers is the ability to change the trim unit. This allows you to trim by the amount of characters or words.
Just select the trim unit when configuring the formatter.
Conclusion
As we’ve seen Smart Trim is a pretty versatile module. It does one thing and one thing well, it trims text. The major functions of the module are, as we learnt, trimming summaries and changing the trim unit. These two things make the module very powerful.