function BookManager::buildItems

Same name in other branches
  1. 9 core/modules/book/src/BookManager.php \Drupal\book\BookManager::buildItems()
  2. 8.9.x core/modules/book/src/BookManager.php \Drupal\book\BookManager::buildItems()
  3. 11.x core/modules/book/src/BookManager.php \Drupal\book\BookManager::buildItems()

Builds the #items property for a book tree's renderable array.

Helper function for ::bookTreeOutput().

Parameters

array $tree: A data structure representing the tree.

Return value

array The value to use for the #items property of a renderable menu.

1 call to BookManager::buildItems()
BookManager::bookTreeOutput in core/modules/book/src/BookManager.php

File

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

Class

BookManager
Defines a book manager.

Namespace

Drupal\book

Code

protected function buildItems(array $tree) {
    $items = [];
    foreach ($tree as $data) {
        $element = [];
        // Generally we only deal with visible links, but just in case.
        if (!$data['link']['access']) {
            continue;
        }
        // Set a class for the <li> tag. Since $data['below'] may contain local
        // tasks, only set 'expanded' to true if the link also has children within
        // the current book.
        $element['is_expanded'] = FALSE;
        $element['is_collapsed'] = FALSE;
        if ($data['link']['has_children'] && $data['below']) {
            $element['is_expanded'] = TRUE;
        }
        elseif ($data['link']['has_children']) {
            $element['is_collapsed'] = TRUE;
        }
        // Set a helper variable to indicate whether the link is in the active
        // trail.
        $element['in_active_trail'] = FALSE;
        if ($data['link']['in_active_trail']) {
            $element['in_active_trail'] = TRUE;
        }
        // Allow book-specific theme overrides.
        $element['attributes'] = new Attribute();
        $element['title'] = $data['link']['title'];
        $element['url'] = Url::fromUri('entity:node/' . $data['link']['nid'], [
            'language' => $this->languageManager
                ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT),
        ]);
        $element['localized_options'] = !empty($data['link']['localized_options']) ? $data['link']['localized_options'] : [];
        $element['localized_options']['set_active_class'] = TRUE;
        $element['below'] = $data['below'] ? $this->buildItems($data['below']) : [];
        $element['original_link'] = $data['link'];
        // Index using the link's unique nid.
        $items[$data['link']['nid']] = $element;
    }
    return $items;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.