6

I have a view that is supposed to display a body field and trim it to under 600 characters. Most of the time it does that just fine, however sometimes it will trim down to under 100 characters for no apparent reason. Usually it is more likely if the text has more html tags than just simple p/ul/li etc (so strong, em, h4, etc). However I don't see why that should shrink how much it trims by that much. As an example, the following example text (obscured and shown as the source html so you can see everything that is happening) will only show the first p tag text at 600 characters, but will show everything if the character limit is increased to 611.

<p>
    <strong>Finding Connection in the Midst of Holiday Loneliness</strong><br>
    <strong>Dtr Hjkkdj Ertlkj ani Kjiyny Wiyhnn &amp; OLI</strong>
</p>
<p>
    As the holidays roll in, for many it can be a time of laughter and joy, with family reunions, clinking of glasses, traditional foods, twinkling lights, and festive decorations; but it’s not as joyous for everyone at all times. The holiday season can also be a reminder of what, or who, is missing. It's hard when the world seems to be celebrating, and you're trying to find your footing in a state of loneliness that the holiday cheer often can’t quite reach.
</p>

Can anyone explain to me why this would trim to only the first paragraph tag at 600 trim limit, but display all of it at 611 trim limit? and how I can fix it so that the trim works as I would expect it to?

1
  • 2
    It'll probably come down to the text filter - at a guess, it's counting with HTML tags included, cutting off at 600 at which point there's broken HTML (missing the </p>), and the correction chops off everything off after the last valid HTML (first paragraph)
    – Clive
    Dec 22, 2023 at 1:10

1 Answer 1

6

This is the expected behaviour, insofar as it's what the text_summary() function does to the HTML. From the function comments:

[...] we generate the summary automatically, trying to end it at a sensible place such as the end of a paragraph, a line break, or the end of a sentence (in that order of preference).

It first truncates the input to the maximum length, then breaks it into HTML paragraphs; if it finds a complete paragraph within the limit, and the next paragraph is incomplete or above the limit, it delivers the first paragraph only, regardless of the remaining number of valid characters.

The Smart Trim module has an option to strip HTML, that might get you better results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.