Updated to Drupal 8.5. Core Media not yet in use.
[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\Field\FieldDefinitionInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\field_ui\Form\EntityViewDisplayEditForm;
8 use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface;
9 use Drupal\layout_builder\SectionStorageInterface;
10
11 /**
12  * Edit form for the LayoutBuilderEntityViewDisplay entity type.
13  *
14  * @internal
15  *   Layout Builder is currently experimental and should only be leveraged by
16  *   experimental modules and development releases of contributed modules.
17  *   See https://www.drupal.org/core/experimental for more information.
18  */
19 class LayoutBuilderEntityViewDisplayForm extends EntityViewDisplayEditForm {
20
21   /**
22    * The entity being used by this form.
23    *
24    * @var \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface
25    */
26   protected $entity;
27
28   /**
29    * The storage section.
30    *
31    * @var \Drupal\layout_builder\SectionStorageInterface
32    */
33   protected $sectionStorage;
34
35   /**
36    * {@inheritdoc}
37    */
38   public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL) {
39     $this->sectionStorage = $section_storage;
40     return parent::buildForm($form, $form_state);
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function form(array $form, FormStateInterface $form_state) {
47     $form = parent::form($form, $form_state);
48
49     // Hide the table of fields.
50     $form['fields']['#access'] = FALSE;
51     $form['#fields'] = [];
52     $form['#extra'] = [];
53
54     $form['manage_layout'] = [
55       '#type' => 'link',
56       '#title' => $this->t('Manage layout'),
57       '#weight' => -10,
58       '#attributes' => ['class' => ['button']],
59       '#url' => $this->sectionStorage->getLayoutBuilderUrl(),
60     ];
61
62     // @todo Expand to work for all view modes in
63     //   https://www.drupal.org/node/2907413.
64     if ($this->entity->getMode() === 'default') {
65       $form['layout'] = [
66         '#type' => 'details',
67         '#open' => TRUE,
68         '#title' => $this->t('Layout options'),
69         '#tree' => TRUE,
70       ];
71
72       $entity_type = $this->entityTypeManager->getDefinition($this->entity->getTargetEntityTypeId());
73       $form['layout']['allow_custom'] = [
74         '#type' => 'checkbox',
75         '#title' => $this->t('Allow each @entity to have its layout customized.', [
76           '@entity' => $entity_type->getSingularLabel(),
77         ]),
78         '#default_value' => $this->entity->isOverridable(),
79       ];
80       // Prevent turning off overrides while any exist.
81       if ($this->hasOverrides($this->entity)) {
82         $form['layout']['allow_custom']['#disabled'] = TRUE;
83         $form['layout']['allow_custom']['#description'] = $this->t('You must revert all customized layouts of this display before you can disable this option.');
84       }
85       else {
86         $form['#entity_builders'][] = '::entityFormEntityBuild';
87       }
88     }
89     return $form;
90   }
91
92   /**
93    * Determines if the defaults have any overrides.
94    *
95    * @param \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface $display
96    *   The entity display.
97    *
98    * @return bool
99    *   TRUE if there are any overrides of this default, FALSE otherwise.
100    */
101   protected function hasOverrides(LayoutEntityDisplayInterface $display) {
102     if (!$display->isOverridable()) {
103       return FALSE;
104     }
105
106     $entity_type = $this->entityTypeManager->getDefinition($display->getTargetEntityTypeId());
107     $query = $this->entityTypeManager->getStorage($display->getTargetEntityTypeId())->getQuery()
108       ->exists('layout_builder__layout');
109     if ($bundle_key = $entity_type->getKey('bundle')) {
110       $query->condition($bundle_key, $display->getTargetBundle());
111     }
112     return (bool) $query->count()->execute();
113   }
114
115   /**
116    * Entity builder for layout options on the entity view display form.
117    */
118   public function entityFormEntityBuild($entity_type_id, LayoutEntityDisplayInterface $display, &$form, FormStateInterface &$form_state) {
119     $new_value = (bool) $form_state->getValue(['layout', 'allow_custom'], FALSE);
120     $display->setOverridable($new_value);
121   }
122
123   /**
124    * {@inheritdoc}
125    */
126   protected function buildFieldRow(FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) {
127     // Intentionally empty.
128   }
129
130   /**
131    * {@inheritdoc}
132    */
133   protected function buildExtraFieldRow($field_id, $extra_field) {
134     // Intentionally empty.
135   }
136
137 }