Verified Commit 0660f7a2 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3044117 by e.escribano, segovia94, bkosborne, Webbeh, zterry95,...

Issue #3044117 by e.escribano, segovia94, bkosborne, Webbeh, zterry95, tim.plunkett, emerham: Add getter for layout object in Layout Builder's ConfigureSectionForm

(cherry picked from commit 29849ead)
parent 409a884b
Loading
Loading
Loading
Loading
+53 −11
Original line number Diff line number Diff line
@@ -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.
   *
@@ -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.
   *
@@ -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.
@@ -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);
@@ -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) {
@@ -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.
@@ -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;
  }

}
+21 −0
Original line number Diff line number Diff line
@@ -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().
 */
+8 −0
Original line number Diff line number Diff line
@@ -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');
  }

  /**