BookManager::saveBookLink

public BookManager::saveBookLink(array $link, $new)

Saves a single book entry.

Parameters

array $link: The link data to save.

bool $new: Is this a new book.

Return value

array The book data of that node.

Overrides BookManagerInterface::saveBookLink

File

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

Class

BookManager
Defines a book manager.

Namespace

Drupal\book

Code

public function saveBookLink(array $link, $new) {
  // Keep track of Book IDs for cache clear.
  $affected_bids[$link['bid']] = $link['bid'];
  $link += $this->getLinkDefaults($link['nid']);
  if ($new) {
    // Insert new.
    $parents = $this->getBookParents($link, (array) $this->loadBookLink($link['pid'], FALSE));
    $this->bookOutlineStorage->insert($link, $parents);

    // Update the has_children status of the parent.
    $this->updateParent($link);
  }
  else {
    $original = $this->loadBookLink($link['nid'], FALSE);
    // Using the Book ID as the key keeps this unique.
    $affected_bids[$original['bid']] = $original['bid'];
    // Handle links that are moving.
    if ($link['bid'] != $original['bid'] || $link['pid'] != $original['pid']) {
      // Update the bid for this page and all children.
      if ($link['pid'] == 0) {
        $link['depth'] = 1;
        $parent = array();
      }
      // In case the form did not specify a proper PID we use the BID as new
      // parent.
      elseif (($parent_link = $this->loadBookLink($link['pid'], FALSE)) && $parent_link['bid'] != $link['bid']) {
        $link['pid'] = $link['bid'];
        $parent = $this->loadBookLink($link['pid'], FALSE);
        $link['depth'] = $parent['depth'] + 1;
      }
      else {
        $parent = $this->loadBookLink($link['pid'], FALSE);
        $link['depth'] = $parent['depth'] + 1;
      }
      $this->setParents($link, $parent);
      $this->moveChildren($link, $original);

      // Update the has_children status of the original parent.
      $this->updateOriginalParent($original);
      // Update the has_children status of the new parent.
      $this->updateParent($link);
    }
    // Update the weight and pid.
    $this->bookOutlineStorage->update($link['nid'], array(
      'weight' => $link['weight'],
      'pid' => $link['pid'],
      'bid' => $link['bid'],
    ));
  }
  $cache_tags = [];
  foreach ($affected_bids as $bid) {
    $cache_tags[] = 'bid:' . $bid;
  }
  Cache::invalidateTags($cache_tags);
  return $link;
}
doc_Drupal
2016-10-29 08:48:12
Comments
Leave a Comment

Please login to continue.