Same name and namespace in other branches
  1. 8.9.x core/modules/book/src/BookManager.php \Drupal\book\BookManager::recurseTableOfContents()
  2. 9 core/modules/book/src/BookManager.php \Drupal\book\BookManager::recurseTableOfContents()

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).

1 call to BookManager::recurseTableOfContents()
BookManager::getTableOfContents in core/modules/book/src/BookManager.php

File

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

Class

BookManager
Defines a book manager.

Namespace

Drupal\book

Code

protected function recurseTableOfContents(array $tree, $indent, array &$toc, array $exclude, $depth_limit) {
  $nids = [];
  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'];
    }
  }

  // Load nodes with proper translation.
  $nodes = $this->entityTypeManager
    ->getStorage('node')
    ->loadMultiple($nids);
  $nodes = array_map([
    $this->entityRepository,
    'getTranslationFromContext',
  ], $nodes);
  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);
    }
  }
}