function book_node_prepare_form

Same name in other branches
  1. 9 core/modules/book/book.module \book_node_prepare_form()
  2. 10 core/modules/book/book.module \book_node_prepare_form()
  3. 11.x core/modules/book/book.module \book_node_prepare_form()

Implements hook_ENTITY_TYPE_prepare_form() for node entities.

File

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

Code

function book_node_prepare_form(NodeInterface $node, $operation, FormStateInterface $form_state) {
    
    /** @var \Drupal\book\BookManagerInterface $book_manager */
    $book_manager = \Drupal::service('book.manager');
    // Prepare defaults for the add/edit form.
    $account = \Drupal::currentUser();
    if (empty($node->book) && ($account->hasPermission('add content to books') || $account->hasPermission('administer book outlines'))) {
        $node->book = [];
        $query = \Drupal::request()->query;
        if ($node->isNew() && !is_null($query->get('parent')) && is_numeric($query->get('parent'))) {
            // Handle "Add child page" links:
            $parent = $book_manager->loadBookLink($query->get('parent'), TRUE);
            if ($parent && $parent['access']) {
                $node->book['bid'] = $parent['bid'];
                $node->book['pid'] = $parent['nid'];
            }
        }
        // Set defaults.
        $node_ref = !$node->isNew() ? $node->id() : 'new';
        $node->book += $book_manager->getLinkDefaults($node_ref);
    }
    else {
        if (isset($node->book['bid']) && !isset($node->book['original_bid'])) {
            $node->book['original_bid'] = $node->book['bid'];
        }
    }
    // Find the depth limit for the parent select.
    if (isset($node->book['bid']) && !isset($node->book['parent_depth_limit'])) {
        $node->book['parent_depth_limit'] = $book_manager->getParentDepthLimit($node->book);
    }
}

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