annotate core/modules/book/src/BookOutline.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 1fec387a4317
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\book;
Chris@0 4
Chris@0 5 /**
Chris@0 6 * Provides handling to render the book outline.
Chris@0 7 */
Chris@0 8 class BookOutline {
Chris@0 9
Chris@0 10 /**
Chris@0 11 * The book manager.
Chris@0 12 *
Chris@0 13 * @var \Drupal\book\BookManagerInterface
Chris@0 14 */
Chris@0 15 protected $bookManager;
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Constructs a new BookOutline.
Chris@0 19 *
Chris@0 20 * @param \Drupal\book\BookManagerInterface $book_manager
Chris@0 21 * The book manager.
Chris@0 22 */
Chris@0 23 public function __construct(BookManagerInterface $book_manager) {
Chris@0 24 $this->bookManager = $book_manager;
Chris@0 25 }
Chris@0 26
Chris@0 27 /**
Chris@0 28 * Fetches the book link for the previous page of the book.
Chris@0 29 *
Chris@0 30 * @param array $book_link
Chris@0 31 * A fully loaded book link that is part of the book hierarchy.
Chris@0 32 *
Chris@0 33 * @return array
Chris@0 34 * A fully loaded book link for the page before the one represented in
Chris@0 35 * $book_link.
Chris@0 36 */
Chris@0 37 public function prevLink(array $book_link) {
Chris@0 38 // If the parent is zero, we are at the start of a book.
Chris@0 39 if ($book_link['pid'] == 0) {
Chris@0 40 return NULL;
Chris@0 41 }
Chris@0 42 $flat = $this->bookManager->bookTreeGetFlat($book_link);
Chris@14 43 reset($flat);
Chris@0 44 $curr = NULL;
Chris@0 45 do {
Chris@0 46 $prev = $curr;
Chris@14 47 $key = key($flat);
Chris@14 48 $curr = current($flat);
Chris@14 49 next($flat);
Chris@0 50 } while ($key && $key != $book_link['nid']);
Chris@0 51
Chris@0 52 if ($key == $book_link['nid']) {
Chris@0 53 // The previous page in the book may be a child of the previous visible link.
Chris@0 54 if ($prev['depth'] == $book_link['depth']) {
Chris@0 55 // The subtree will have only one link at the top level - get its data.
Chris@0 56 $tree = $this->bookManager->bookSubtreeData($prev);
Chris@0 57 $data = array_shift($tree);
Chris@0 58 // The link of interest is the last child - iterate to find the deepest one.
Chris@0 59 while ($data['below']) {
Chris@0 60 $data = end($data['below']);
Chris@0 61 }
Chris@0 62 $this->bookManager->bookLinkTranslate($data['link']);
Chris@0 63 return $data['link'];
Chris@0 64 }
Chris@0 65 else {
Chris@0 66 $this->bookManager->bookLinkTranslate($prev);
Chris@0 67 return $prev;
Chris@0 68 }
Chris@0 69 }
Chris@0 70 }
Chris@0 71
Chris@0 72 /**
Chris@0 73 * Fetches the book link for the next page of the book.
Chris@0 74 *
Chris@0 75 * @param array $book_link
Chris@0 76 * A fully loaded book link that is part of the book hierarchy.
Chris@0 77 *
Chris@0 78 * @return array
Chris@0 79 * A fully loaded book link for the page after the one represented in
Chris@0 80 * $book_link.
Chris@0 81 */
Chris@0 82 public function nextLink(array $book_link) {
Chris@0 83 $flat = $this->bookManager->bookTreeGetFlat($book_link);
Chris@14 84 reset($flat);
Chris@0 85 do {
Chris@14 86 $key = key($flat);
Chris@14 87 next($flat);
Chris@0 88 } while ($key && $key != $book_link['nid']);
Chris@0 89
Chris@0 90 if ($key == $book_link['nid']) {
Chris@0 91 $next = current($flat);
Chris@0 92 if ($next) {
Chris@0 93 $this->bookManager->bookLinkTranslate($next);
Chris@0 94 }
Chris@0 95 return $next;
Chris@0 96 }
Chris@0 97 }
Chris@0 98
Chris@0 99 /**
Chris@0 100 * Formats the book links for the child pages of the current page.
Chris@0 101 *
Chris@0 102 * @param array $book_link
Chris@0 103 * A fully loaded book link that is part of the book hierarchy.
Chris@0 104 *
Chris@0 105 * @return array
Chris@0 106 * HTML for the links to the child pages of the current page.
Chris@0 107 */
Chris@0 108 public function childrenLinks(array $book_link) {
Chris@0 109 $flat = $this->bookManager->bookTreeGetFlat($book_link);
Chris@0 110
Chris@0 111 $children = [];
Chris@0 112
Chris@0 113 if ($book_link['has_children']) {
Chris@0 114 // Walk through the array until we find the current page.
Chris@0 115 do {
Chris@0 116 $link = array_shift($flat);
Chris@0 117 } while ($link && ($link['nid'] != $book_link['nid']));
Chris@0 118 // Continue though the array and collect the links whose parent is this page.
Chris@0 119 while (($link = array_shift($flat)) && $link['pid'] == $book_link['nid']) {
Chris@0 120 $data['link'] = $link;
Chris@0 121 $data['below'] = '';
Chris@0 122 $children[] = $data;
Chris@0 123 }
Chris@0 124 }
Chris@0 125
Chris@0 126 if ($children) {
Chris@0 127 return $this->bookManager->bookTreeOutput($children);
Chris@0 128 }
Chris@0 129 return '';
Chris@0 130 }
Chris@0 131
Chris@0 132 }