3 namespace Drupal\layout_builder\Form;
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;
13 * Edit form for the LayoutBuilderEntityViewDisplay entity type.
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.
20 class LayoutBuilderEntityViewDisplayForm extends EntityViewDisplayEditForm {
23 * The entity being used by this form.
25 * @var \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface
30 * The storage section.
32 * @var \Drupal\layout_builder\DefaultsSectionStorageInterface
34 protected $sectionStorage;
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);
47 public function form(array $form, FormStateInterface $form_state) {
48 $form = parent::form($form, $form_state);
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']);
54 $is_enabled = $this->entity->isLayoutBuilderEnabled();
56 // Hide the table of fields.
57 $form['fields']['#access'] = FALSE;
58 $form['#fields'] = [];
62 $form['manage_layout'] = [
64 '#title' => $this->t('Manage layout'),
66 '#attributes' => ['class' => ['button']],
67 '#url' => $this->sectionStorage->getLayoutBuilderUrl(),
68 '#access' => $is_enabled,
74 '#title' => $this->t('Layout options'),
78 $form['layout']['enabled'] = [
79 '#type' => 'checkbox',
80 '#title' => $this->t('Use Layout Builder'),
81 '#default_value' => $is_enabled,
83 $form['#entity_builders']['layout_builder'] = '::entityFormEntityBuild';
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(),
94 '#default_value' => $this->entity->isOverridable(),
97 ':input[name="layout[enabled]"]' => ['checked' => FALSE],
100 ':input[name="layout[enabled]"]' => ['checked' => FALSE],
105 $form['layout']['allow_custom']['#attributes']['disabled'] = 'disabled';
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']);
121 * Determines if the defaults have any overrides.
123 * @param \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface $display
124 * The entity display.
127 * TRUE if there are any overrides of this default, FALSE otherwise.
129 protected function hasOverrides(LayoutEntityDisplayInterface $display) {
130 if (!$display->isOverridable()) {
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());
140 return (bool) $query->count()->execute();
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'] = [];
156 parent::copyFormValuesToEntity($entity, $form, $form_state);
160 * Entity builder for layout options on the entity view display form.
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();
167 $overridable = (bool) $form_state->getValue(['layout', 'allow_custom'], FALSE);
168 $display->setOverridable($overridable);
170 if (!$already_enabled) {
171 $display->enableLayoutBuilder();
174 elseif ($already_enabled) {
175 $form_state->setRedirectUrl($this->sectionStorage->getLayoutBuilderUrl('disable'));
182 protected function buildFieldRow(FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) {
183 if ($this->entity->isLayoutBuilderEnabled()) {
187 return parent::buildFieldRow($field_definition, $form, $form_state);
193 protected function buildExtraFieldRow($field_id, $extra_field) {
194 if ($this->entity->isLayoutBuilderEnabled()) {
198 return parent::buildExtraFieldRow($field_id, $extra_field);