function LayoutBuilderEntityViewDisplay::addSectionField

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

Adds a layout section field to a given bundle.

Parameters

string $entity_type_id: The entity type ID.

string $bundle: The bundle.

string $field_name: The name for the layout section field.

1 call to LayoutBuilderEntityViewDisplay::addSectionField()
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 197

Class

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

Namespace

Drupal\layout_builder\Entity

Code

protected function addSectionField($entity_type_id, $bundle, $field_name) {
    $field = FieldConfig::loadByName($entity_type_id, $bundle, $field_name);
    if (!$field) {
        $field_storage = FieldStorageConfig::loadByName($entity_type_id, $field_name);
        if (!$field_storage) {
            $field_storage = FieldStorageConfig::create([
                'entity_type' => $entity_type_id,
                'field_name' => $field_name,
                'type' => 'layout_section',
                'locked' => TRUE,
            ]);
            $field_storage->setTranslatable(FALSE);
            $field_storage->save();
        }
        $field = FieldConfig::create([
            'field_storage' => $field_storage,
            'bundle' => $bundle,
            'label' => t('Layout'),
        ]);
        $field->setTranslatable(FALSE);
        $field->save();
    }
}

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