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\DefaultsSectionStorageInterface;
9 use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
10 use Drupal\layout_builder\SectionStorageInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Disables Layout Builder for a given default.
16 class LayoutBuilderDisableForm extends ConfirmFormBase {
19 * The layout tempstore repository.
21 * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
23 protected $layoutTempstoreRepository;
26 * The section storage.
28 * @var \Drupal\layout_builder\DefaultsSectionStorageInterface
30 protected $sectionStorage;
33 * Constructs a new RevertOverridesForm.
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.
40 public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, MessengerInterface $messenger) {
41 $this->layoutTempstoreRepository = $layout_tempstore_repository;
42 $this->setMessenger($messenger);
48 public static function create(ContainerInterface $container) {
50 $container->get('layout_builder.tempstore_repository'),
51 $container->get('messenger')
58 public function getFormId() {
59 return 'layout_builder_disable_form';
65 public function getQuestion() {
66 return $this->t('Are you sure you want to disable Layout Builder?');
72 public function getDescription() {
73 return $this->t('All customizations will be removed. This action cannot be undone.');
79 public function getCancelUrl() {
80 return $this->sectionStorage->getRedirectUrl();
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()));
91 $this->sectionStorage = $section_storage;
92 return parent::buildForm($form, $form_state);
98 public function submitForm(array &$form, FormStateInterface $form_state) {
99 $this->sectionStorage->disableLayoutBuilder()->save();
100 $this->layoutTempstoreRepository->delete($this->sectionStorage);
102 $this->messenger()->addMessage($this->t('Layout Builder has been disabled.'));
103 $form_state->setRedirectUrl($this->getCancelUrl());