Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / layout_builder / src / Form / RevertOverridesForm.php
1 <?php
2
3 namespace Drupal\layout_builder\Form;
4
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;
12
13 /**
14  * Reverts the overridden layout to the defaults.
15  */
16 class RevertOverridesForm extends ConfirmFormBase {
17
18   /**
19    * The layout tempstore repository.
20    *
21    * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
22    */
23   protected $layoutTempstoreRepository;
24
25   /**
26    * The messenger service.
27    *
28    * @var \Drupal\Core\Messenger\MessengerInterface
29    */
30   protected $messenger;
31
32   /**
33    * The section storage.
34    *
35    * @var \Drupal\layout_builder\SectionStorageInterface
36    */
37   protected $sectionStorage;
38
39   /**
40    * Constructs a new RevertOverridesForm.
41    *
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.
46    */
47   public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, MessengerInterface $messenger) {
48     $this->layoutTempstoreRepository = $layout_tempstore_repository;
49     $this->messenger = $messenger;
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public static function create(ContainerInterface $container) {
56     return new static(
57       $container->get('layout_builder.tempstore_repository'),
58       $container->get('messenger')
59     );
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function getFormId() {
66     return 'layout_builder_revert_overrides';
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function getQuestion() {
73     return $this->t('Are you sure you want to revert this to defaults?');
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public function getConfirmText() {
80     return $this->t('Revert');
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function getCancelUrl() {
87     return $this->sectionStorage->getLayoutBuilderUrl();
88   }
89
90   /**
91    * {@inheritdoc}
92    */
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()));
96     }
97
98     $this->sectionStorage = $section_storage;
99     return parent::buildForm($form, $form_state);
100   }
101
102   /**
103    * {@inheritdoc}
104    */
105   public function submitForm(array &$form, FormStateInterface $form_state) {
106     // Remove all sections.
107     while ($this->sectionStorage->count()) {
108       $this->sectionStorage->removeSection(0);
109     }
110     $this->sectionStorage->save();
111     $this->layoutTempstoreRepository->delete($this->sectionStorage);
112
113     $this->messenger->addMessage($this->t('The layout has been reverted back to defaults.'));
114     $form_state->setRedirectUrl($this->getCancelUrl());
115   }
116
117 }