function _book_update_outline

Handles additions and updates to the book outline.

This common helper function performs all additions and updates to the book outline through node addition, node editing, node deletion, or the outline tab.

Parameters

\Drupal\Core\Entity\EntityInterface $node: The node that is being saved, added, deleted, or moved.

Return value

TRUE if the menu link was saved; FALSE otherwise.

4 calls to _book_update_outline()
book_node_insert in drupal/core/modules/book/book.module
Implements hook_node_insert().
book_node_predelete in drupal/core/modules/book/book.module
Implements hook_node_predelete().
book_node_update in drupal/core/modules/book/book.module
Implements hook_node_update().
book_outline_form_submit in drupal/core/modules/book/book.pages.inc
Form submission handler for book_outline_form().

File

drupal/core/modules/book/book.module, line 514
Allows users to create and organize related content in an outline.

Code

function _book_update_outline(EntityInterface $node) {
  if (empty($node->book['bid'])) {
    return FALSE;
  }
  $new = empty($node->book['mlid']);
  $node->book['link_path'] = 'node/' . $node->nid;
  $node->book['link_title'] = $node
    ->label();
  $node->book['parent_mismatch'] = FALSE;

  // The normal case.
  if ($node->book['bid'] == $node->nid) {
    $node->book['plid'] = 0;
    $node->book['menu_name'] = book_menu_name($node->nid);
  }
  else {

    // Check in case the parent is not is this book; the book takes precedence.
    if (!empty($node->book['plid'])) {
      $parent = db_query("SELECT * FROM {book} WHERE mlid = :mlid", array(
        ':mlid' => $node->book['plid'],
      ))
        ->fetchAssoc();
    }
    if (empty($node->book['plid']) || !$parent || $parent['bid'] != $node->book['bid']) {
      $node->book['plid'] = db_query("SELECT mlid FROM {book} WHERE nid = :nid", array(
        ':nid' => $node->book['bid'],
      ))
        ->fetchField();
      $node->book['parent_mismatch'] = TRUE;

      // Likely when JS is disabled.
    }
  }
  $node->book = entity_create('menu_link', $node->book);
  if ($node->book
    ->save()) {
    if ($new) {

      // Insert new.
      db_insert('book')
        ->fields(array(
        'nid' => $node->nid,
        'mlid' => $node->book['mlid'],
        'bid' => $node->book['bid'],
      ))
        ->execute();

      // Reset the cache of stored books.
      drupal_static_reset('book_get_books');
    }
    else {
      if ($node->book['bid'] != db_query("SELECT bid FROM {book} WHERE nid = :nid", array(
        ':nid' => $node->nid,
      ))
        ->fetchField()) {

        // Update the bid for this page and all children.
        book_update_bid($node->book);

        // Reset the cache of stored books.
        drupal_static_reset('book_get_books');
      }
    }
    return TRUE;
  }

  // Failed to save the menu link.
  return FALSE;
}