Newer
Older
Lee Rowlands
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace Drupal\layout_builder\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
use Drupal\layout_builder\SectionStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form containing the Layout Builder UI for overrides.
*
* @internal
*/
class OverridesEntityForm extends ContentEntityForm {
/**
* Layout tempstore repository.
*
* @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
*/
protected $layoutTempstoreRepository;
/**
* The section storage.
*
* @var \Drupal\layout_builder\SectionStorageInterface
*/
protected $sectionStorage;
/**
* Constructs a new OverridesEntityForm.
*
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository service.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle service.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
* @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
* The layout tempstore repository.
*/
public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time, LayoutTempstoreRepositoryInterface $layout_tempstore_repository) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->layoutTempstoreRepository = $layout_tempstore_repository;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.repository'),
$container->get('entity_type.bundle.info'),
$container->get('datetime.time'),
$container->get('layout_builder.tempstore_repository')
);
}
/**
* {@inheritdoc}
*/
public function getBaseFormId() {
return $this->getEntity()->getEntityTypeId() . '_layout_builder_form';
}
/**
* {@inheritdoc}
*/
protected function init(FormStateInterface $form_state) {
parent::init($form_state);
$form_display = EntityFormDisplay::collectRenderDisplay($this->entity, $this->getOperation(), FALSE);
$form_display->setComponent(OverridesSectionStorage::FIELD_NAME, [
Lee Rowlands
committed
'type' => 'layout_builder_widget',
'weight' => -10,
'settings' => [],
]);
$this->setFormDisplay($form_display, $form_state);
Lee Rowlands
committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL) {
$this->sectionStorage = $section_storage;
$form = parent::buildForm($form, $form_state);
// @todo \Drupal\layout_builder\Field\LayoutSectionItemList::defaultAccess()
// restricts all access to the field, explicitly allow access here until
// https://www.drupal.org/node/2942975 is resolved.
$form[OverridesSectionStorage::FIELD_NAME]['#access'] = TRUE;
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$return = parent::save($form, $form_state);
$this->layoutTempstoreRepository->delete($this->sectionStorage);
$this->messenger()->addStatus($this->t('The layout override has been saved.'));
$form_state->setRedirectUrl($this->sectionStorage->getRedirectUrl());
return $return;
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this->t('Save layout');
$actions['delete']['#access'] = FALSE;
$actions['#weight'] = -1000;
$actions['discard_changes'] = [
'#type' => 'submit',
'#value' => $this->t('Discard changes'),
'#submit' => ['::redirectOnSubmit'],
'#redirect' => 'discard_changes',
Lee Rowlands
committed
];
// @todo This button should be conditionally displayed, see
Lee Rowlands
committed
// https://www.drupal.org/node/2917777.
$actions['revert'] = [
'#type' => 'submit',
'#value' => $this->t('Revert to defaults'),
'#submit' => ['::redirectOnSubmit'],
'#redirect' => 'revert',
Lee Rowlands
committed
];
return $actions;
}
/**
* Form submission handler.
*/
public function redirectOnSubmit(array $form, FormStateInterface $form_state) {
$form_state->setRedirectUrl($this->sectionStorage->getLayoutBuilderUrl($form_state->getTriggeringElement()['#redirect']));
}
Lee Rowlands
committed
/**
* Retrieves the section storage object.
*
* @return \Drupal\layout_builder\SectionStorageInterface
* The section storage for the current form.
*/
public function getSectionStorage() {
return $this->sectionStorage;
}
}