You are here

protected function BookExport::exportTraverse in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/book/src/BookExport.php \Drupal\book\BookExport::exportTraverse()
  2. 9 core/modules/book/src/BookExport.php \Drupal\book\BookExport::exportTraverse()

Traverses the book tree to build printable or exportable output.

During the traversal, the callback is applied to each node and is called recursively for each child of the node (in weight, title order).

Parameters

array $tree: A subtree of the book menu hierarchy, rooted at the current page.

callable $callable: A callback to be called upon visiting a node in the tree.

Return value

array The render array generated in visiting each node.

File

core/modules/book/src/BookExport.php, line 114

Class

BookExport
Provides methods for exporting book to different formats.

Namespace

Drupal\book

Code

protected function exportTraverse(array $tree, $callable) {

  // If there is no valid callable, use the default callback.
  $callable = !empty($callable) ? $callable : [
    $this,
    'bookNodeExport',
  ];
  $build = [];
  foreach ($tree as $data) {

    // Access checking is already performed when building the tree.
    if ($node = $this->nodeStorage
      ->load($data['link']['nid'])) {
      $node = $this->entityRepository
        ->getTranslationFromContext($node);
      $children = $data['below'] ? $this
        ->exportTraverse($data['below'], $callable) : '';
      $build[] = call_user_func($callable, $node, $children);
    }
  }
  return $build;
}