function template_preprocess_book_navigation in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/book/book.module \template_preprocess_book_navigation()
Prepares variables for book navigation templates.
Default template: book-navigation.html.twig.
Parameters
array $variables: An associative array containing the following key:
- book_link: An associative array of book link properties. Properties used: bid, link_title, depth, pid, nid.
File
- core/
modules/ book/ book.module, line 380 - Allows users to create and organize related content in an outline.
Code
function template_preprocess_book_navigation(&$variables) {
$book_link = $variables['book_link'];
// Provide extra variables for themers. Not needed by default.
$variables['book_id'] = $book_link['bid'];
$variables['book_title'] = $book_link['link_title'];
$variables['book_url'] = \Drupal::url('entity.node.canonical', array(
'node' => $book_link['bid'],
));
$variables['current_depth'] = $book_link['depth'];
$variables['tree'] = '';
/** @var \Drupal\book\BookOutline $book_outline */
$book_outline = \Drupal::service('book.outline');
if ($book_link['nid']) {
$variables['tree'] = $book_outline
->childrenLinks($book_link);
$build = array();
if ($prev = $book_outline
->prevLink($book_link)) {
$prev_href = \Drupal::url('entity.node.canonical', array(
'node' => $prev['nid'],
));
$build['#attached']['html_head_link'][][] = array(
'rel' => 'prev',
'href' => $prev_href,
);
$variables['prev_url'] = $prev_href;
$variables['prev_title'] = $prev['title'];
}
/** @var \Drupal\book\BookManagerInterface $book_manager */
$book_manager = \Drupal::service('book.manager');
if ($book_link['pid'] && ($parent = $book_manager
->loadBookLink($book_link['pid']))) {
$parent_href = \Drupal::url('entity.node.canonical', array(
'node' => $book_link['pid'],
));
$build['#attached']['html_head_link'][][] = array(
'rel' => 'up',
'href' => $parent_href,
);
$variables['parent_url'] = $parent_href;
$variables['parent_title'] = $parent['title'];
}
if ($next = $book_outline
->nextLink($book_link)) {
$next_href = \Drupal::url('entity.node.canonical', array(
'node' => $next['nid'],
));
$build['#attached']['html_head_link'][][] = array(
'rel' => 'next',
'href' => $next_href,
);
$variables['next_url'] = $next_href;
$variables['next_title'] = $next['title'];
}
}
if (!empty($build)) {
drupal_render($build);
}
$variables['has_links'] = FALSE;
// Link variables to filter for values and set state of the flag variable.
$links = array(
'prev_url',
'prev_title',
'parent_url',
'parent_title',
'next_url',
'next_title',
);
foreach ($links as $link) {
if (isset($variables[$link])) {
// Flag when there is a value.
$variables['has_links'] = TRUE;
}
else {
// Set empty to prevent notices.
$variables[$link] = '';
}
}
}