You are here

public function LayoutBuilderEntityViewDisplay::setComponent in Drupal 8

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

Sets the display options for a component.

Parameters

string $name: The name of the component.

array $options: The display options.

Return value

$this

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 433

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 = isset($options['region']) ? $options['region'] : $section
      ->getDefaultRegion();
    $new_component = new SectionComponent(\Drupal::service('uuid')
      ->generate(), $region, $configuration);
    $section
      ->appendComponent($new_component);
  }
  return $this;
}