Traverse context switchers to collect contexts
Following #3540610: Context management for Pattern presets, traverse the context switchers sources in PatternPreset::doGetContexts()
Context switchers sources are:
- [Entity] ➜ [Field]
- [Entity] ➜ Referenced [Entity]
- [Field item] ➜ Referenced [Entity]
More specific entity contexts
If you save a preset with entity context sources, they are available in all entity view displays:
- the displays from the same bundle
- the displays from other bundles of the same entity type:
- the displays from other entity types
When a base or configurable field is not available in the new context, we have a placeholder instead (which is tolerable) but break the display building:
- sometimes it is not rendered
- sometimes it is rendered but not added to the Instance entity
- sometimes it is rendered, added to the Instance entity but we can't publish the display ("publish" = save in content or config entity)
Let's fix that.
Preview
Preview of pattern presets with contextual sources don't work.
Issue fork display_builder-3546964
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:
- 3546964-pdu
changes, plain diff MR !436
- 1.0.x
compare
- 3546964-better-context-management
changes, plain diff MR !431
Comments
Comment #2
pdureau commentedComment #5
mogtofu33 commentedTraverse context switchers
PatternPreset::getContextFromSource()had a@todofor this. A source extendingDerivableContextSourceBasenow also contributes the required context definitions of the derivable context itsderivable_contextsetting names. That covers the three switchers listed, since they share the base.More specific entity contexts
Falls out of the same change.
entity_fieldalone only declares a genericentity, butfield:node:article:bodycarriesentity:nodeplusbundleconstrained toarticle, and those win. A preset built on article fields is offered on an article display, not on node.page, not on user, not in a page layout.The merge needed a rule. Contexts were collected with
array_merge, so the last source won a key and a preset mixing a field source withentity_linklost its entity type to the generic one. They now merge key by key with the narrower data type winning whatever the order, equal precision keeping the first. So one bundle per preset, not two.Since a preset can no longer be offered where its fields do not exist, the three symptoms in the summary no longer have a way to start. I did not reproduce them separately before the fix.
Preview
Nothing handed
ApiPreviewController::getPresetPreview()an entity, so every field slot rendered empty. It now reads the entity type and bundle from the preset's own context definitions and stands a sample entity in their place withui_patterns.sample_entity_generator.That needed the
in_previewmarking: without it the comment field's form loads the sample node by its NULL ID and asserts out, taking the whole preview down.markSampleEntity()was private onEntityView, so it moves toDisplayBuilderHelpersand both call it.Follow-up
calculateDependencies()does not walk nested sources, so a preset readingfield_imagedepends onui_patternsand the theme only, never on the field. Deleting the field leaves the preset in place rendering nothing. That predates this issue and affects every preset, so it wants its own. Happy to open it.Comment #6
pdureau commentedCode review
The comeback of
DisplayBuilderHelpers::markSampleEntity();) But not a big deal, let's move on.Feature review
EntityView
Now, a Bundle context is added each time, which is ✅ OK to prevent a configurable field to land in a bundle where it is not available.
⚠️ Unfortunately preventing a base field to be shared between bundle of a same entity type. But this is an issue UI Patterns side where base field are returning
bundlecontext:❌ Adding a single field as a pattern breaks:
✅ Thanks to #3626043: Config panel loses source settings on AJAX actions and saves raw values, i was able to check if a referenced entity is not overriding the
entityandbundlecontexts.Views
But adding a pattern form ViewDisplay, with a context-aware source in a SDC slot doesn't work:
::getContextDefinitions()return an empty arrayI will propose a patch in a new MR.
Comment #8
pdureau commentedHi @mogtofu33,
I have pushed a commit in a new MR which address:
Pipeline is red because of #3625969: Twig's 3.30 TypeError: Twig\Runtime\EscaperRuntime::escape(): Argument #4 ($autoescape) must be of type bool, null given.
However, about Adding a single field as a pattern breaks", I am not finding the fix. Can you have a look?
Comment #9
pdureau commentedI have found the fix
Comment #10
pdureau commentedTo review.
I will rebase #3617547: Make pattern presets buildable with this one to continue my review there.
Comment #12
mogtofu33 commented