Layout Builder not working with views block contextual filters

Created on 24 December 2021, almost 3 years ago
Updated 12 September 2023, about 1 year ago

Problem/Motivation

Layout Builder missing ability to assign required context

Steps to reproduce

Add views block, or any block that relies on context to display content, to a Layout Builder section. When adding, you no longer have the ability to select the context to pass on to the block.

πŸ› Bug report
Status

Closed: works as designed

Version

9.3

Component
Layout builderΒ  β†’

Last updated about 5 hours ago

Created by

πŸ‡ΊπŸ‡ΈUnited States SiliconValet

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

Comments & Activities

Not all content is available!

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

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

    This issue thread is a little confusing. I'm adding my notes in case it helps someone else.

    I also needed this functionality where I have a view block with a contextual filter of node id (ID), and I want to place
    this block onto a content type via layout builder. Then, when viewing a node the block should display the appropriate
    content.

    Similar to what SiliconValet said, I got this to work by doing this:

    1. Create a Views Block of node fields (any field).
    2. Add a contextual filter of ID.
      • - Expand When the filter value IS available or a default is provided
      • - Enable Specify validation criteria
      • - Validator = Content
      • - Content type = your content type
    3. Add this Views Block to a node Layout Builder layout section.
    4. Configure block and set Content: ID = Content
  • πŸ‡³πŸ‡ΏNew Zealand john pitcairn

    Setting validation on the contextual filter as above does get the relevant argument mapping select added to the layout builder block form. But if there's only a single possible value (the current content entity) it isn't preselected by default, and editors have to know enough to select "Content". That's totally unnecessary cruft.

    Generic module or theme alter() code to preselect and hide this if there's only one available value for each contextual filter mapping:

    /**
     * Implements hook_form_FORM_ID_alter().
     */
    function MYMODULE_form_layout_builder_add_block_alter(&$form, FormStateInterface $form_state) {
      switch ($form['settings']['provider']['#value']) {
        case 'views':
          // Views contextual filter argument handling. For each context mapping,
          // if there is only one option, set the default and hide it.
          if (!empty($form['settings']['context_mapping'])) {
            foreach (Element::children($form['settings']['context_mapping']) as $context_id) {
              $context = &$form['settings']['context_mapping'][$context_id];
              if (count($context['#options']) === 1) {
                $context['#default_value'] = array_key_first($context['#options']);
                $context['#access'] = FALSE;
              }
            }
          }
      }
    }
  • πŸ‡³πŸ‡ΏNew Zealand john pitcairn

    Changing title for a bit better findability.

Production build 0.71.5 2024