3 namespace Drupal\layout_builder\Form;
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;
15 * Provides a base class for confirmation forms that rebuild the Layout Builder.
19 abstract class LayoutRebuildConfirmFormBase extends ConfirmFormBase {
21 use AjaxFormHelperTrait;
22 use LayoutRebuildTrait;
25 * The layout tempstore repository.
27 * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
29 protected $layoutTempstoreRepository;
32 * The section storage.
34 * @var \Drupal\layout_builder\SectionStorageInterface
36 protected $sectionStorage;
46 * Constructs a new RemoveSectionForm.
48 * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
49 * The layout tempstore repository.
50 * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
53 public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, ClassResolverInterface $class_resolver) {
54 $this->layoutTempstoreRepository = $layout_tempstore_repository;
55 $this->classResolver = $class_resolver;
61 public static function create(ContainerInterface $container) {
63 $container->get('layout_builder.tempstore_repository'),
64 $container->get('class_resolver')
71 public function getCancelUrl() {
72 return $this->sectionStorage->getLayoutBuilderUrl()->setOption('query', ['layout_is_rebuilding' => TRUE]);
78 public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL) {
79 $this->sectionStorage = $section_storage;
80 $this->delta = $delta;
82 $form = parent::buildForm($form, $form_state);
84 if ($this->isAjax()) {
85 $form['actions']['submit']['#ajax']['callback'] = '::ajaxSubmit';
86 $form['actions']['cancel']['#attributes']['class'][] = 'dialog-cancel';
95 public function submitForm(array &$form, FormStateInterface $form_state) {
96 $this->handleSectionStorage($this->sectionStorage, $form_state);
98 $this->layoutTempstoreRepository->set($this->sectionStorage);
100 $form_state->setRedirectUrl($this->getCancelUrl());
106 protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
107 return $this->rebuildAndClose($this->sectionStorage);
111 * Performs any actions on the section storage before saving.
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.
118 abstract protected function handleSectionStorage(SectionStorageInterface $section_storage, FormStateInterface $form_state);