In Drupal 8, if I add a field of type long text with summary and only fill the body, the default handling simply uses the summary field. It should use a trimmed version of the body.

I've prepared a patch which addresses this issue. A simple test is to look at the page source before and after applying the patch when Metatags is enabled. A node which only have the body field completed will have no description field before the patch is applied, and will have the additional content:

<meta property="og:description" content="(Trimmed version of body)" />

afterwards. I would have liked to have used the settings for the field from the view mode that's in use at the time but I couldn't locate a way to find it. This still does better than core though. Core assumes the field is always named body. This patch uses the field name (so field_article_body works in the project for which I've prepared this patch).

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

NigelCunningham created an issue. See original summary.

istavros’s picture

Priority: Normal » Major
Status: Active » Needs review
Issue tags: +symmary or trimmed
FileSize
2.33 KB

Fixed above patch.

s.messaris’s picture

Issue summary: View changes
Nigel Cunningham’s picture

Issue tags: -symmary or trimmed +summary or trimmed
s.messaris’s picture

Status: Needs review » Reviewed & tested by the community

I have been using this in production for about a month without issue. Setting to RTBC.

istavros’s picture

Can this be committed to the next version?

Berdir’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: -summary or trimmed +Needs tests

This is relatively complex and could really use a test if we are adding something like that. But maybe that's not actually needed?

Not too happy about all this special logic. Harcoding the teaser view mode is also not very nice.

These tokens are accessing the raw values, not the formatted output. If it's empty then that's how it is.

Token supports rendering fields with the token view mode, so instead of [node:field_something:summary], you do just [node:field_something] and then enable the token view mode for the affected and configure it to use the summary formatted. You do get the field HTML then, but if used in a place that doesn't allow that then it should be stripped away...

Edit: This will not work with the body field, but if you do use that, then you should be able to use node:summary, which basically does what's suggested here and I guess the code was taken from there.

Nigel Cunningham’s picture

@berdir FWIW, this is essentially a copy-paste of code used elsewhere. Not arguing that it couldn't be improved or unit tested, but that was the basis for the patch and the reason it looks as it does.

More to the point, if this patch is improved, perhaps the other code (I've forgotten the details of what it was, sorry) could be modified too so they're both improved. I'll try to find time to come back to this and do that.

Christopher Riley’s picture

Has this issue ever been resolved. I am seeing the same issue with the [node:summary] token and before patching I figured I would find out the status considering how long it has been here.

joseph.olstad’s picture

joseph.olstad’s picture

Re-rolled patch 2 on the head of 8.x-1.x, gets rid of the paths that had no business being there such as modules/contrib/token
This way folks that use composer to apply patches will be able to use this patch.

joseph.olstad’s picture

Status: Needs work » Needs review
joseph.olstad’s picture

Anyone willing to write a test for this please?

joseph.olstad’s picture

Berdir expressed some concerns with this patch, with that said, we've been using it as there's currently no better solution available.