Contexts without data not visible in block context selector

Created on 22 January 2021, about 4 years ago
Updated 15 October 2024, 4 months ago

Problem/Motivation

Contexts without data not visible in block context selector

Steps to reproduce

Create new node with title "Super".
Create taxonomy term with name "Super".
Create custom context provider. Let it return term with name = title of current node.
Part of the code:

  public function getAvailableContexts() {
    $context = EntityContext::fromEntityTypeId('taxonomy_term', $this->t('Same name term'));
    return ['taxonomy_term' => $context];
  }

Create taxonomy term view with a block display.
Add contextual parameter "Term ID" and save the view.
Go to node type display settings.
Add this block to layout builder.
Click to block settings. There is no new created context provider.
But if change code to:

    $context = EntityContext::fromEntity(Term::create([...]), $this->t('Same name term'));
    return ['taxonomy_term' => $context];

context became visible.

Look at \Drupal\layout_builder\Context\LayoutBuilderContextTrait::getAvailableContexts.

Proposed resolution

All context providers should be visible (with and without data).
As in getAvailableContexts() written:

* When a context aware plugin is being configured, the configuration UI must
* know which named contexts are potentially available, but does not care
* about the value, since the value can be different for each request, and
* might not be available at all during the configuration UI's request.

But I don't know how we can get sample entity in context provider during preview.

πŸ› Bug report
Status

Needs review

Version

11.0 πŸ”₯

Component

layout_builder.module

Created by

πŸ‡·πŸ‡ΈSerbia super_romeo Belgrade

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ͺπŸ‡ΈSpain akalam

    I come into this issue in the following scenario using the "group" module.
    We are using the group content entity to render the full view mode of the node, and we are using layout builder on the nodes. The problem is with the blocks that have group context, because the run time contexts are not selectable on the block config form because we don't have the context of the group populated (since we are on the layout builder of a node), but the context of the group will be populated properly when viewing the node because it's gonna be rendered within the group content entity, and therefore accessed within a route with the group context.

    The general idea is that not having the context populated on layout builder doesn't mean that the context won't be populated when viewing that entity. Filtering the contexts based on that precept will make layout builder less functional compared to the block layout, where no contexts are populated and they are not filtered.

    I'm uploading a simple patch just removing that filtering so all contexts will be available to select.

  • πŸ‡ͺπŸ‡ΈSpain akalam

    Even when the previous patch was avoiding layout builder to filter some contexts, many context definitions are not complete (for example they don't have label). For this reason when having different contexts for the same entity type, they use the entity type name instead of the context label.

    Calling the method getAvailableContexts() instead is doing the trick, and now the labels are properly setup. Among that, is more performative to call just the available context and not the runtime contexts if we are not going to take care if the context is populated or not.

  • πŸ‡ͺπŸ‡ΈSpain akalam

    The previous patches had a side effect: When you are trying to add a block on a node, the node fields for all content types are available which is not nice. On the other hand, according to the name of the method "getPopulatedContexts()", make sense how it works, using the runtime context instead of the available contexts.

    Therefore I've changed the approach, and I'm getting the available context only on the \Drupal\layout_builder\Form\ConfigureBlockFormBase::doBuildForm

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    So patches should be in MRs now

    And will need test coverage

    Thanks!

  • πŸ‡ͺπŸ‡ΈSpain akalam

    I've found an issue when trying to add a entity field block on layout builder. Seems like the best solution is to filter for the contexts that are both required and empty, so non required contexts are still available.
    I'm adding a MR instead of a static patch.

  • Pipeline finished with Failed
    4 months ago
    Total: 702s
    #318170
  • Pipeline finished with Failed
    4 months ago
    Total: 433s
    #318214
  • Pipeline finished with Canceled
    4 months ago
    Total: 91s
    #318231
  • Pipeline finished with Failed
    4 months ago
    Total: 595s
    #318232
  • Pipeline finished with Failed
    4 months ago
    Total: 699s
    #319003
  • Pipeline finished with Failed
    4 months ago
    Total: 104s
    #319031
  • Pipeline finished with Success
    4 months ago
    Total: 390s
    #319039
  • Pipeline finished with Failed
    4 months ago
    Total: 538s
    #319240
Production build 0.71.5 2024