Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / layout_builder / src / Form / LayoutBuilderEntityViewDisplayForm.php
1 <?php
2
3 namespace Drupal\layout_builder\Form;
4
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Field\FieldDefinitionInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\field_ui\Form\EntityViewDisplayEditForm;
9 use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface;
10 use Drupal\layout_builder\SectionStorageInterface;
11
12 /**
13  * Edit form for the LayoutBuilderEntityViewDisplay entity type.
14  *
15  * @internal
16  *   Layout Builder is currently experimental and should only be leveraged by
17  *   experimental modules and development releases of contributed modules.
18  *   See https://www.drupal.org/core/experimental for more information.
19  */
20 class LayoutBuilderEntityViewDisplayForm extends EntityViewDisplayEditForm {
21
22   /**
23    * The entity being used by this form.
24    *
25    * @var \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface
26    */
27   protected $entity;
28
29   /**
30    * The storage section.
31    *
32    * @var \Drupal\layout_builder\DefaultsSectionStorageInterface
33    */
34   protected $sectionStorage;
35
36   /**
37    * {@inheritdoc}
38    */
39   public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL) {
40     $this->sectionStorage = $section_storage;
41     return parent::buildForm($form, $form_state);
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function form(array $form, FormStateInterface $form_state) {
48     $form = parent::form($form, $form_state);
49
50     // Remove the Layout Builder field from the list.
51     $form['#fields'] = array_diff($form['#fields'], ['layout_builder__layout']);
52     unset($form['fields']['layout_builder__layout']);
53
54     $is_enabled = $this->entity->isLayoutBuilderEnabled();
55     if ($is_enabled) {
56       // Hide the table of fields.
57       $form['fields']['#access'] = FALSE;
58       $form['#fields'] = [];
59       $form['#extra'] = [];
60     }
61
62     $form['manage_layout'] = [
63       '#type' => 'link',
64       '#title' => $this->t('Manage layout'),
65       '#weight' => -10,
66       '#attributes' => ['class' => ['button']],
67       '#url' => $this->sectionStorage->getLayoutBuilderUrl(),
68       '#access' => $is_enabled,
69     ];
70
71     $form['layout'] = [
72       '#type' => 'details',
73       '#open' => TRUE,
74       '#title' => $this->t('Layout options'),
75       '#tree' => TRUE,
76     ];
77
78     $form['layout']['enabled'] = [
79       '#type' => 'checkbox',
80       '#title' => $this->t('Use Layout Builder'),
81       '#default_value' => $is_enabled,
82     ];
83     $form['#entity_builders']['layout_builder'] = '::entityFormEntityBuild';
84
85     // @todo Expand to work for all view modes in
86     //   https://www.drupal.org/node/2907413.
87     if ($this->entity->getMode() === 'default') {
88       $entity_type = $this->entityTypeManager->getDefinition($this->entity->getTargetEntityTypeId());
89       $form['layout']['allow_custom'] = [
90         '#type' => 'checkbox',
91         '#title' => $this->t('Allow each @entity to have its layout customized.', [
92           '@entity' => $entity_type->getSingularLabel(),
93         ]),
94         '#default_value' => $this->entity->isOverridable(),
95         '#states' => [
96           'disabled' => [
97             ':input[name="layout[enabled]"]' => ['checked' => FALSE],
98           ],
99           'invisible' => [
100             ':input[name="layout[enabled]"]' => ['checked' => FALSE],
101           ],
102         ],
103       ];
104       if (!$is_enabled) {
105         $form['layout']['allow_custom']['#attributes']['disabled'] = 'disabled';
106       }
107       // Prevent turning off overrides while any exist.
108       if ($this->hasOverrides($this->entity)) {
109         $form['layout']['enabled']['#disabled'] = TRUE;
110         $form['layout']['enabled']['#description'] = $this->t('You must revert all customized layouts of this display before you can disable this option.');
111         $form['layout']['allow_custom']['#disabled'] = TRUE;
112         $form['layout']['allow_custom']['#description'] = $this->t('You must revert all customized layouts of this display before you can disable this option.');
113         unset($form['layout']['allow_custom']['#states']);
114         unset($form['#entity_builders']['layout_builder']);
115       }
116     }
117     return $form;
118   }
119
120   /**
121    * Determines if the defaults have any overrides.
122    *
123    * @param \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface $display
124    *   The entity display.
125    *
126    * @return bool
127    *   TRUE if there are any overrides of this default, FALSE otherwise.
128    */
129   protected function hasOverrides(LayoutEntityDisplayInterface $display) {
130     if (!$display->isOverridable()) {
131       return FALSE;
132     }
133
134     $entity_type = $this->entityTypeManager->getDefinition($display->getTargetEntityTypeId());
135     $query = $this->entityTypeManager->getStorage($display->getTargetEntityTypeId())->getQuery()
136       ->exists('layout_builder__layout');
137     if ($bundle_key = $entity_type->getKey('bundle')) {
138       $query->condition($bundle_key, $display->getTargetBundle());
139     }
140     return (bool) $query->count()->execute();
141   }
142
143   /**
144    * {@inheritdoc}
145    */
146   protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
147     // Do not process field values if Layout Builder is or will be enabled.
148     $set_enabled = (bool) $form_state->getValue(['layout', 'enabled'], FALSE);
149     /** @var \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface $entity */
150     $already_enabled = $entity->isLayoutBuilderEnabled();
151     if ($already_enabled || $set_enabled) {
152       $form['#fields'] = [];
153       $form['#extra'] = [];
154     }
155
156     parent::copyFormValuesToEntity($entity, $form, $form_state);
157   }
158
159   /**
160    * Entity builder for layout options on the entity view display form.
161    */
162   public function entityFormEntityBuild($entity_type_id, LayoutEntityDisplayInterface $display, &$form, FormStateInterface &$form_state) {
163     $set_enabled = (bool) $form_state->getValue(['layout', 'enabled'], FALSE);
164     $already_enabled = $display->isLayoutBuilderEnabled();
165
166     if ($set_enabled) {
167       $overridable = (bool) $form_state->getValue(['layout', 'allow_custom'], FALSE);
168       $display->setOverridable($overridable);
169
170       if (!$already_enabled) {
171         $display->enableLayoutBuilder();
172       }
173     }
174     elseif ($already_enabled) {
175       $form_state->setRedirectUrl($this->sectionStorage->getLayoutBuilderUrl('disable'));
176     }
177   }
178
179   /**
180    * {@inheritdoc}
181    */
182   protected function buildFieldRow(FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) {
183     if ($this->entity->isLayoutBuilderEnabled()) {
184       return [];
185     }
186
187     return parent::buildFieldRow($field_definition, $form, $form_state);
188   }
189
190   /**
191    * {@inheritdoc}
192    */
193   protected function buildExtraFieldRow($field_id, $extra_field) {
194     if ($this->entity->isLayoutBuilderEnabled()) {
195       return [];
196     }
197
198     return parent::buildExtraFieldRow($field_id, $extra_field);
199   }
200
201 }