function BookManager::saveBookLink

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

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 846

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.