94c2a4eb4c2b35d0b81d8dce2ad6af2bb2f7bd6c
[yaffs-website] / web / core / modules / layout_builder / src / Form / ConfigureSectionForm.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\FormBase;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Form\SubformState;
10 use Drupal\Core\Layout\LayoutInterface;
11 use Drupal\Core\Plugin\PluginFormFactoryInterface;
12 use Drupal\Core\Plugin\PluginFormInterface;
13 use Drupal\Core\Plugin\PluginWithFormsInterface;
14 use Drupal\layout_builder\Controller\LayoutRebuildTrait;
15 use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
16 use Drupal\layout_builder\Section;
17 use Drupal\layout_builder\SectionStorageInterface;
18 use Symfony\Component\DependencyInjection\ContainerInterface;
19
20 /**
21  * Provides a form for configuring a layout section.
22  *
23  * @internal
24  */
25 class ConfigureSectionForm extends FormBase {
26
27   use AjaxFormHelperTrait;
28   use LayoutRebuildTrait;
29
30   /**
31    * The layout tempstore repository.
32    *
33    * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
34    */
35   protected $layoutTempstoreRepository;
36
37   /**
38    * The plugin being configured.
39    *
40    * @var \Drupal\Core\Layout\LayoutInterface|\Drupal\Core\Plugin\PluginFormInterface
41    */
42   protected $layout;
43
44   /**
45    * The plugin form manager.
46    *
47    * @var \Drupal\Core\Plugin\PluginFormFactoryInterface
48    */
49   protected $pluginFormFactory;
50
51   /**
52    * The section storage.
53    *
54    * @var \Drupal\layout_builder\SectionStorageInterface
55    */
56   protected $sectionStorage;
57
58   /**
59    * The field delta.
60    *
61    * @var int
62    */
63   protected $delta;
64
65   /**
66    * Indicates whether the section is being added or updated.
67    *
68    * @var bool
69    */
70   protected $isUpdate;
71
72   /**
73    * Constructs a new ConfigureSectionForm.
74    *
75    * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
76    *   The layout tempstore repository.
77    * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
78    *   The class resolver.
79    * @param \Drupal\Core\Plugin\PluginFormFactoryInterface $plugin_form_manager
80    *   The plugin form manager.
81    */
82   public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, ClassResolverInterface $class_resolver, PluginFormFactoryInterface $plugin_form_manager) {
83     $this->layoutTempstoreRepository = $layout_tempstore_repository;
84     $this->classResolver = $class_resolver;
85     $this->pluginFormFactory = $plugin_form_manager;
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public static function create(ContainerInterface $container) {
92     return new static(
93       $container->get('layout_builder.tempstore_repository'),
94       $container->get('class_resolver'),
95       $container->get('plugin_form.factory')
96     );
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   public function getFormId() {
103     return 'layout_builder_configure_section';
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL, $plugin_id = NULL) {
110     $this->sectionStorage = $section_storage;
111     $this->delta = $delta;
112     $this->isUpdate = is_null($plugin_id);
113
114     if ($this->isUpdate) {
115       $section = $this->sectionStorage->getSection($this->delta);
116     }
117     else {
118       $section = new Section($plugin_id);
119     }
120     $this->layout = $section->getLayout();
121
122     $form['#tree'] = TRUE;
123     $form['layout_settings'] = [];
124     $subform_state = SubformState::createForSubform($form['layout_settings'], $form, $form_state);
125     $form['layout_settings'] = $this->getPluginForm($this->layout)->buildConfigurationForm($form['layout_settings'], $subform_state);
126
127     $form['actions']['submit'] = [
128       '#type' => 'submit',
129       '#value' => $this->isUpdate ? $this->t('Update') : $this->t('Add section'),
130       '#button_type' => 'primary',
131     ];
132     if ($this->isAjax()) {
133       $form['actions']['submit']['#ajax']['callback'] = '::ajaxSubmit';
134     }
135
136     return $form;
137   }
138
139   /**
140    * {@inheritdoc}
141    */
142   public function validateForm(array &$form, FormStateInterface $form_state) {
143     $subform_state = SubformState::createForSubform($form['layout_settings'], $form, $form_state);
144     $this->getPluginForm($this->layout)->validateConfigurationForm($form['layout_settings'], $subform_state);
145   }
146
147   /**
148    * {@inheritdoc}
149    */
150   public function submitForm(array &$form, FormStateInterface $form_state) {
151     // Call the plugin submit handler.
152     $subform_state = SubformState::createForSubform($form['layout_settings'], $form, $form_state);
153     $this->getPluginForm($this->layout)->submitConfigurationForm($form['layout_settings'], $subform_state);
154
155     $plugin_id = $this->layout->getPluginId();
156     $configuration = $this->layout->getConfiguration();
157
158     if ($this->isUpdate) {
159       $this->sectionStorage->getSection($this->delta)->setLayoutSettings($configuration);
160     }
161     else {
162       $this->sectionStorage->insertSection($this->delta, new Section($plugin_id, $configuration));
163     }
164
165     $this->layoutTempstoreRepository->set($this->sectionStorage);
166     $form_state->setRedirectUrl($this->sectionStorage->getLayoutBuilderUrl());
167   }
168
169   /**
170    * {@inheritdoc}
171    */
172   protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
173     return $this->rebuildAndClose($this->sectionStorage);
174   }
175
176   /**
177    * Retrieves the plugin form for a given layout.
178    *
179    * @param \Drupal\Core\Layout\LayoutInterface $layout
180    *   The layout plugin.
181    *
182    * @return \Drupal\Core\Plugin\PluginFormInterface
183    *   The plugin form for the layout.
184    */
185   protected function getPluginForm(LayoutInterface $layout) {
186     if ($layout instanceof PluginWithFormsInterface) {
187       return $this->pluginFormFactory->createInstance($layout, 'configure');
188     }
189
190     if ($layout instanceof PluginFormInterface) {
191       return $layout;
192     }
193
194     throw new \InvalidArgumentException(sprintf('The "%s" layout does not provide a configuration form', $layout->getPluginId()));
195   }
196
197 }