Loading core/modules/layout_builder/src/Form/ConfigureSectionForm.php +53 −11 Original line number Diff line number Diff line Loading @@ -47,6 +47,13 @@ class ConfigureSectionForm extends FormBase { */ protected $layout; /** * The section being configured. * * @var \Drupal\layout_builder\Section */ protected $section; /** * The plugin form manager. * Loading @@ -68,6 +75,13 @@ class ConfigureSectionForm extends FormBase { */ protected $delta; /** * The plugin ID. * * @var string */ protected $pluginId; /** * Indicates whether the section is being added or updated. * Loading Loading @@ -112,16 +126,15 @@ public function buildForm(array $form, FormStateInterface $form_state, SectionSt $this->sectionStorage = $section_storage; $this->delta = $delta; $this->isUpdate = is_null($plugin_id); $this->pluginId = $plugin_id; $section = $this->getCurrentSection(); if ($this->isUpdate) { $section = $this->sectionStorage->getSection($this->delta); if ($label = $section->getLayoutSettings()['label']) { $form['#title'] = $this->t('Configure @section', ['@section' => $label]); } } else { $section = new Section($plugin_id); } // Passing available contexts to the layout plugin here could result in an // exception since the layout may not have a context mapping for a required // context slot on creation. Loading Loading @@ -179,14 +192,12 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $this->layout->setContextMapping($subform_state->getValue('context_mapping', [])); } $plugin_id = $this->layout->getPluginId(); $configuration = $this->layout->getConfiguration(); if ($this->isUpdate) { $this->sectionStorage->getSection($this->delta)->setLayoutSettings($configuration); } else { $this->sectionStorage->insertSection($this->delta, new Section($plugin_id, $configuration)); $section = $this->getCurrentSection(); $section->setLayoutSettings($configuration); if (!$this->isUpdate) { $this->sectionStorage->insertSection($this->delta, $section); } $this->layoutTempstoreRepository->set($this->sectionStorage); Loading @@ -208,6 +219,8 @@ protected function successfulAjaxSubmit(array $form, FormStateInterface $form_st * * @return \Drupal\Core\Plugin\PluginFormInterface * The plugin form for the layout. * * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException */ protected function getPluginForm(LayoutInterface $layout) { if ($layout instanceof PluginWithFormsInterface) { Loading @@ -222,7 +235,7 @@ protected function getPluginForm(LayoutInterface $layout) { } /** * Retrieve the section storage property. * Retrieves the section storage property. * * @return \Drupal\layout_builder\SectionStorageInterface * The section storage for the current form. Loading @@ -231,4 +244,33 @@ public function getSectionStorage() { return $this->sectionStorage; } /** * Retrieves the layout being modified by the form. * * @return \Drupal\Core\Layout\LayoutInterface|\Drupal\Core\Plugin\PluginFormInterface * The layout for the current form. */ public function getCurrentLayout(): LayoutInterface { return $this->layout; } /** * Retrieves the section being modified by the form. * * @return \Drupal\layout_builder\Section * The section for the current form. */ public function getCurrentSection(): Section { if (!isset($this->section)) { if ($this->isUpdate) { $this->section = $this->sectionStorage->getSection($this->delta); } else { $this->section = new Section($this->pluginId); } } return $this->section; } } core/modules/layout_builder/tests/modules/layout_builder_test/layout_builder_test.module +21 −0 Original line number Diff line number Diff line Loading @@ -97,6 +97,27 @@ function layout_builder_test_form_layout_builder_configure_block_alter(&$form, F ]; } /** * Implements hook_form_BASE_FORM_ID_alter() for layout_builder_configure_section. */ function layout_builder_test_form_layout_builder_configure_section_alter(&$form, FormStateInterface $form_state, $form_id) { /** @var \Drupal\layout_builder\Form\ConfigureSectionForm $form_object */ $form_object = $form_state->getFormObject(); $form['layout_builder_test']['storage'] = [ '#type' => 'item', '#title' => 'Layout Builder Storage: ' . $form_object->getSectionStorage()->getStorageId(), ]; $form['layout_builder_test']['section'] = [ '#type' => 'item', '#title' => 'Layout Builder Section: ' . $form_object->getCurrentSection()->getLayoutId(), ]; $form['layout_builder_test']['layout'] = [ '#type' => 'item', '#title' => 'Layout Builder Layout: ' . $form_object->getCurrentLayout()->getPluginId(), ]; } /** * Implements hook_entity_form_display_alter(). */ Loading core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php +8 −0 Original line number Diff line number Diff line Loading @@ -1167,6 +1167,14 @@ public function testFormAlter() { $assert_session->pageTextContains('Layout Builder Storage: node.bundle_with_section_field.default'); $assert_session->pageTextContains('Layout Builder Section: layout_onecol'); $assert_session->pageTextContains('Layout Builder Component: system_powered_by_block'); $this->drupalGet("$field_ui_prefix/display/default"); $page->clickLink('Manage layout'); $page->clickLink('Add section'); $page->clickLink('One column'); $assert_session->pageTextContains('Layout Builder Storage: node.bundle_with_section_field.default'); $assert_session->pageTextContains('Layout Builder Section: layout_onecol'); $assert_session->pageTextContains('Layout Builder Layout: layout_onecol'); } /** Loading Loading
core/modules/layout_builder/src/Form/ConfigureSectionForm.php +53 −11 Original line number Diff line number Diff line Loading @@ -47,6 +47,13 @@ class ConfigureSectionForm extends FormBase { */ protected $layout; /** * The section being configured. * * @var \Drupal\layout_builder\Section */ protected $section; /** * The plugin form manager. * Loading @@ -68,6 +75,13 @@ class ConfigureSectionForm extends FormBase { */ protected $delta; /** * The plugin ID. * * @var string */ protected $pluginId; /** * Indicates whether the section is being added or updated. * Loading Loading @@ -112,16 +126,15 @@ public function buildForm(array $form, FormStateInterface $form_state, SectionSt $this->sectionStorage = $section_storage; $this->delta = $delta; $this->isUpdate = is_null($plugin_id); $this->pluginId = $plugin_id; $section = $this->getCurrentSection(); if ($this->isUpdate) { $section = $this->sectionStorage->getSection($this->delta); if ($label = $section->getLayoutSettings()['label']) { $form['#title'] = $this->t('Configure @section', ['@section' => $label]); } } else { $section = new Section($plugin_id); } // Passing available contexts to the layout plugin here could result in an // exception since the layout may not have a context mapping for a required // context slot on creation. Loading Loading @@ -179,14 +192,12 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $this->layout->setContextMapping($subform_state->getValue('context_mapping', [])); } $plugin_id = $this->layout->getPluginId(); $configuration = $this->layout->getConfiguration(); if ($this->isUpdate) { $this->sectionStorage->getSection($this->delta)->setLayoutSettings($configuration); } else { $this->sectionStorage->insertSection($this->delta, new Section($plugin_id, $configuration)); $section = $this->getCurrentSection(); $section->setLayoutSettings($configuration); if (!$this->isUpdate) { $this->sectionStorage->insertSection($this->delta, $section); } $this->layoutTempstoreRepository->set($this->sectionStorage); Loading @@ -208,6 +219,8 @@ protected function successfulAjaxSubmit(array $form, FormStateInterface $form_st * * @return \Drupal\Core\Plugin\PluginFormInterface * The plugin form for the layout. * * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException */ protected function getPluginForm(LayoutInterface $layout) { if ($layout instanceof PluginWithFormsInterface) { Loading @@ -222,7 +235,7 @@ protected function getPluginForm(LayoutInterface $layout) { } /** * Retrieve the section storage property. * Retrieves the section storage property. * * @return \Drupal\layout_builder\SectionStorageInterface * The section storage for the current form. Loading @@ -231,4 +244,33 @@ public function getSectionStorage() { return $this->sectionStorage; } /** * Retrieves the layout being modified by the form. * * @return \Drupal\Core\Layout\LayoutInterface|\Drupal\Core\Plugin\PluginFormInterface * The layout for the current form. */ public function getCurrentLayout(): LayoutInterface { return $this->layout; } /** * Retrieves the section being modified by the form. * * @return \Drupal\layout_builder\Section * The section for the current form. */ public function getCurrentSection(): Section { if (!isset($this->section)) { if ($this->isUpdate) { $this->section = $this->sectionStorage->getSection($this->delta); } else { $this->section = new Section($this->pluginId); } } return $this->section; } }
core/modules/layout_builder/tests/modules/layout_builder_test/layout_builder_test.module +21 −0 Original line number Diff line number Diff line Loading @@ -97,6 +97,27 @@ function layout_builder_test_form_layout_builder_configure_block_alter(&$form, F ]; } /** * Implements hook_form_BASE_FORM_ID_alter() for layout_builder_configure_section. */ function layout_builder_test_form_layout_builder_configure_section_alter(&$form, FormStateInterface $form_state, $form_id) { /** @var \Drupal\layout_builder\Form\ConfigureSectionForm $form_object */ $form_object = $form_state->getFormObject(); $form['layout_builder_test']['storage'] = [ '#type' => 'item', '#title' => 'Layout Builder Storage: ' . $form_object->getSectionStorage()->getStorageId(), ]; $form['layout_builder_test']['section'] = [ '#type' => 'item', '#title' => 'Layout Builder Section: ' . $form_object->getCurrentSection()->getLayoutId(), ]; $form['layout_builder_test']['layout'] = [ '#type' => 'item', '#title' => 'Layout Builder Layout: ' . $form_object->getCurrentLayout()->getPluginId(), ]; } /** * Implements hook_entity_form_display_alter(). */ Loading
core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php +8 −0 Original line number Diff line number Diff line Loading @@ -1167,6 +1167,14 @@ public function testFormAlter() { $assert_session->pageTextContains('Layout Builder Storage: node.bundle_with_section_field.default'); $assert_session->pageTextContains('Layout Builder Section: layout_onecol'); $assert_session->pageTextContains('Layout Builder Component: system_powered_by_block'); $this->drupalGet("$field_ui_prefix/display/default"); $page->clickLink('Manage layout'); $page->clickLink('Add section'); $page->clickLink('One column'); $assert_session->pageTextContains('Layout Builder Storage: node.bundle_with_section_field.default'); $assert_session->pageTextContains('Layout Builder Section: layout_onecol'); $assert_session->pageTextContains('Layout Builder Layout: layout_onecol'); } /** Loading