3 namespace Drupal\layout_builder\Form;
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;
14 * Provides a base class for confirmation forms that rebuild the Layout Builder.
18 abstract class LayoutRebuildConfirmFormBase extends ConfirmFormBase {
20 use AjaxFormHelperTrait;
21 use LayoutRebuildTrait;
24 * The layout tempstore repository.
26 * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
28 protected $layoutTempstoreRepository;
31 * The section storage.
33 * @var \Drupal\layout_builder\SectionStorageInterface
35 protected $sectionStorage;
45 * Constructs a new RemoveSectionForm.
47 * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
48 * The layout tempstore repository.
49 * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
52 public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, ClassResolverInterface $class_resolver) {
53 $this->layoutTempstoreRepository = $layout_tempstore_repository;
54 $this->classResolver = $class_resolver;
60 public static function create(ContainerInterface $container) {
62 $container->get('layout_builder.tempstore_repository'),
63 $container->get('class_resolver')
70 public function getCancelUrl() {
71 return $this->sectionStorage->getLayoutBuilderUrl()->setOption('query', ['layout_is_rebuilding' => TRUE]);
77 public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL) {
78 $this->sectionStorage = $section_storage;
79 $this->delta = $delta;
81 $form = parent::buildForm($form, $form_state);
83 if ($this->isAjax()) {
84 $form['actions']['submit']['#ajax']['callback'] = '::ajaxSubmit';
85 $form['actions']['cancel']['#attributes']['class'][] = 'dialog-cancel';
94 public function submitForm(array &$form, FormStateInterface $form_state) {
95 $this->handleSectionStorage($this->sectionStorage, $form_state);
97 $this->layoutTempstoreRepository->set($this->sectionStorage);
99 $form_state->setRedirectUrl($this->getCancelUrl());
105 protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
106 return $this->rebuildAndClose($this->sectionStorage);
110 * Performs any actions on the section storage before saving.
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.
117 abstract protected function handleSectionStorage(SectionStorageInterface $section_storage, FormStateInterface $form_state);