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