LayoutBuilderEntityViewDisplayForm.php 7.79 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\layout_builder\Form;

5
use Drupal\Core\Entity\EntityInterface;
6 7 8 9
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field_ui\Form\EntityViewDisplayEditForm;
use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface;
10
use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
11
use Drupal\layout_builder\SectionStorageInterface;
12 13 14 15 16

/**
 * Edit form for the LayoutBuilderEntityViewDisplay entity type.
 *
 * @internal
17
 *   Form classes are internal.
18 19 20 21 22 23 24 25 26 27
 */
class LayoutBuilderEntityViewDisplayForm extends EntityViewDisplayEditForm {

  /**
   * The entity being used by this form.
   *
   * @var \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface
   */
  protected $entity;

28 29 30
  /**
   * The storage section.
   *
31
   * @var \Drupal\layout_builder\DefaultsSectionStorageInterface
32 33 34 35 36 37 38 39 40 41 42
   */
  protected $sectionStorage;

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL) {
    $this->sectionStorage = $section_storage;
    return parent::buildForm($form, $form_state);
  }

43 44 45 46 47 48
  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);

49
    // Remove the Layout Builder field from the list.
50 51
    $form['#fields'] = array_diff($form['#fields'], [OverridesSectionStorage::FIELD_NAME]);
    unset($form['fields'][OverridesSectionStorage::FIELD_NAME]);
52

53 54 55 56 57 58 59
    $is_enabled = $this->entity->isLayoutBuilderEnabled();
    if ($is_enabled) {
      // Hide the table of fields.
      $form['fields']['#access'] = FALSE;
      $form['#fields'] = [];
      $form['#extra'] = [];
    }
60 61 62 63 64 65

    $form['manage_layout'] = [
      '#type' => 'link',
      '#title' => $this->t('Manage layout'),
      '#weight' => -10,
      '#attributes' => ['class' => ['button']],
66
      '#url' => $this->sectionStorage->getLayoutBuilderUrl(),
67 68 69 70 71 72 73 74
      '#access' => $is_enabled,
    ];

    $form['layout'] = [
      '#type' => 'details',
      '#open' => TRUE,
      '#title' => $this->t('Layout options'),
      '#tree' => TRUE,
75 76
    ];

77 78 79 80 81 82 83
    $form['layout']['enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Use Layout Builder'),
      '#default_value' => $is_enabled,
    ];
    $form['#entity_builders']['layout_builder'] = '::entityFormEntityBuild';

84 85
    // @todo Expand to work for all view modes in
    //   https://www.drupal.org/node/2907413.
86
    if ($this->isCanonicalMode($this->entity->getMode())) {
87 88 89 90 91 92 93
      $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(),
94 95 96 97 98 99 100 101
        '#states' => [
          'disabled' => [
            ':input[name="layout[enabled]"]' => ['checked' => FALSE],
          ],
          'invisible' => [
            ':input[name="layout[enabled]"]' => ['checked' => FALSE],
          ],
        ],
102
      ];
103 104 105
      if (!$is_enabled) {
        $form['layout']['allow_custom']['#attributes']['disabled'] = 'disabled';
      }
106 107
      // Prevent turning off overrides while any exist.
      if ($this->hasOverrides($this->entity)) {
108 109
        $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.');
110 111
        $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.');
112 113
        unset($form['layout']['allow_custom']['#states']);
        unset($form['#entity_builders']['layout_builder']);
114
      }
115
    }
116 117 118 119 120 121 122
    // For non-canonical modes, the existing value should be preserved.
    else {
      $form['layout']['allow_custom'] = [
        '#type' => 'value',
        '#value' => $this->entity->isOverridable(),
      ];
    }
123 124 125
    return $form;
  }

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
  /**
   * Determines if the mode is used by the canonical route.
   *
   * @param string $mode
   *   The view mode.
   *
   * @return bool
   *   TRUE if the mode is valid, FALSE otherwise.
   */
  protected function isCanonicalMode($mode) {
    // @todo This is a convention core uses but is not a given, nor is it easily
    //   introspectable. Address in https://www.drupal.org/node/2907413.
    $canonical_mode = 'full';

    if ($mode === $canonical_mode) {
      return TRUE;
    }

    // The default mode is valid if the canonical mode is not enabled.
    if ($mode === 'default') {
      $query = $this->entityTypeManager->getStorage($this->entity->getEntityTypeId())->getQuery()
147 148
        ->condition('targetEntityType', $this->entity->getTargetEntityTypeId())
        ->condition('bundle', $this->entity->getTargetBundle())
149 150 151 152 153 154 155 156
        ->condition('status', TRUE)
        ->condition('mode', $canonical_mode);
      return !$query->count()->execute();
    }

    return FALSE;
  }

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  /**
   * Determines if the defaults have any overrides.
   *
   * @param \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface $display
   *   The entity display.
   *
   * @return bool
   *   TRUE if there are any overrides of this default, FALSE otherwise.
   */
  protected function hasOverrides(LayoutEntityDisplayInterface $display) {
    if (!$display->isOverridable()) {
      return FALSE;
    }

    $entity_type = $this->entityTypeManager->getDefinition($display->getTargetEntityTypeId());
    $query = $this->entityTypeManager->getStorage($display->getTargetEntityTypeId())->getQuery()
173
      ->exists(OverridesSectionStorage::FIELD_NAME);
174 175 176 177 178 179
    if ($bundle_key = $entity_type->getKey('bundle')) {
      $query->condition($bundle_key, $display->getTargetBundle());
    }
    return (bool) $query->count()->execute();
  }

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
  /**
   * {@inheritdoc}
   */
  protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
    // Do not process field values if Layout Builder is or will be enabled.
    $set_enabled = (bool) $form_state->getValue(['layout', 'enabled'], FALSE);
    /** @var \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface $entity */
    $already_enabled = $entity->isLayoutBuilderEnabled();
    if ($already_enabled || $set_enabled) {
      $form['#fields'] = [];
      $form['#extra'] = [];
    }

    parent::copyFormValuesToEntity($entity, $form, $form_state);
  }

196 197 198 199
  /**
   * Entity builder for layout options on the entity view display form.
   */
  public function entityFormEntityBuild($entity_type_id, LayoutEntityDisplayInterface $display, &$form, FormStateInterface &$form_state) {
200 201 202 203 204 205 206 207 208 209 210 211 212 213
    $set_enabled = (bool) $form_state->getValue(['layout', 'enabled'], FALSE);
    $already_enabled = $display->isLayoutBuilderEnabled();

    if ($set_enabled) {
      $overridable = (bool) $form_state->getValue(['layout', 'allow_custom'], FALSE);
      $display->setOverridable($overridable);

      if (!$already_enabled) {
        $display->enableLayoutBuilder();
      }
    }
    elseif ($already_enabled) {
      $form_state->setRedirectUrl($this->sectionStorage->getLayoutBuilderUrl('disable'));
    }
214 215 216 217 218 219
  }

  /**
   * {@inheritdoc}
   */
  protected function buildFieldRow(FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) {
220
    if ($this->entity->isLayoutBuilderEnabled()) {
221 222 223 224
      return [];
    }

    return parent::buildFieldRow($field_definition, $form, $form_state);
225 226 227 228 229 230
  }

  /**
   * {@inheritdoc}
   */
  protected function buildExtraFieldRow($field_id, $extra_field) {
231 232 233 234 235
    if ($this->entity->isLayoutBuilderEnabled()) {
      return [];
    }

    return parent::buildExtraFieldRow($field_id, $extra_field);
236 237 238
  }

}