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).
Issue fork token-2924873
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
istavros commentedFixed above patch.
Comment #3
s.messaris commentedComment #4
nigelcunningham commentedComment #5
s.messaris commentedI have been using this in production for about a month without issue. Setting to RTBC.
Comment #6
istavros commentedCan this be committed to the next version?
Comment #7
berdirThis 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.
Comment #8
nigelcunningham commented@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.
Comment #9
Christopher Riley commentedHas 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.
Comment #10
joseph.olstadComment #11
joseph.olstadRe-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/tokenThis way folks that use composer to apply patches will be able to use this patch.
Comment #12
joseph.olstadComment #13
joseph.olstadAnyone willing to write a test for this please?
Comment #14
joseph.olstadBerdir expressed some concerns with this patch, with that said, we've been using it as there's currently no better solution available.
Comment #15
2dareis2do commentedThis patch kind of works for me. I have a field name field_feed_item_description which is of type "Text (formatted, long, with summary)" The logic did not apply with the following conditions:
isset($field_item->$property_name) && $field_item instanceOf TextWithSummaryItemactually i discovered in my case that field_item is
$field_item instanceOf Drupal\text\Plugin\Field\FieldType\TextItemBaseSo it might be better to reference this parent if that used by more text formats.
Also
isset($field_item->$property_name)is sort of like saying
isset($field_item->summary)in web/core/modules/node/node.tokens.inc
we have the following logic:
So my feeling is $field_item->summary is not always available and should not be a condition.
Comment #16
2dareis2do commentedComment #17
yeniatencio commentedRe-rolled patch 11 as there is a new release.
Comment #18
yeniatencio commentedAdded and additional validation to avoid `Call to a member function getComponent() on null` issues.
Comment #19
2dareis2do commentedThis broke for me when upgrading token 1.15.0
I have tried both #17 and #18 patches and none worked for me.
Please try this patch and see if it works for you or not.
Screenshot showing token output also shown
Comment #20
damienmckennaThis is related: #2854930: [node:summary] token's value get trimmed incorrectly
Comment #21
damienmckennaBack to needing tests.
Comment #22
joseph.olstad