3 namespace Drupal\layout_builder\Form;
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;
12 * Edit form for the LayoutBuilderEntityViewDisplay entity type.
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.
19 class LayoutBuilderEntityViewDisplayForm extends EntityViewDisplayEditForm {
22 * The entity being used by this form.
24 * @var \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface
29 * The storage section.
31 * @var \Drupal\layout_builder\SectionStorageInterface
33 protected $sectionStorage;
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);
46 public function form(array $form, FormStateInterface $form_state) {
47 $form = parent::form($form, $form_state);
49 // Hide the table of fields.
50 $form['fields']['#access'] = FALSE;
51 $form['#fields'] = [];
54 $form['manage_layout'] = [
56 '#title' => $this->t('Manage layout'),
58 '#attributes' => ['class' => ['button']],
59 '#url' => $this->sectionStorage->getLayoutBuilderUrl(),
62 // @todo Expand to work for all view modes in
63 // https://www.drupal.org/node/2907413.
64 if ($this->entity->getMode() === 'default') {
68 '#title' => $this->t('Layout options'),
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(),
78 '#default_value' => $this->entity->isOverridable(),
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.');
86 $form['#entity_builders'][] = '::entityFormEntityBuild';
93 * Determines if the defaults have any overrides.
95 * @param \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface $display
99 * TRUE if there are any overrides of this default, FALSE otherwise.
101 protected function hasOverrides(LayoutEntityDisplayInterface $display) {
102 if (!$display->isOverridable()) {
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());
112 return (bool) $query->count()->execute();
116 * Entity builder for layout options on the entity view display form.
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);
126 protected function buildFieldRow(FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) {
127 // Intentionally empty.
133 protected function buildExtraFieldRow($field_id, $extra_field) {
134 // Intentionally empty.