Problem/Motivation

There are 3 scenarios with the component render element:

  1. Rendering an auto selected variant by not specifying any variant
  2. Rendering a variant set in the #variant key
  3. 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

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

s_leu created an issue. See original summary.

s_leu’s picture

Status: Active » Needs review
s_leu’s picture

Title: Component element can no longer render base component » Component render element can no longer render base component
graber’s picture

Status: Needs review » Needs work
s_leu’s picture

Status: Needs work » Needs review

  • graber committed 88cc50dd on 1.0.x authored by s_leu
    Issues #3593659: Add new method for getting available variants options...
graber’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.