Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / layout_builder / src / Form / LayoutBuilderDisableForm.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\DefaultsSectionStorageInterface;
9 use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
10 use Drupal\layout_builder\SectionStorageInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Disables Layout Builder for a given default.
15  */
16 class LayoutBuilderDisableForm extends ConfirmFormBase {
17
18   /**
19    * The layout tempstore repository.
20    *
21    * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
22    */
23   protected $layoutTempstoreRepository;
24
25   /**
26    * The section storage.
27    *
28    * @var \Drupal\layout_builder\DefaultsSectionStorageInterface
29    */
30   protected $sectionStorage;
31
32   /**
33    * Constructs a new RevertOverridesForm.
34    *
35    * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
36    *   The layout tempstore repository.
37    * @param \Drupal\Core\Messenger\MessengerInterface $messenger
38    *   The messenger service.
39    */
40   public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, MessengerInterface $messenger) {
41     $this->layoutTempstoreRepository = $layout_tempstore_repository;
42     $this->setMessenger($messenger);
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public static function create(ContainerInterface $container) {
49     return new static(
50       $container->get('layout_builder.tempstore_repository'),
51       $container->get('messenger')
52     );
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function getFormId() {
59     return 'layout_builder_disable_form';
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function getQuestion() {
66     return $this->t('Are you sure you want to disable Layout Builder?');
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function getDescription() {
73     return $this->t('All customizations will be removed. This action cannot be undone.');
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public function getCancelUrl() {
80     return $this->sectionStorage->getRedirectUrl();
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL) {
87     if (!$section_storage instanceof DefaultsSectionStorageInterface) {
88       throw new \InvalidArgumentException(sprintf('The section storage with type "%s" and ID "%s" does not provide defaults', $section_storage->getStorageType(), $section_storage->getStorageId()));
89     }
90
91     $this->sectionStorage = $section_storage;
92     return parent::buildForm($form, $form_state);
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function submitForm(array &$form, FormStateInterface $form_state) {
99     $this->sectionStorage->disableLayoutBuilder()->save();
100     $this->layoutTempstoreRepository->delete($this->sectionStorage);
101
102     $this->messenger()->addMessage($this->t('Layout Builder has been disabled.'));
103     $form_state->setRedirectUrl($this->getCancelUrl());
104   }
105
106 }