- πΊπΈ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:
- Create a Views Block of node fields (any field).
- 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
- Add this Views Block to a node Layout Builder layout section.
- 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.