function LayoutBuilderEntityViewDisplay::setComponent

Same name and namespace in other branches
  1. 9 core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::setComponent()
  2. 8.9.x core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::setComponent()
  3. 10 core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::setComponent()

Attributes

#[ActionMethod(adminLabel: new TranslatableMarkup('Add component to display'))]

Overrides EntityDisplayBase::setComponent

1 call to LayoutBuilderEntityViewDisplay::setComponent()
LayoutBuilderEntityViewDisplay::preSave in core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php
Acts on an entity before the presave hook is invoked.

File

core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php, line 430

Class

LayoutBuilderEntityViewDisplay
Provides an entity view display entity that has a layout.

Namespace

Drupal\layout_builder\Entity

Code

public function setComponent($name, array $options = []) {
  parent::setComponent($name, $options);
  // Only continue if Layout Builder is enabled.
  if (!$this->isLayoutBuilderEnabled()) {
    return $this;
  }
  // Retrieve the updated options after the parent:: call.
  $options = $this->content[$name];
  // Provide backwards compatibility by converting to a section component.
  $field_definition = $this->getFieldDefinition($name);
  $extra_fields = $this->entityFieldManager
    ->getExtraFields($this->getTargetEntityTypeId(), $this->getTargetBundle());
  $is_view_configurable_non_extra_field = $field_definition && $field_definition->isDisplayConfigurable('view') && isset($options['type']);
  if ($is_view_configurable_non_extra_field || isset($extra_fields['display'][$name])) {
    $configuration = [
      'label_display' => '0',
      'context_mapping' => [
        'entity' => 'layout_builder.entity',
      ],
    ];
    if ($is_view_configurable_non_extra_field) {
      $configuration['id'] = 'field_block:' . $this->getTargetEntityTypeId() . ':' . $this->getTargetBundle() . ':' . $name;
      $keys = array_flip([
        'type',
        'label',
        'settings',
        'third_party_settings',
      ]);
      $configuration['formatter'] = array_intersect_key($options, $keys);
    }
    else {
      $configuration['id'] = 'extra_field_block:' . $this->getTargetEntityTypeId() . ':' . $this->getTargetBundle() . ':' . $name;
    }
    $section = $this->getDefaultSection();
    $region = $options['region'] ?? $section->getDefaultRegion();
    $new_component = new SectionComponent(\Drupal::service('uuid')->generate(), $region, $configuration);
    $section->appendComponent($new_component);
  }
  return $this;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.