Problem/Motivation
There are 3 scenarios with the component render element:
- Rendering an auto selected variant by not specifying any variant
- Rendering a variant set in the #variant key
- Rendering the base component
Scenario 1 and 2 are covered by the current code. But when trying to embed the base component (scenario 3) via a component render array such as the one below without specifying the #variant key while having an implementation of hook_select_component_variant, which always selects a variant, the render array will render a variant even though the variant is not specified.
[
'#type' => 'component',
'#component' => 'example',
'#variant' => '',
'#props' => [
...
],
];
// or
[
'#type' => 'component',
'#component' => 'example',
'#props' => [
...
],
];
This is problematic, as in some cases rendering a component is the desired behavior.
Steps to reproduce
Proposed resolution
The current selection of a variant on the component loader level in ComponentVariantComponentLoader works by calling ComponentVariantPluginManager::find() which invokes a the mentioned hook to resolve a variant.
Because we don't have the enough context when calling that hook from ComponentVariantComponentLoader, we need to bypass calling the hook there to enforce rendering of the base component.
This mechanism is already in place, it was implemented in #3591290: Allow embedding base component in its own variant and we can leverage that to enforce rendering of the base component from render arrays by adding a special __default value for the #variant key and if that is set we print the component name with the base_component prefix when overriding the inline template of ComponentElement in \Drupal\component_variants\Hook\ComponentVariantsHooks::getVariantInlineTemplate
Remaining tasks
User interface changes
API changes
Data model changes
Issue fork component_variants-3593659
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 #3
s_leu commentedComment #4
s_leu commentedComment #5
graber commentedComment #6
s_leu commentedComment #8
graber commented