You are here

public function BookOutline::prevLink in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 42
Contains \Drupal\book\BookOutline.

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

  // Assigning the array to $flat resets the array pointer for use with each().
  $flat = $this->bookManager
    ->bookTreeGetFlat($book_link);
  $curr = NULL;
  do {
    $prev = $curr;
    list($key, $curr) = each($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;
    }
  }
}