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)

Issue fork drupal-3154986

Command icon 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

cyb.tachyon created an issue. See original summary.

cyb_tachyon’s picture

Title: Support context aware block plugins » Support context aware block plugin form configuration

Updated the title to make it clearer (added "form configuration").

cyb_tachyon’s picture

cyb_tachyon’s picture

Title: Support context aware block plugin form configuration » Support context aware block plugin creation
Issue summary: View changes

Additional clarity to the issue title and description.

tim.plunkett’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 3: 3154986-3-lb-context-block-plugin-form.diff, failed testing. View results

cyb_tachyon’s picture

Ah - there's a redundant set in \Drupal\Core\Block\BlockBase::__construct@59 that 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.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

yivanov’s picture

I 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:

*   context_definitions = {
 *     "node" = @ContextDefinition("entity:node",
 *       label = @Translation("Node"),
 *       description = @Translation("The node"),
 *     ),
 *   }

the problem is that using Layout builder the context value is not passed and I get MissingValueContextException:

MissingValueContextException: Required contexts without a value: node

I 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.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.