You are here

function book_prev in Drupal 7

Same name and namespace in other branches
  1. 4 modules/book.module \book_prev()
  2. 5 modules/book/book.module \book_prev()
  3. 6 modules/book/book.module \book_prev()

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

Parameters

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

Return value

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

1 call to book_prev()
template_preprocess_book_navigation in modules/book/book.module
Processes variables for book-navigation.tpl.php.

File

modules/book/book.module, line 765
Allows users to create and organize related content in an outline.

Code

function book_prev($book_link) {

  // If the parent is zero, we are at the start of a book.
  if ($book_link['plid'] == 0) {
    return NULL;
  }
  $flat = book_get_flat_menu($book_link);
  reset($flat);
  $curr = NULL;
  do {
    $prev = $curr;
    $curr = current($flat);
    $key = key($flat);
    next($flat);
  } while ($key && $key != $book_link['mlid']);
  if ($key == $book_link['mlid']) {

    // The previous page in the book may be a child of the previous visible link.
    if ($prev['depth'] == $book_link['depth'] && $prev['has_children']) {

      // The subtree will have only one link at the top level - get its data.
      $tree = book_menu_subtree_data($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']);
      }
      return $data['link'];
    }
    else {
      return $prev;
    }
  }
}