function book_form_node_form_alter

Same name and namespace in other branches
  1. 7.x modules/book/book.module \book_form_node_form_alter()
  2. 9 core/modules/book/book.module \book_form_node_form_alter()
  3. 8.9.x core/modules/book/book.module \book_form_node_form_alter()
  4. 10 core/modules/book/book.module \book_form_node_form_alter()

Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.

Adds the book form element to the node form.

See also

book_pick_book_nojs_submit()

File

core/modules/book/book.module, line 137

Code

function book_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
    $node = $form_state->getFormObject()
        ->getEntity();
    $account = \Drupal::currentUser();
    $access = $account->hasPermission('administer book outlines');
    if (!$access) {
        if ($account->hasPermission('add content to books') && (!empty($node->book['bid']) && !$node->isNew() || book_type_is_allowed($node->getType()))) {
            // Already in the book hierarchy, or this node type is allowed.
            $access = TRUE;
        }
    }
    if ($access) {
        $collapsed = !($node->isNew() && !empty($node->book['pid']));
        $form = \Drupal::service('book.manager')->addFormElements($form, $form_state, $node, $account, $collapsed);
        // The "js-hide" class hides submit button when JavaScript is enabled.
        $form['book']['pick-book'] = [
            '#type' => 'submit',
            '#value' => t('Change book (update list of parents)'),
            '#submit' => [
                'book_pick_book_nojs_submit',
            ],
            '#weight' => 20,
            '#attributes' => [
                'class' => [
                    'js-hide',
                ],
            ],
        ];
        $form['#entity_builders'][] = 'book_node_builder';
    }
}

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