Problem/Motivation
After #2921626: Add proper context-awareness to Layout Builder we have gathered contexts available for Layout Builder forms via $form_state->getTemporaryValue('gathered_contexts'), but for some reason (let me know if there was a good one) it is not passed into Block Plugins where it would be expected on creation.
Proposed resolution
Simply pass the result of \Drupal\layout_builder\Form\ConfigureBlockFormBase::doBuildForm's call to $this->getAvailableContext($section_storage); to $component->getPlugin() as context.
Unfortunately, this exposes two glaring flaws in the plugin ecosystem:
1. Context Mapping is built around arbitrary input and output strings, and does not rely on Context identifiers to allow context sharing easily between plugins, so every time you pass or applyContextMapping() to a new plugin you must reverse the mapping by calling $this->contextHandler()->getMatchingContexts() for the entirety of $component->getPlugin()->getContextDefinitions().
2. Multiple context providers can exist for the same context definition type, and it's not clear which one would be correct.
Here's a rewrite that effects a workaround (patch incoming):
\Drupal\layout_builder\Form\ConfigureBlockFormBase @ 165 $contexts = $this->getAvailableContexts($section_storage); $context_mapping = []; foreach ($component->getPlugin()->getContextDefinitions() as $context_name => $context_definition) { $matching_contexts = $this->contextHandler() ->getMatchingContexts($contexts, $context_definition); $context_mapping[$context_name] = array_pop($matching_contexts); } $this->block = $component->getPlugin($context_mapping); $form_state->setTemporaryValue('gathered_contexts', $contexts);
Notes:
#3018782: Remove extraneous context mapping of layout_builder.entity would be slightly helpful here, but doesn't resolve the root of the problem.
Remaining tasks
(reviews needed, tests to be written or run, documentation to be written, etc.)
User interface changes
None.
API changes
None.
Data model changes
None.
Release notes snippet
(Major and critical issues should have a snippet that can be pulled into the release notes when a release is created that includes the fix)
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | 3154986-3-lb-context-block-plugin-form.diff | 1.12 KB | cyb_tachyon |
Issue fork drupal-3154986
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
cyb_tachyon commentedUpdated the title to make it clearer (added "form configuration").
Comment #3
cyb_tachyon commentedComment #4
cyb_tachyon commentedAdditional clarity to the issue title and description.
Comment #5
tim.plunkettComment #7
cyb_tachyon commentedAh - there's a redundant set in
\Drupal\Core\Block\BlockBase::__construct@59that can cause issues with block storage.There's also the above 5 test failures, some which may be caused by the redundant set. I'll look into what other bugs have been exposed by the patch and see if I can include fixes.
Comment #11
yivanov commentedI landed here while searching for a problem I have with custom Block that requires the current node context.
Usually in the annotation of the plugin block we need to define:
the problem is that using Layout builder the context value is not passed and I get MissingValueContextException:
MissingValueContextException: Required contexts without a value: nodeI also found another issue thread similar to this, but the patch there doesn't seem to fix the issue -
https://www.drupal.org/project/drupal/issues/3099968
Not quite sure if this thread here is the right place to report this or a new one is required though.