function BookAdminEditForm::submitForm

Same name in other branches
  1. 9 core/modules/book/src/Form/BookAdminEditForm.php \Drupal\book\Form\BookAdminEditForm::submitForm()
  2. 8.9.x core/modules/book/src/Form/BookAdminEditForm.php \Drupal\book\Form\BookAdminEditForm::submitForm()
  3. 11.x core/modules/book/src/Form/BookAdminEditForm.php \Drupal\book\Form\BookAdminEditForm::submitForm()

Overrides FormInterface::submitForm

File

core/modules/book/src/Form/BookAdminEditForm.php, line 107

Class

BookAdminEditForm
Provides a form for administering a single book's hierarchy.

Namespace

Drupal\book\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
    // Save elements in the same order as defined in post rather than the form.
    // This ensures parents are updated before their children, preventing orphans.
    $user_input = $form_state->getUserInput();
    if (isset($user_input['table'])) {
        $order = array_flip(array_keys($user_input['table']));
        $form['table'] = array_merge($order, $form['table']);
        foreach (Element::children($form['table']) as $key) {
            if ($form['table'][$key]['#item']) {
                $row = $form['table'][$key];
                $values = $form_state->getValue([
                    'table',
                    $key,
                ]);
                // Update menu item if moved.
                if ($row['parent']['pid']['#default_value'] != $values['pid'] || $row['weight']['#default_value'] != $values['weight']) {
                    $link = $this->bookManager
                        ->loadBookLink($values['nid'], FALSE);
                    $link['weight'] = $values['weight'];
                    $link['pid'] = $values['pid'];
                    $this->bookManager
                        ->saveBookLink($link, FALSE);
                }
                // Update the title if changed.
                if ($row['title']['#default_value'] != $values['title']) {
                    $node = $this->nodeStorage
                        ->load($values['nid']);
                    $node = $this->entityRepository
                        ->getTranslationFromContext($node);
                    $node->revision_log = $this->t('Title changed from %original to %current.', [
                        '%original' => $node->label(),
                        '%current' => $values['title'],
                    ]);
                    $node->title = $values['title'];
                    $node->book['link_title'] = $values['title'];
                    $node->setNewRevision();
                    $node->save();
                    $this->logger('content')
                        ->info('book: updated %title.', [
                        '%title' => $node->label(),
                        'link' => $node->toLink($this->t('View'))
                            ->toString(),
                    ]);
                }
            }
        }
    }
    $this->messenger()
        ->addStatus($this->t('Updated book %title.', [
        '%title' => $form['#node']->label(),
    ]));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.