BookManager::recurseTableOfContents

protected BookManager::recurseTableOfContents(array $tree, $indent, array &$toc, array $exclude, $depth_limit)

Recursively processes and formats book links for getTableOfContents().

This helper function recursively modifies the table of contents array for each item in the book tree, ignoring items in the exclude array or at a depth greater than the limit. Truncates titles over thirty characters and appends an indentation string incremented by depth.

Parameters

array $tree: The data structure of the book's outline tree. Includes hidden links.

string $indent: A string appended to each node title. Increments by '--' per depth level.

array $toc: Reference to the table of contents array. This is modified in place, so the function does not have a return value.

array $exclude: Optional array of Node ID values. Any link whose node ID is in this array will be excluded (along with its children).

int $depth_limit: Any link deeper than this value will be excluded (along with its children).

File

core/modules/book/src/BookManager.php, line 390

Class

BookManager
Defines a book manager.

Namespace

Drupal\book

Code

protected function recurseTableOfContents(array $tree, $indent, array &$toc, array $exclude, $depth_limit) {
  $nids = array();
  foreach ($tree as $data) {
    if ($data['link']['depth'] > $depth_limit) {
      // Don't iterate through any links on this level.
      return;
    }
    if (!in_array($data['link']['nid'], $exclude)) {
      $nids[] = $data['link']['nid'];
    }
  }

  $nodes = $this->entityManager->getStorage('node')->loadMultiple($nids);

  foreach ($tree as $data) {
    $nid = $data['link']['nid'];
    // Check for excluded or missing node.
    if (empty($nodes[$nid])) {
      continue;
    }
    $toc[$nid] = $indent . ' ' . Unicode::truncate($nodes[$nid]->label(), 30, TRUE, TRUE);
    if ($data['below']) {
      $this->recurseTableOfContents($data['below'], $indent . '--', $toc, $exclude, $depth_limit);
    }
  }
}
doc_Drupal
2016-10-29 08:48:12
Comments
Leave a Comment

Please login to continue.