BookManager::buildItems

protected BookManager::buildItems(array $tree)

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.

File

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

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'];
    $node = $this->entityManager->getStorage('node')->load($data['link']['nid']);
    $element['url'] = $node->urlInfo();
    $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;
}
doc_Drupal
2016-10-29 08:48:09
Comments
Leave a Comment

Please login to continue.