You are here

function book_node_prepare_form in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/book/book.module \book_node_prepare_form()
  2. 9 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
Allows users to create and organize related content in an outline.

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);
  }
}