function template_preprocess_book_navigation

Same name in other branches
  1. 7.x modules/book/book.module \template_preprocess_book_navigation()
  2. 9 core/modules/book/book.module \template_preprocess_book_navigation()
  3. 8.9.x core/modules/book/book.module \template_preprocess_book_navigation()
  4. 11.x 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 389

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'] = Url::fromRoute('entity.node.canonical', [
        'node' => $book_link['bid'],
    ])->toString();
    $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 = [];
        if ($prev = $book_outline->prevLink($book_link)) {
            $prev_href = Url::fromRoute('entity.node.canonical', [
                'node' => $prev['nid'],
            ])->toString();
            $build['#attached']['html_head_link'][][] = [
                '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 = Url::fromRoute('entity.node.canonical', [
                'node' => $book_link['pid'],
            ])->toString();
            $build['#attached']['html_head_link'][][] = [
                'rel' => 'up',
                'href' => $parent_href,
            ];
            $variables['parent_url'] = $parent_href;
            $variables['parent_title'] = $parent['title'];
        }
        if ($next = $book_outline->nextLink($book_link)) {
            $next_href = Url::fromRoute('entity.node.canonical', [
                'node' => $next['nid'],
            ])->toString();
            $build['#attached']['html_head_link'][][] = [
                'rel' => 'next',
                'href' => $next_href,
            ];
            $variables['next_url'] = $next_href;
            $variables['next_title'] = $next['title'];
        }
    }
    if (!empty($build)) {
        \Drupal::service('renderer')->render($build);
    }
    $variables['has_links'] = FALSE;
    // Link variables to filter for values and set state of the flag variable.
    $links = [
        '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] = '';
        }
    }
}

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