3 namespace Drupal\layout_builder\Form;
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;
21 * Provides a form for configuring a layout section.
25 class ConfigureSectionForm extends FormBase {
27 use AjaxFormHelperTrait;
28 use LayoutRebuildTrait;
31 * The layout tempstore repository.
33 * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
35 protected $layoutTempstoreRepository;
38 * The plugin being configured.
40 * @var \Drupal\Core\Layout\LayoutInterface|\Drupal\Core\Plugin\PluginFormInterface
45 * The plugin form manager.
47 * @var \Drupal\Core\Plugin\PluginFormFactoryInterface
49 protected $pluginFormFactory;
52 * The section storage.
54 * @var \Drupal\layout_builder\SectionStorageInterface
56 protected $sectionStorage;
66 * Indicates whether the section is being added or updated.
73 * Constructs a new ConfigureSectionForm.
75 * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
76 * The layout tempstore repository.
77 * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
79 * @param \Drupal\Core\Plugin\PluginFormFactoryInterface $plugin_form_manager
80 * The plugin form manager.
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;
91 public static function create(ContainerInterface $container) {
93 $container->get('layout_builder.tempstore_repository'),
94 $container->get('class_resolver'),
95 $container->get('plugin_form.factory')
102 public function getFormId() {
103 return 'layout_builder_configure_section';
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);
114 if ($this->isUpdate) {
115 $section = $this->sectionStorage->getSection($this->delta);
118 $section = new Section($plugin_id);
120 $this->layout = $section->getLayout();
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);
127 $form['actions']['submit'] = [
129 '#value' => $this->isUpdate ? $this->t('Update') : $this->t('Add section'),
130 '#button_type' => 'primary',
132 if ($this->isAjax()) {
133 $form['actions']['submit']['#ajax']['callback'] = '::ajaxSubmit';
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);
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);
155 $plugin_id = $this->layout->getPluginId();
156 $configuration = $this->layout->getConfiguration();
158 if ($this->isUpdate) {
159 $this->sectionStorage->getSection($this->delta)->setLayoutSettings($configuration);
162 $this->sectionStorage->insertSection($this->delta, new Section($plugin_id, $configuration));
165 $this->layoutTempstoreRepository->set($this->sectionStorage);
166 $form_state->setRedirectUrl($this->sectionStorage->getLayoutBuilderUrl());
172 protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
173 return $this->rebuildAndClose($this->sectionStorage);
177 * Retrieves the plugin form for a given layout.
179 * @param \Drupal\Core\Layout\LayoutInterface $layout
182 * @return \Drupal\Core\Plugin\PluginFormInterface
183 * The plugin form for the layout.
185 protected function getPluginForm(LayoutInterface $layout) {
186 if ($layout instanceof PluginWithFormsInterface) {
187 return $this->pluginFormFactory->createInstance($layout, 'configure');
190 if ($layout instanceof PluginFormInterface) {
194 throw new \InvalidArgumentException(sprintf('The "%s" layout does not provide a configuration form', $layout->getPluginId()));