function BookOutline::prevLink

Same name in other branches
  1. 9 core/modules/book/src/BookOutline.php \Drupal\book\BookOutline::prevLink()
  2. 8.9.x core/modules/book/src/BookOutline.php \Drupal\book\BookOutline::prevLink()
  3. 10 core/modules/book/src/BookOutline.php \Drupal\book\BookOutline::prevLink()

Fetches the book link for the previous page of the book.

Parameters

array $book_link: A fully loaded book link that is part of the book hierarchy.

Return value

array A fully loaded book link for the page before the one represented in $book_link.

File

core/modules/book/src/BookOutline.php, line 37

Class

BookOutline
Provides handling to render the book outline.

Namespace

Drupal\book

Code

public function prevLink(array $book_link) {
    // If the parent is zero, we are at the start of a book.
    if ($book_link['pid'] == 0) {
        return NULL;
    }
    $flat = $this->bookManager
        ->bookTreeGetFlat($book_link);
    reset($flat);
    $curr = NULL;
    do {
        $prev = $curr;
        $key = key($flat);
        $curr = current($flat);
        next($flat);
    } while ($key && $key != $book_link['nid']);
    if ($key == $book_link['nid']) {
        // The previous page in the book may be a child of the previous visible link.
        if ($prev['depth'] == $book_link['depth']) {
            // The subtree will have only one link at the top level - get its data.
            $tree = $this->bookManager
                ->bookSubtreeData($prev);
            $data = array_shift($tree);
            // The link of interest is the last child - iterate to find the deepest one.
            while ($data['below']) {
                $data = end($data['below']);
            }
            $this->bookManager
                ->bookLinkTranslate($data['link']);
            return $data['link'];
        }
        else {
            $this->bookManager
                ->bookLinkTranslate($prev);
            return $prev;
        }
    }
}

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