Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / layout_builder / src / Form / LayoutRebuildConfirmFormBase.php
1 <?php
2
3 namespace Drupal\layout_builder\Form;
4
5 use Drupal\Core\DependencyInjection\ClassResolverInterface;
6 use Drupal\Core\Form\ConfirmFormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\layout_builder\Controller\LayoutRebuildTrait;
9 use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
10 use Drupal\layout_builder\SectionStorageInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Provides a base class for confirmation forms that rebuild the Layout Builder.
15  *
16  * @internal
17  */
18 abstract class LayoutRebuildConfirmFormBase extends ConfirmFormBase {
19
20   use AjaxFormHelperTrait;
21   use LayoutRebuildTrait;
22
23   /**
24    * The layout tempstore repository.
25    *
26    * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
27    */
28   protected $layoutTempstoreRepository;
29
30   /**
31    * The section storage.
32    *
33    * @var \Drupal\layout_builder\SectionStorageInterface
34    */
35   protected $sectionStorage;
36
37   /**
38    * The field delta.
39    *
40    * @var int
41    */
42   protected $delta;
43
44   /**
45    * Constructs a new RemoveSectionForm.
46    *
47    * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
48    *   The layout tempstore repository.
49    * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
50    *   The class resolver.
51    */
52   public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, ClassResolverInterface $class_resolver) {
53     $this->layoutTempstoreRepository = $layout_tempstore_repository;
54     $this->classResolver = $class_resolver;
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public static function create(ContainerInterface $container) {
61     return new static(
62       $container->get('layout_builder.tempstore_repository'),
63       $container->get('class_resolver')
64     );
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function getCancelUrl() {
71     return $this->sectionStorage->getLayoutBuilderUrl()->setOption('query', ['layout_is_rebuilding' => TRUE]);
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL) {
78     $this->sectionStorage = $section_storage;
79     $this->delta = $delta;
80
81     $form = parent::buildForm($form, $form_state);
82
83     if ($this->isAjax()) {
84       $form['actions']['submit']['#ajax']['callback'] = '::ajaxSubmit';
85       $form['actions']['cancel']['#attributes']['class'][] = 'dialog-cancel';
86     }
87
88     return $form;
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function submitForm(array &$form, FormStateInterface $form_state) {
95     $this->handleSectionStorage($this->sectionStorage, $form_state);
96
97     $this->layoutTempstoreRepository->set($this->sectionStorage);
98
99     $form_state->setRedirectUrl($this->getCancelUrl());
100   }
101
102   /**
103    * {@inheritdoc}
104    */
105   protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
106     return $this->rebuildAndClose($this->sectionStorage);
107   }
108
109   /**
110    * Performs any actions on the section storage before saving.
111    *
112    * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
113    *   The section storage.
114    * @param \Drupal\Core\Form\FormStateInterface $form_state
115    *   The current state of the form.
116    */
117   abstract protected function handleSectionStorage(SectionStorageInterface $section_storage, FormStateInterface $form_state);
118
119 }