Problem/Motivation
My scenario involves the placement of a views block in layout builder for a content type. During the rendering process, section and section component information is not available once the views block build method is called. I need the ability to inject the section layout ID and the component region values into the views result row/item before it reaches an implementation of hook_preprocess_node().
Proposed resolution
After much digging, I found the SECTION_COMPONENT_BUILD_RENDER_ARRAY event. Using that event and a new one for SECTION_BUILD_RENDER_ARRAY, an event listener can be used to store the information needed in drupal_static() that can be accessed in the implementation of hook_preprocess_node().
Remaining tasks
Add tests.
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | 3101655-section_event-11-interdiff.txt | 1.28 KB | tim.plunkett |
| #11 | 3101655-section_event-11.patch | 11.16 KB | tim.plunkett |
| #9 | 3101655-section_event-9-interdiff.txt | 821 bytes | tim.plunkett |
| #9 | 3101655-section_event-9.patch | 11.11 KB | tim.plunkett |
| #7 | 3101655-section_event-7-interdiff.txt | 5.81 KB | tim.plunkett |
Comments
Comment #2
weekbeforenextThis is a patch that applies cleanly to 8.7.x.
Comment #3
weekbeforenextComment #4
tim.plunkettWhile the implementation you describe above sounds particularly terrifying, it seems absolutely correct to have Section mirror SectionComponent in their toRenderArray() approaches.
Similar to how
\Drupal\layout_builder\SectionComponent::toRenderArray()consists solely of the dispatched event, this current implementation should be moved to an event subscriber with a priority of 100. That will allow custom event subscribers to occur before or after this default code, with the non-explicit priority code running *after* (as a sort of alter).Note also that
\Drupal\Tests\layout_builder\Unit\SectionRenderTestshould be updated/adapted to reflect the changes in rendering flow. See\Drupal\Tests\layout_builder\Unit\SectionComponentTestfor inspiration.Bumping to 8.9.x, but this might be a 9.1.x issue, I'm not sure of the timing/rules with that currently.
Comment #5
weekbeforenextThanks for taking a look so quickly.
I updated the Proposed solution to replace "tempstore" with "drupal_static()" which hopefully seems a little less terrifying. :) It's working which is hopeful.
I think I understand your suggested changes. I gave it a shot yesterday but broke things and backed out of it. I'll keep this on my radar and try again.
I joined the #layouts channel on Drupal Slack, so feel free to ping me if you want to chat about potentially better solutions for this sort of issue.
Comment #7
tim.plunkettHere's a patch that implements #4.
It still needs explicit test coverage to prove another subscriber can affect the output. The previous patch did not call
$event->getBuild()Note: The order of arguments to dispatch() changed in D9, so this will not work as-is for D8.
Comment #9
tim.plunkettThat's what happens when you only run the unit tests before posting...
Comment #11
tim.plunkettI knew I was missing something, passing
$regionsto setBuild() did not feel right.Comment #12
neograph734@tim.plunkett, how did you manage to get this test to pass? I am getting the following error on my site:
Which makes sense because your patch swapped the event name and the event.
+ $this->eventDispatcher()->dispatch($event, LayoutBuilderEvents::SECTION_BUILD_RENDER_ARRAY);In SectionComponent.php we see this inversed:
$this->eventDispatcher()->dispatch(LayoutBuilderEvents::SECTION_COMPONENT_BUILD_RENDER_ARRAY, $event);Manually swapping them back makes the error go away.
This works great in conjunction with #3044117: Add getter for layout object in Layout Builder's ConfigureSectionForm. Allowing one to easily set third party settings by altering the configuration form and then having access to them in this event. Many thanks for the effort.
Comment #13
tim.plunkettThis patch is written for 9.1, the development version of Drupal. As a feature request, it will not be backported to D8.
Comment #14
neograph734Ah apologies, I was under the assumption that I had spun up a 9.1 site, but it appeared to be 9.0.6 instead. I'll give it another shot later this week.
Comment #15
neograph734I did spin up a 9.1 instance, but patched another site in the wrong folder... It works and for me it is RTBC :)
Comment #17
claudiu.cristea#3062862: Third party should be allowed to alter the section render array is doing the same but it's older. Closing this as duplicate.