BookOutline.php 3.5 KB

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