You are here

public function ConfigureSectionController::build in Layout Builder UX 8

Adds the new section.

Parameters

\Drupal\layout_builder\SectionStorageInterface $section_storage: The section storage.

int $delta: The delta of the section to splice.

string $plugin_id: The plugin ID of the layout to add.

Return value

\Symfony\Component\HttpFoundation\Response|array The controller response.

File

src/Controller/ConfigureSectionController.php, line 100

Class

ConfigureSectionController
Attempts to add a new section, falls back to a form if necessary.

Namespace

Drupal\lb_ux\Controller

Code

public function build(SectionStorageInterface $section_storage, $delta, $plugin_id) {

  // Store any existing messages.
  $old_messages_by_type = $this->messenger
    ->all();

  // Attempt to submit the form with only default values.
  $form_state = new FormState();
  $form_state
    ->addBuildInfo('args', func_get_args());
  $this->formBuilder
    ->submitForm(ConfigureSectionForm::class, $form_state);

  // Clear all new messages and restore the original ones.
  $this->messenger
    ->deleteAll();
  foreach ($old_messages_by_type as $type => $old_messages) {
    foreach ($old_messages as $old_message) {
      $this->messenger
        ->addMessage($old_message, $type);
    }
  }

  // If there are errors, the form must be filled out manually.
  if (FormState::hasAnyErrors()) {

    // Clear any existing errors.
    $form_state
      ->clearErrors();
    $form_state = new FormState();
    $form_state
      ->addBuildInfo('args', func_get_args());
    return $this->formBuilder
      ->buildForm(ConfigureSectionForm::class, $form_state);
  }
  if ($this
    ->isAjax()) {
    return $this
      ->rebuildAndClose($section_storage);
  }
  else {
    $url = $section_storage
      ->getLayoutBuilderUrl();
    return new RedirectResponse($url
      ->setAbsolute()
      ->toString());
  }
}