You are here

public function BookManager::saveBookLink in Drupal 8

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

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

1 call to BookManager::saveBookLink()
BookManager::updateOutline in core/modules/book/src/BookManager.php
Handles additions and updates to the book outline.

File

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

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 = [];
      }
      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'], [
      '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;
}