function LayoutBuilderEntityViewDisplayForm::form

Same name and namespace in other branches
  1. 9 core/modules/layout_builder/src/Form/LayoutBuilderEntityViewDisplayForm.php \Drupal\layout_builder\Form\LayoutBuilderEntityViewDisplayForm::form()
  2. 8.9.x core/modules/layout_builder/src/Form/LayoutBuilderEntityViewDisplayForm.php \Drupal\layout_builder\Form\LayoutBuilderEntityViewDisplayForm::form()
  3. 11.x core/modules/layout_builder/src/Form/LayoutBuilderEntityViewDisplayForm.php \Drupal\layout_builder\Form\LayoutBuilderEntityViewDisplayForm::form()

Overrides EntityDisplayFormBase::form

File

core/modules/layout_builder/src/Form/LayoutBuilderEntityViewDisplayForm.php, line 46

Class

LayoutBuilderEntityViewDisplayForm
Edit form for the LayoutBuilderEntityViewDisplay entity type.

Namespace

Drupal\layout_builder\Form

Code

public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
    // Remove the Layout Builder field from the list.
    $form['#fields'] = array_diff($form['#fields'], [
        OverridesSectionStorage::FIELD_NAME,
    ]);
    unset($form['fields'][OverridesSectionStorage::FIELD_NAME]);
    $is_enabled = $this->entity
        ->isLayoutBuilderEnabled();
    if ($is_enabled) {
        // Hide the table of fields.
        $form['fields']['#access'] = FALSE;
        $form['#fields'] = [];
        $form['#extra'] = [];
    }
    $form['manage_layout'] = [
        '#type' => 'link',
        '#title' => $this->t('Manage layout'),
        '#weight' => -10,
        '#attributes' => [
            'class' => [
                'button',
            ],
        ],
        '#url' => $this->sectionStorage
            ->getLayoutBuilderUrl(),
        '#access' => $is_enabled,
    ];
    $form['layout'] = [
        '#type' => 'details',
        '#open' => TRUE,
        '#title' => $this->t('Layout options'),
        '#tree' => TRUE,
    ];
    $form['layout']['enabled'] = [
        '#type' => 'checkbox',
        '#title' => $this->t('Use Layout Builder'),
        '#default_value' => $is_enabled,
    ];
    $form['#entity_builders']['layout_builder'] = '::entityFormEntityBuild';
    // @todo Expand to work for all view modes in
    //   https://www.drupal.org/node/2907413.
    if ($this->isCanonicalMode($this->entity
        ->getMode())) {
        $entity_type = $this->entityTypeManager
            ->getDefinition($this->entity
            ->getTargetEntityTypeId());
        $form['layout']['allow_custom'] = [
            '#type' => 'checkbox',
            '#title' => $this->t('Allow each @entity to have its layout customized.', [
                '@entity' => $entity_type->getSingularLabel(),
            ]),
            '#default_value' => $this->entity
                ->isOverridable(),
            '#states' => [
                'disabled' => [
                    ':input[name="layout[enabled]"]' => [
                        'checked' => FALSE,
                    ],
                ],
                'invisible' => [
                    ':input[name="layout[enabled]"]' => [
                        'checked' => FALSE,
                    ],
                ],
            ],
        ];
        if (!$is_enabled) {
            $form['layout']['allow_custom']['#attributes']['disabled'] = 'disabled';
        }
        // Prevent turning off overrides while any exist.
        if ($this->hasOverrides($this->entity)) {
            $form['layout']['enabled']['#disabled'] = TRUE;
            $form['layout']['enabled']['#description'] = $this->t('You must revert all customized layouts of this display before you can disable this option.');
            $form['layout']['allow_custom']['#disabled'] = TRUE;
            $form['layout']['allow_custom']['#description'] = $this->t('You must revert all customized layouts of this display before you can disable this option.');
            unset($form['layout']['allow_custom']['#states']);
            unset($form['#entity_builders']['layout_builder']);
        }
    }
    else {
        $form['layout']['allow_custom'] = [
            '#type' => 'value',
            '#value' => $this->entity
                ->isOverridable(),
        ];
    }
    return $form;
}

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