Module Spotlight #1: Smart Trim

Node teasers simply work right? Wrong...

Node teasers are broken and most developers don't even know it, meaning it can be a pain for content creators. If you're building large websites that constantly pump out content, then read on to learn how we fix Drupal's teasers with one simple module...

I think the reason that many experienced Drupal developers don't know exists is due to the fact that they are not full-time content creators. The problems surface when your job is to create great content on a website and your company has a workflow that involves editing and checking your content meticulously. I had been working as a Drupal developer for many years before I was in a meeting, during which the content creators of a large Drupal content platform were complaining about inconsistent display of their article teasers on various parts of the website.

In fact, there were 2 problems my clients were pointing out. The first problem is that when they edited their article to have a nice summary on the blog page, the article did not have the same teaser in the homepage sliders, or elsewhere on the website. Thinking back to this moment, I now realize that I already knew on some level that Drupal's teasers were not reliable enough to use in places where trim length makes or breaks the layout. Like in the homepage slider, which uses a very confined space to show the teaser. To "solve" this, I adopted the practice of using the views module to trim teaser lengths in the slider as well as in other pages that list teasers layouts that required carefully trimmed texts. My clients were unhappy about this; they wanted uniform teasers across their site. More accurately, they wanted control.


What is wrong with Drupal core's teasers?

There are several issues with the text_summary() function that Drupal uses:

  • The function counts HTML tags and their attributes when determining teaser length. This means a 400 character teaser with a 200 character link tag will display a teaser that is only 200 characters long
  • It can generate invalid HTML, breaking up your code without a closing tag
  • Read more links always show, even if there isn't any text to read
  • It provides no control over markup that is allowed in the teaser


Smart Trim to the rescue

Smart Trim introduces a new field formatter for textfields that improves upon the "Summary or Trimmed" formatter built into Drupal 7.

The Smart Trim field formatter gives you the following additional control over display of your teaser:

  1. The trim length excluding HTML
  2. Whether the trim length is measured in characters or words
  3. Appending an optional, configurable suffix at the trim point
  4. Properly displaying an optional "More" link immediately after the trimmed text
  5. Stripping out HTML tags from the field
  6. Preserve certain tags when stripping out HTML

Personally, I especially like the last option. For most teaser displays, I want to strip out all HTML but sometimes I want to preserve emphasis and leave in the <strong> and <em> tags. For some teasers, I may want to leave in the links, but in the featured content slider I want to strip them out. Smart Trim gives me this level of control. I'm looking forward to implement this little module in the next release of our drag and drop Drupal theme.


Node Teasers and Trimming in Drupal 8

There is a very old thread in the issue queue where a discussion about these problems is ongoing. Development for new fixes has moved to Drupal 8.x. Wim Leers points out that perhaps auto-truncating should be removed completely to enforce manual teaser creation. I think this is not a great option because it adds to the content creators' workload and will probably also lead to even less consistent teasers in a website with many contributors.

What is your opinion on node teasers and auto-truncating? Let us know in the comments!