3 namespace Drupal\layout_builder\Form;
5 use Drupal\Core\Form\ConfirmFormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Messenger\MessengerInterface;
8 use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
9 use Drupal\layout_builder\OverridesSectionStorageInterface;
10 use Drupal\layout_builder\SectionStorageInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Reverts the overridden layout to the defaults.
16 class RevertOverridesForm extends ConfirmFormBase {
19 * The layout tempstore repository.
21 * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
23 protected $layoutTempstoreRepository;
26 * The messenger service.
28 * @var \Drupal\Core\Messenger\MessengerInterface
33 * The section storage.
35 * @var \Drupal\layout_builder\SectionStorageInterface
37 protected $sectionStorage;
40 * Constructs a new RevertOverridesForm.
42 * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
43 * The layout tempstore repository.
44 * @param \Drupal\Core\Messenger\MessengerInterface $messenger
45 * The messenger service.
47 public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, MessengerInterface $messenger) {
48 $this->layoutTempstoreRepository = $layout_tempstore_repository;
49 $this->messenger = $messenger;
55 public static function create(ContainerInterface $container) {
57 $container->get('layout_builder.tempstore_repository'),
58 $container->get('messenger')
65 public function getFormId() {
66 return 'layout_builder_revert_overrides';
72 public function getQuestion() {
73 return $this->t('Are you sure you want to revert this to defaults?');
79 public function getConfirmText() {
80 return $this->t('Revert');
86 public function getCancelUrl() {
87 return $this->sectionStorage->getLayoutBuilderUrl();
93 public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL) {
94 if (!$section_storage instanceof OverridesSectionStorageInterface) {
95 throw new \InvalidArgumentException(sprintf('The section storage with type "%s" and ID "%s" does not provide overrides', $section_storage->getStorageType(), $section_storage->getStorageId()));
98 $this->sectionStorage = $section_storage;
99 return parent::buildForm($form, $form_state);
105 public function submitForm(array &$form, FormStateInterface $form_state) {
106 // Remove all sections.
107 while ($this->sectionStorage->count()) {
108 $this->sectionStorage->removeSection(0);
110 $this->sectionStorage->save();
111 $this->layoutTempstoreRepository->delete($this->sectionStorage);
113 $this->messenger->addMessage($this->t('The layout has been reverted back to defaults.'));
114 $form_state->setRedirectUrl($this->getCancelUrl());