book_blocks-8.x-1.4/book_blocks.module
book_blocks.module
<?php
/**
* @file
* Allows users to create and organize related content in an outline.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
/**
* Implements hook_help().
*/
function book_blocks_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.book_blocks':
$output = '';
$output .= '<h2>' . t('Module description') . '</h2>';
$output .= '<p>' . t('The built-in Book module has a couple of useful
blocks including a list of children (Children), navigation,
Table of Contents (includes a link to the top page),
and Edit (combination of navigation, TOC and edit links).') . '</p>';
return $output;
break;
}
}
/**
* Implements hook_theme().
*/
function book_blocks_theme() {
return [
'book_blocks_navigation' => [
'variables' => ['book_link' => NULL],
],
'book_blocks_edit' => [
'variables' => [
'book_link' => NULL,
'css' => '',
'toc' => FALSE,
'nav' => FALSE,
'icons' => FALSE,
'left_link' => NULL,
'middle_link' => NULL,
'right_link' => NULL,
'sibling_link' => NULL,
'child_link' => NULL,
'print_link' => NULL,
'my_element_id' => NULL,
'module_path' => base_path() . \Drupal::service('module_handler')->getModule('book_blocks')->getPath(),
],
],
'book_blocks_children' => [
'variables' => ['book_link' => NULL],
],
];
}
/**
* Get URL String from nid.
*
* @param int $nid
*
* @return string
*/
function _book_block_url($nid) {
return Url::fromRoute('entity.node.canonical', ['node' => $nid])->toString();
}
/**
* Prepares variables for book_blocks templates.
*
* @param 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.
*/
function _template_preprocess_book_blocks(&$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['title'];
$variables['book_url'] = _book_block_url($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']) {
$build = [];
if ($prev = $book_outline->prevLink($book_link)) {
$prev_href = _book_block_url($prev['nid']);
$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 = _book_block_url($book_link['pid']);
$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 = _book_block_url($next['nid']);
$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] = '';
}
}
}
/**
* Prepares variables for book navigation templates.
*
* Default template: book-blocks-navigation.html.twig.
*
* @param 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.
*/
function template_preprocess_book_blocks_navigation(&$variables) {
_template_preprocess_book_blocks($variables);
}
/**
* Prepares variables for book navigation templates.
*
* Default template: book-blocks-edit.html.twig.
*
* @param 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.
*/
function template_preprocess_book_blocks_edit(&$variables) {
_template_preprocess_book_blocks($variables);
}
/**
* Prepares variables for book navigation templates.
*
* Default template: book-blocks-children.html.twig.
*
* @param 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.
*/
function template_preprocess_book_blocks_children(&$variables) {
_template_preprocess_book_blocks($variables);
$book_outline = \Drupal::service('book.outline');
$variables['tree'] = $book_outline->childrenLinks($variables['book_link']);
}
