You are here

public function ConfigureSectionForm::buildForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/layout_builder/src/Form/ConfigureSectionForm.php \Drupal\layout_builder\Form\ConfigureSectionForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

core/modules/layout_builder/src/Form/ConfigureSectionForm.php, line 111

Class

ConfigureSectionForm
Provides a form for configuring a layout section.

Namespace

Drupal\layout_builder\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL, $plugin_id = NULL) {
  $this->sectionStorage = $section_storage;
  $this->delta = $delta;
  $this->isUpdate = is_null($plugin_id);
  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.
  $this->layout = $section
    ->getLayout();
  $form_state
    ->setTemporaryValue('gathered_contexts', $this
    ->getPopulatedContexts($this->sectionStorage));
  $form['#tree'] = TRUE;
  $form['layout_settings'] = [];
  $subform_state = SubformState::createForSubform($form['layout_settings'], $form, $form_state);
  $form['layout_settings'] = $this
    ->getPluginForm($this->layout)
    ->buildConfigurationForm($form['layout_settings'], $subform_state);
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this->isUpdate ? $this
      ->t('Update') : $this
      ->t('Add section'),
    '#button_type' => 'primary',
  ];
  if ($this
    ->isAjax()) {
    $form['actions']['submit']['#ajax']['callback'] = '::ajaxSubmit';

    // @todo static::ajaxSubmit() requires data-drupal-selector to be the same
    //   between the various Ajax requests. A bug in
    //   \Drupal\Core\Form\FormBuilder prevents that from happening unless
    //   $form['#id'] is also the same. Normally, #id is set to a unique HTML
    //   ID via Html::getUniqueId(), but here we bypass that in order to work
    //   around the data-drupal-selector bug. This is okay so long as we
    //   assume that this form only ever occurs once on a page. Remove this
    //   workaround in https://www.drupal.org/node/2897377.
    $form['#id'] = Html::getId($form_state
      ->getBuildInfo()['form_id']);
  }
  $target_highlight_id = $this->isUpdate ? $this
    ->sectionUpdateHighlightId($delta) : $this
    ->sectionAddHighlightId($delta);
  $form['#attributes']['data-layout-builder-target-highlight-id'] = $target_highlight_id;

  // Mark this as an administrative page for JavaScript ("Back to site" link).
  $form['#attached']['drupalSettings']['path']['currentPathIsAdmin'] = TRUE;
  return $form;
}