Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Follow-up to #2862083: [META] Improve paragraphs summary
Problem/Motivation
We currently support a few field types in getSummary. Modules that implement other field types could use a hook to handle their types.
Proposed resolution
Let's first see how much does current solution already cover and if we see that there are more use cases that we can't handle in Paragraphs, add the hook.
Remaining tasks
User interface changes
API changes
Data model changes
Comment | File | Size | Author |
---|---|---|---|
#46 | 2868252-paragraph-summary-alter-46.patch | 6.62 KB | gmercer |
#42 | 2868252-paragraph-summary-alter-42.patch | 6.52 KB | Andrew Robinson |
#38 | 2868252-paragraph-summary-alter-38.patch | 6.52 KB | a.dmitriiev |
| |||
#38 | interdiff_33_38.txt | 7.99 KB | a.dmitriiev |
#33 | interdiff.txt | 352 bytes | Lal_ |
Comments
Comment #2
Primsi CreditAttribution: Primsi at MD Systems GmbH for MD Systems GmbH commentedComment #3
thelmer CreditAttribution: thelmer at Adapt commentedI have created a patch where the getSummary dispatches two different events :
ParagraphSummaryExcludeFieldsEvent
Dispatched in the beginning of getSummary, subscribers can add fields excluded from the summary
ParagraphSummaryAlterEvent
Dispatched after data is collected from the fields, subscriber can prepend/append strings to the summary array
The subscriber could do something like this:
Comment #4
thelmer CreditAttribution: thelmer at Adapt commentedNB: there are some uncommented code for list_string fields.
Currently only the first value (not the label) are being outputted.
I will work on some code so all labels are outputted.
Comment #5
BerdirComment #6
Ginovski CreditAttribution: Ginovski at MD Systems GmbH commentedNeeds reroll, summary method is changed from #2852001: Consider container multivalue children for closed summary
Comment #7
Ginovski CreditAttribution: Ginovski at MD Systems GmbH commentedDifferent approach with the alter hook.
Comment #8
miro_dietikerIMHO this doesn't help too much.
We are updating here the summary that then is concatenated.
We have other issues that will extend the summary with two icon areas per summary. Those icons should be similarly alterable by the hook. The collapsed plain text summary is just one summary component.
Comment #9
miro_dietikerSee also newly related task.
Comment #10
thelmer CreditAttribution: thelmer at Adapt commentedPatch rerolled
Comment #11
sam.spinoy@gmail.com CreditAttribution: sam.spinoy@gmail.com at Adapt commentedPatch for version 1.2
Comment #12
miro_dietikerComment #14
kevineinarsson CreditAttribution: kevineinarsson as a volunteer commentedRerolled #7 to keep the ball rolling. Why should this be event driven? Sorry if it's an ignorant question, but I don't see why this shouldn't just be an alter
Comment #15
rwohlebI think the alter hook would be a lot more useful if the $summary array was keyed by field id. As it is now we can add summaries, but it doesn't give us an easy way to selectively strip certain fields.
Additionally, having the $options array that is passed to the getSummary method would be useful. It would allow any additions to respect the options.
Comment #16
BerdirThat's definitely true but also challenging, considering that we generate the summary recursively in some cases. We'd need to either switch to a nested structure or introduce a solution for having unique flat keys. Not sure yet.
Comment #17
rwohlebAfter working with this over the weekend, I'd definitely vote for the nested structure. I was simplifying/adjusting output to be more user-friendly for editors and it would have made parts of it a lot easier. The order of fields is particularly an issue.
Additionally, the forced implode(',', $summary) just didn't work for what I was trying to do. I eventually had to combine everything the way I wanted and inserted it as a single entry returned by the hook to avoid that. It got me thinking that there should probably be a new template for summaries, and the implode behavior could be handled there.
Comment #18
sam.spinoy@gmail.com CreditAttribution: sam.spinoy@gmail.com at Adapt commentedPatch from #3 for version 1.3
Comment #19
sam.spinoy@gmail.com CreditAttribution: sam.spinoy@gmail.com at Adapt commentedCorrecting patch
Comment #21
dpacassiThe patch provided in #19 doesn't fire the exclude fields event.
I've fixed this and also a few coding standard issues.
Comment #22
dpacassiHere's the patch :D
Comment #23
BerdirThe internal structure of the summary is still in flux and evolving, we've now changed it to an array of content and behavior items, so we're a step closer. But to make any sense of it being alterable, we also need named keys and a stable structure.
People who used this patch might also want to check if the new summary template and corresponding preprocess helps them do what they want to do.
Comment #24
miro_dietikerYeah, we should put those keys asap.
What could still add one last(?) change in the summary structure is #2932406: Display field image thumbnail in the paragraph summary
Comment #25
miro_dietikerPromoting this to major so we will look into it when we review the Roadmap. :-)
Comment #26
johnchqueComment #27
attiks CreditAttribution: attiks at Attiks for United Nations commentedNew approach, calls the alter inside the loop and passes field name and field type, so implementing a subscriber will be easier
Example subscriber https://www.drupal.org/project/viewsreference/issues/3137849#comment-136...
Comment #28
attiks CreditAttribution: attiks at Attiks for United Nations commentedBetter event name
Comment #29
sam.spinoy@gmail.com CreditAttribution: sam.spinoy@gmail.com at Adapt commentedI've reworked the patch from #19 for the latest paragraphs version.
Comment #30
sam.spinoy@gmail.com CreditAttribution: sam.spinoy@gmail.com at Adapt commentedComment #31
sam.spinoy@gmail.com CreditAttribution: sam.spinoy@gmail.com at Adapt commentedsomething went wrong with the patch there, here is the correct version
Comment #32
Lal_Alter also means completely changing the summary, currently there aren't any way to remove the existing summary data and replace it with something else. Lets add that also..
Comment #33
Lal_Comment #34
bvoynickComparing patch 28 vs patch 29, we seem to have lost the actual trigger for the fields exclusion event.
Comment #35
LRoelsPatch #33 seems to work and include the needed functionalities.
Just tested this on my project and worked without issues.
Tested against 8.x-1.12.
Comment #36
pingevt CreditAttribution: pingevt commentedPatch in #33 seems to work for me against 8.x-1.13.
My use case is to give more information around a "From Library" paragraph. For now just prepending the Paragraph bundle of the item in the Library.
Comment #37
a.dmitriiev CreditAttribution: a.dmitriiev as a volunteer and at 1xINTERNET for 1xINTERNET commentedI wonder why the summary is only limited to
content
in patch #33, there is alsobehaviors
and maybe potentially more array properties of summary. I think it is better to allow modifying the summary array completely if needed, this is the main purpose of alter.Comment #38
a.dmitriiev CreditAttribution: a.dmitriiev as a volunteer and at 1xINTERNET for 1xINTERNET commentedHere is modified patch to allow alterations to any parts of summary
Comment #39
HitchShockBoth patches (#38 and #33) work for me on my project. +1 RTBC
Comment #40
Andrew Robinson CreditAttribution: Andrew Robinson at Versantus commentedI believe these patches fail in Drupal 10 sites with the error:
Uncaught Error: Class "Symfony\Component\EventDispatcher\Event" not found in modules/contrib/paragraphs/src/Event/ParagraphSummaryAlterEvent.php:4
According to
vendor/symfony/event-dispatcher/CHANGELOG.md
, in Symfony 5.xThe `Event` class has been removed in favor of `Symfony\Contracts\EventDispatcher\Event`.
Based on this I think the use statement at the top of
src/Event/ParagraphSummaryAlterEvent.php
should be updatedfrom
use Symfony\Component\EventDispatcher\Event;
to
use Symfony\Contracts\EventDispatcher\Event;
for sites that are based on Symfony 5 or above.
Comment #41
a.dmitriiev CreditAttribution: a.dmitriiev as a volunteer and at 1xINTERNET for 1xINTERNET commented@vladigor@gmail.com In patch #38 this class is not used. It uses the class
Drupal\Component\EventDispatcher\Event
according to change record: https://www.drupal.org/node/3159012Comment #42
Andrew Robinson CreditAttribution: Andrew Robinson at Versantus commentedThanks @a.dmitriiev.
I've switched over to patch #38 on my Drupal 10.1.0 site using Paragraphs 1.15.0.
When I try to edit a page that contains paragraphs I get the following error:
It appears that in Drupal 10.1 the event_dispatcher service uses the dispatch function from ContainerAwareEventDispatcher.php, which expects $event as the first parameter.
Ref. https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Component%21Even...
I've attached an updated version of patch #38 that switches around the parameters in the two calls to the dispatch function.
Comment #43
LRoelsChanging the status to needs review for now. Seems wrong to keep it on RTBC.
Comment #44
LiamPower CreditAttribution: LiamPower at Reading Room commented#42 is working for me with Drupal 10.1.5 and Paragraphs 8.x-1.16
Comment #45
a.dmitriiev CreditAttribution: a.dmitriiev as a volunteer and at 1xINTERNET for 1xINTERNET commented#42 is working for me as well for Drupal 10
Comment #46
gmercer CreditAttribution: gmercer as a volunteer and at Stanford University commentedRe-rolled #42 patch to check for empty of summary[$type] on appendTextToSummary() and prependTextToSummary() in src/Event/ParagraphSummaryAlterEvent.php