function BookManager::saveBookLink

Same name in this branch
  1. 10 core/modules/book/src/ProxyClass/BookManager.php \Drupal\book\ProxyClass\BookManager::saveBookLink()
Same name and namespace in other branches
  1. 9 core/modules/book/src/BookManager.php \Drupal\book\BookManager::saveBookLink()
  2. 8.9.x core/modules/book/src/BookManager.php \Drupal\book\BookManager::saveBookLink()
  3. 11.x core/modules/book/src/ProxyClass/BookManager.php \Drupal\book\ProxyClass\BookManager::saveBookLink()
  4. 11.x core/modules/book/src/BookManager.php \Drupal\book\BookManager::saveBookLink()

Saves a link for a single book entry to the book.

Parameters

array $link: The link data to save. $link['nid'] must be set. Other keys in this array get default values from \Drupal\book\BookManagerInterface::getLinkDefaults(). The array keys available to be set are documented in \Drupal\book\BookOutlineStorageInterface::loadMultiple().

bool $new: Whether this is a link to a new book entry.

Return value

array The book entry link information. This is $link with values added or updated.

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 832

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.