You are here

public function BookManager::updateOutline in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/book/src/BookManager.php \Drupal\book\BookManager::updateOutline()

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\node\NodeInterface $node: The node that is being saved, added, deleted, or moved.

Return value

bool TRUE if the book link was saved; FALSE otherwise.

Overrides BookManagerInterface::updateOutline

1 call to BookManager::updateOutline()
BookManager::deleteFromBook in core/modules/book/src/BookManager.php
Deletes node's entry from book table.

File

core/modules/book/src/BookManager.php, line 265

Class

BookManager
Defines a book manager.

Namespace

Drupal\book

Code

public function updateOutline(NodeInterface $node) {
  if (empty($node->book['bid'])) {
    return FALSE;
  }
  if (!empty($node->book['bid'])) {
    if ($node->book['bid'] == 'new') {

      // New nodes that are their own book.
      $node->book['bid'] = $node
        ->id();
    }
    elseif (!isset($node->book['original_bid'])) {
      $node->book['original_bid'] = $node->book['bid'];
    }
  }

  // Ensure we create a new book link if either the node itself is new, or the
  // bid was selected the first time, so that the original_bid is still empty.
  $new = empty($node->book['nid']) || empty($node->book['original_bid']);
  $node->book['nid'] = $node
    ->id();

  // Create a new book from a node.
  if ($node->book['bid'] == $node
    ->id()) {
    $node->book['pid'] = 0;
  }
  elseif ($node->book['pid'] < 0) {

    // -1 is the default value in BookManager::addParentSelectFormElements().
    // The node save should have set the bid equal to the node ID, but
    // handle it here if it did not.
    $node->book['pid'] = $node->book['bid'];
  }

  // Prevent changes to the book outline if the node being saved is not the
  // default revision.
  $updated = FALSE;
  if (!$new) {
    $original = $this
      ->loadBookLink($node
      ->id(), FALSE);
    if ($node->book['bid'] != $original['bid'] || $node->book['pid'] != $original['pid'] || $node->book['weight'] != $original['weight']) {
      $updated = TRUE;
    }
  }
  if (($new || $updated) && !$node
    ->isDefaultRevision()) {
    return FALSE;
  }
  return $this
    ->saveBookLink($node->book, $new);
}