Problem/Motivation
When no summary is specified, token [node:summary] uses formatter settings of teaser
view mode in order to generate trimmed version of body field. If 'teaser' view mode is not configured, default text_summary_or_trimmed
trim lengths will be used (600 characters atm)
However, existing code does not check what formatter is being used. Instead, it checks for trim_length
setting only.
When some other field formatter (like Smart trim) is being used for teasers, summary is being generated incorrectly. This happens because Smart trim uses exactly the same trim_length
setting, but it can be words, not characters.
In particular case, smart trim is configured to use 30 words. Token [node:summary] gives 30 characters, instead of default 600.
Proposed resolution
Add extra check for the formatter that is being used for teaser view mode for body field.
Remaining tasks
- Review patch
User interface changes
No
API changes
No
Data model changes
No
Comment | File | Size | Author |
---|---|---|---|
#53 | interdiff_48-53.txt | 1.63 KB | ravi.shankar |
#53 | 2854930-53.patch | 12.05 KB | ravi.shankar |
#48 | 2854930-48.patch | 12.06 KB | Suresh Prabhu Parkala |
#43 | interdiff-39-43.txt | 673 bytes | naresh_bavaskar |
#43 | 2854930-43.patch | 12.04 KB | naresh_bavaskar |
Comments
Comment #2
Andrew147 CreditAttribution: Andrew147 commentedComment #4
Andrew147 CreditAttribution: Andrew147 commentedFixed incorrect paths.
Comment #5
Taran2LUpdating summary.
Probably this will not end up in 8.2.x; moving to 8.3.x
Comment #6
gnugetFirst, needs to be fixed on 8.4.x and after can be cherry-picked to 8.3.x.
Comment #7
Taran2LComment #8
gnugetlooks great but this needs tests.
Thanks.
Comment #9
Taran2LAdded tests
Comment #11
Taran2LHuh, DrupalCI PHP does not have mbstring extension enabled causing new tests to fail.
Unicode::substr()
expects string input, and not a\Drupal\Component\Render\MarkupInterface
Also fixed coding standards issues
Comment #12
gnugetHi! Taran2L
It looks great, just a few comments:
We need to keep the changes in the scope and at the minimum (so the core commiter can review this easier and quicker) let's revert this change, and all the
$this->account
changes, if you think to we really need these changes let's create a follow-up.The same as above, not sure if it is really necessary change the comment.
The same as above, There is already an issue to replace all the format_strings methods (see #2228741: Replace calls to format_string() with Drupal\Component\Render\FormattableMarkup), so let's revert this as well.
. 'Fusce tempor tincidunt lorem vitae mollis.'; // 828
Put the comment int is own line above the variable please (per https://www.drupal.org/docs/develop/coding-standards/api-documentation-a...).
This should also trims the string, no? I mean,
the text_summary_or_trimmed
just extends thetext_trimmed
formatter:So for this specific formatter the trim should work as well, at least for these two should work.
Also the
$expected_trimmed_to_600
is an invalid markup there is a</p>
missing at the end of the variable.We still need to test that the value is trimmed correctly if the formater is text_summary_or_trimmed or text_trimmed and test if the trim_length is ignored if a different formated is used.
Why did you deleted this part? this is where actually all the tokens are tested, now we are just testing the summary. can we please revert this part?
Great work!
Thanks.
Comment #13
Taran2LHi @gnuget,
Thanks for the review.
However I can't agree with couple items.
1 and 3 - this is minor refactoring. I don't think spamming drupal issue queue is a good thing. Reviewing multiple tickets instead of a single one takes less time
2 - test method description should include what is being tested, not how we do. Also check a new test method, this naming has a lot of sense IMO
4 - ok
5 - please take a look how [node:summary] token is being generated. It uses summary or trimmed version of body
6 - please apply patch and check how it works or pay more attention reviewing it in browser. It does test all the tokens and node summary
Thanks
Comment #14
gnuget1,2,3 I just wanted to help here, I've been asked to revert that types of changes before in other core issues (especially 3), if you don't want to it's ok, in the end, who decide if this is or not in the scope is the core commiter, as I said I just want to help :-)
5. I can see now that the text_summary_or_trimmed is being tested thanks, but I have doubts about this:
Not sure if I'm not understanding something but here you are using the
text_trimmed
formatter? becauseDrupal\text\Plugin\Field\FieldFormatter\TextSummaryOrTrimmedFormatter
is empty and just extends TextTrimmedFormatter so that is why I was wondering if this formatter should behave as TextSummaryOrTrimmedFormatter and it should take in account the `trim_length` value (42) and trim the summary because right now you are testing to it's length should be 600.So basically what I'm saying is that we might want to include text_trimmed in the node.tokens.inc file:
What do you think?
6. My bad, sorry!
Thanks!
Comment #15
Taran2LHi @gnuget,
I might have been a little bit too sharp in the previous comment . Sorry.
I can revert 1,2,3 - but let's have some other opinion on them
5 - At the moment trim length defaults to setting of `text_summary_or_trimmed formatter` (not text_trimmed). The values are both 600 now, nut could change in the future. Allowing both formatters creates an inconsistency with a fallback value. What do you think?
New patch addressing 4 attached
Comment #17
gnugetBecause of: #2776975: March 3, 2017: Convert core to array syntax coding standards for Drupal 8.3.x RC phase #15 doesn't apply anymore.
Here a straight reroll.
Comment #18
gnugetThe
trim_length
used on text_summary_or_trimmed and in text_trimmed is the same (literally both use the same variable) If you change one the other is changed as well because is the same.So right now if the user is using the formatted "trimmed" in the teaser view mode and set the "trimmed limit" to 250 the
summary token
will use this value and I think that is the expected behavior.With the suggested fix on #4 this won't work in this way anymore, the summary token won't use the trim_length if the user is using the "trimmed" formatted and will use the default value (600), and the only way to set a different value will be forcing the user to use the "text_summary_or_trimmed" formatted in the body in the teaser view mode.
So I think we should update node.tokens.inc with:
An alternative (not sure how easy would be to do it or even if it is a good idea) is making the
[node:summary]
token use the same formatter and not just the trim_length value, in this way we can at least guarantee that the token will contain exactly the same value as the body in the teaser view mode, and the expected behaviour using theSmart trim
would be having a teaser with a specific number of words.Another alternative would be make the
Smart trim
module stop using the trim_lenght and use an a different setting variable.At this point not sure what is the best thing to do. :-/
Comment #19
Taran2LThanks for the reroll.
Add
Trimmed
as a second option for [node:summary] token.This might be a good idea. Could be a follow-up issue.
Comment #20
gnugetLooks great, just i have two doubts.
is there a case when
$display_options
exists but$display_options['settings']['trim_length'];
doesn't? I mean this is never going to fail? maybe using a custom formatter which doesn't rely on trim_lenght will make it fail?Html::escape('<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi diam dui, finibus et purus ac, elementum pretium augue.');
Before trim the output shouldn't remove tags or do something to avoid having an empty
tag? even if it is escaped it will looks weird in the summary token no?
Thanks!
Comment #21
Taran2LWe explicitly expect
text_trimmed
ortext_summary_or_trimmed
formatter only. Both havetrim_length
. I think extra check is unnecessary.This is interesting question. I believe this test runs in a very limited environment with only a few modules enabled, which makes the output not valid html (seems like html corrector filter is not being invoked)
Comment #22
Taran2LIn
text.module
:And by default
plain_text
filter format does not have html corrector. Seefilter.format.plain_text.yml
Comment #23
gnugetIt is sometimes hard to me see this type of things. Thanks for your explanation.
#22 agree as well.
I haven't any other doubt, to me this is RTBC, we just need more opinions about 12.1, 12.2 and 12.3.
Thanks!
Comment #24
Taran2LThis is quite interesting comment to be honest.
[node:summary]
uses this, when no summary is provided:and processed value contains already processed text, and then
text_summary()
applies filters for the second time!Will update patch soon!
Comment #25
Taran2LNew patch that addresses issue with double escaping.
Comment #26
Taran2LMinor naming fixes
Comment #33
Henry Tran CreditAttribution: Henry Tran at Deloitte Digital commentedI've re-rolled to 8.7.x.
Comment #35
MrPaulDriver CreditAttribution: MrPaulDriver commentedUnfortunately the latest patch does not appear compatible with 8.8.x. Any chance of a re-roll?
Given that Smart Trim has over 50,000 installs and that [node:summary] is used for meta descriptions by the Metatag module, might I suggest this core bug could be adversely affecting the SEO of many thousands of Drupal sites.
Comment #36
ravi.shankar CreditAttribution: ravi.shankar at OpenSense Labs commentedI have done the patch on Drupal 8.9.x
I have also tried to fix the failed tests.
Comment #38
ravi.shankar CreditAttribution: ravi.shankar at OpenSense Labs commentedI have again uploaded a patch.
Comment #40
Henry Tran CreditAttribution: Henry Tran at Deloitte Digital commentedI've re roll to 8.9.1.
Comment #42
naresh_bavaskarComment #43
naresh_bavaskarFixed the failed test cases and re-rolled in 9.1.x
Comment #45
gregglesJust tested this out on Drupal 8.9 and it solved a bug I was running into.
I'm going to remove the "needs tests" tag here since there seem to be tests now, but if the tests are insufficient in some way perhaps that tag should come back.
Comment #47
darvanenNeeds work for failed patch.
Will prioritise this with assistance.
Comment #48
Suresh Prabhu Parkala CreditAttribution: Suresh Prabhu Parkala at Specbee commentedA re-rolled patch against 9.3.x. Please review.
Comment #49
darvanenThis patch is doing way more than outlined in the IS.
All of the feedback in #12 needs to be addressed, and the "Lorem Ipsum" text which is causing CSpell failure must be replaced with something else (make sure not to use anything under copyright).
Comment #52
JSchref CreditAttribution: JSchref commented#48 appears to work for 9.4.3
Comment #53
ravi.shankar CreditAttribution: ravi.shankar at OpenSense Labs commentedAddressed Drupal CS issues of patch #48.
There are lots of unknown words being used in
core/modules/node/tests/src/Kernel/NodeTokenReplaceTest.php
file.So can we use cspell:ignore words or we will add these in the dictionary?
Comment #54
darvanenPer #49, those aren't English words and shouldn't be added to the dictionary, we need a piece of text other than Lorem Ipsum in there.