You are here

public function BookOutlineForm::save in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/book/src/Form/BookOutlineForm.php \Drupal\book\Form\BookOutlineForm::save()
  2. 9 core/modules/book/src/Form/BookOutlineForm.php \Drupal\book\Form\BookOutlineForm::save()

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

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

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

core/modules/book/src/Form/BookOutlineForm.php, line 109

Class

BookOutlineForm
Displays the book outline form.

Namespace

Drupal\book\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $form_state
    ->setRedirect('entity.node.canonical', [
    'node' => $this->entity
      ->id(),
  ]);
  $book_link = $form_state
    ->getValue('book');
  if (!$book_link['bid']) {
    $this
      ->messenger()
      ->addStatus($this
      ->t('No changes were made'));
    return;
  }
  $this->entity->book = $book_link;
  if ($this->bookManager
    ->updateOutline($this->entity)) {
    if (isset($this->entity->book['parent_mismatch']) && $this->entity->book['parent_mismatch']) {

      // This will usually only happen when JS is disabled.
      $this
        ->messenger()
        ->addStatus($this
        ->t('The post has been added to the selected book. You may now position it relative to other pages.'));
      $form_state
        ->setRedirectUrl($this->entity
        ->toUrl('book-outline-form'));
    }
    else {
      $this
        ->messenger()
        ->addStatus($this
        ->t('The book outline has been updated.'));
    }
  }
  else {
    $this
      ->messenger()
      ->addError($this
      ->t('There was an error adding the post to the book.'));
  }
}