3 namespace Drupal\layout_builder\Form;
5 use Drupal\Core\DependencyInjection\ClassResolverInterface;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Form\SubformState;
9 use Drupal\Core\Layout\LayoutInterface;
10 use Drupal\Core\Plugin\PluginFormFactoryInterface;
11 use Drupal\Core\Plugin\PluginFormInterface;
12 use Drupal\Core\Plugin\PluginWithFormsInterface;
13 use Drupal\layout_builder\Controller\LayoutRebuildTrait;
14 use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
15 use Drupal\layout_builder\Section;
16 use Drupal\layout_builder\SectionStorageInterface;
17 use Symfony\Component\DependencyInjection\ContainerInterface;
20 * Provides a form for configuring a layout section.
24 class ConfigureSectionForm extends FormBase {
26 use AjaxFormHelperTrait;
27 use LayoutRebuildTrait;
30 * The layout tempstore repository.
32 * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
34 protected $layoutTempstoreRepository;
37 * The plugin being configured.
39 * @var \Drupal\Core\Layout\LayoutInterface|\Drupal\Core\Plugin\PluginFormInterface
44 * The plugin form manager.
46 * @var \Drupal\Core\Plugin\PluginFormFactoryInterface
48 protected $pluginFormFactory;
51 * The section storage.
53 * @var \Drupal\layout_builder\SectionStorageInterface
55 protected $sectionStorage;
65 * Indicates whether the section is being added or updated.
72 * Constructs a new ConfigureSectionForm.
74 * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
75 * The layout tempstore repository.
76 * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
78 * @param \Drupal\Core\Plugin\PluginFormFactoryInterface $plugin_form_manager
79 * The plugin form manager.
81 public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, ClassResolverInterface $class_resolver, PluginFormFactoryInterface $plugin_form_manager) {
82 $this->layoutTempstoreRepository = $layout_tempstore_repository;
83 $this->classResolver = $class_resolver;
84 $this->pluginFormFactory = $plugin_form_manager;
90 public static function create(ContainerInterface $container) {
92 $container->get('layout_builder.tempstore_repository'),
93 $container->get('class_resolver'),
94 $container->get('plugin_form.factory')
101 public function getFormId() {
102 return 'layout_builder_configure_section';
108 public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL, $plugin_id = NULL) {
109 $this->sectionStorage = $section_storage;
110 $this->delta = $delta;
111 $this->isUpdate = is_null($plugin_id);
113 if ($this->isUpdate) {
114 $section = $this->sectionStorage->getSection($this->delta);
117 $section = new Section($plugin_id);
119 $this->layout = $section->getLayout();
121 $form['#tree'] = TRUE;
122 $form['layout_settings'] = [];
123 $subform_state = SubformState::createForSubform($form['layout_settings'], $form, $form_state);
124 $form['layout_settings'] = $this->getPluginForm($this->layout)->buildConfigurationForm($form['layout_settings'], $subform_state);
126 $form['actions']['submit'] = [
128 '#value' => $this->isUpdate ? $this->t('Update') : $this->t('Add section'),
129 '#button_type' => 'primary',
131 if ($this->isAjax()) {
132 $form['actions']['submit']['#ajax']['callback'] = '::ajaxSubmit';
141 public function validateForm(array &$form, FormStateInterface $form_state) {
142 $subform_state = SubformState::createForSubform($form['layout_settings'], $form, $form_state);
143 $this->getPluginForm($this->layout)->validateConfigurationForm($form['layout_settings'], $subform_state);
149 public function submitForm(array &$form, FormStateInterface $form_state) {
150 // Call the plugin submit handler.
151 $subform_state = SubformState::createForSubform($form['layout_settings'], $form, $form_state);
152 $this->getPluginForm($this->layout)->submitConfigurationForm($form['layout_settings'], $subform_state);
154 $plugin_id = $this->layout->getPluginId();
155 $configuration = $this->layout->getConfiguration();
157 if ($this->isUpdate) {
158 $this->sectionStorage->getSection($this->delta)->setLayoutSettings($configuration);
161 $this->sectionStorage->insertSection($this->delta, new Section($plugin_id, $configuration));
164 $this->layoutTempstoreRepository->set($this->sectionStorage);
165 $form_state->setRedirectUrl($this->sectionStorage->getLayoutBuilderUrl());
171 protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
172 return $this->rebuildAndClose($this->sectionStorage);
176 * Retrieves the plugin form for a given layout.
178 * @param \Drupal\Core\Layout\LayoutInterface $layout
181 * @return \Drupal\Core\Plugin\PluginFormInterface
182 * The plugin form for the layout.
184 protected function getPluginForm(LayoutInterface $layout) {
185 if ($layout instanceof PluginWithFormsInterface) {
186 return $this->pluginFormFactory->createInstance($layout, 'configure');
189 if ($layout instanceof PluginFormInterface) {
193 throw new \InvalidArgumentException(sprintf('The "%s" layout does not provide a configuration form', $layout->getPluginId()));