annotate core/modules/book/book.module @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * Allows users to create and organize related content in an outline.
Chris@0 6 */
Chris@0 7
Chris@0 8 use Drupal\book\BookManager;
Chris@0 9 use Drupal\Core\Entity\EntityInterface;
Chris@0 10 use Drupal\Core\Form\FormStateInterface;
Chris@0 11 use Drupal\Core\Render\Element;
Chris@0 12 use Drupal\Core\Routing\RouteMatchInterface;
Chris@0 13 use Drupal\Core\Url;
Chris@0 14 use Drupal\node\NodeInterface;
Chris@0 15 use Drupal\node\NodeTypeInterface;
Chris@0 16 use Drupal\node\Entity\Node;
Chris@0 17 use Drupal\Core\Language\LanguageInterface;
Chris@0 18 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
Chris@0 19 use Drupal\Core\Template\Attribute;
Chris@0 20
Chris@0 21 /**
Chris@0 22 * Implements hook_help().
Chris@0 23 */
Chris@0 24 function book_help($route_name, RouteMatchInterface $route_match) {
Chris@0 25 switch ($route_name) {
Chris@0 26 case 'help.page.book':
Chris@0 27 $output = '<h3>' . t('About') . '</h3>';
Chris@0 28 $output .= '<p>' . t('The Book module is used for creating structured, multi-page content, such as site resource guides, manuals, and wikis. It allows you to create content that has chapters, sections, subsections, or any similarly-tiered structure. Enabling the module creates a new content type <em>Book page</em>. For more information, see the <a href=":book">online documentation for the Book module</a>.', [':book' => 'https://www.drupal.org/documentation/modules/book']) . '</p>';
Chris@0 29 $output .= '<h3>' . t('Uses') . '</h3>';
Chris@0 30 $output .= '<dl>';
Chris@0 31 $output .= '<dt>' . t('Adding and managing book content') . '</dt>';
Chris@0 32 $output .= '<dd>' . t('Books have a hierarchical structure, called a <em>book outline</em>. Each book outline can have nested pages up to nine levels deep. Multiple content types can be configured to behave as a book outline. From the content edit form, it is possible to add a page to a book outline or create a new book.') . '</dd>';
Chris@18 33 $output .= '<dd>' . t('You can assign separate permissions for <em>creating new books</em> as well as <em>creating</em>, <em>editing</em> and <em>deleting</em> book content. Users with the <em>Administer book outlines</em> permission can add <em>any</em> type of content to a book by selecting the appropriate book outline while editing the content. They can also view a list of all books, and edit and rearrange section titles on the <a href=":admin-book">Book list page</a>.', [':admin-book' => Url::fromRoute('book.admin')->toString()]) . '</dd>';
Chris@0 34 $output .= '<dt>' . t('Configuring content types for books') . '</dt>';
Chris@18 35 $output .= '<dd>' . t('The <em>Book page</em> content type is the initial content type enabled for book outlines. On the <a href=":admin-settings">Book settings page</a> you can configure content types that can used in book outlines.', [':admin-settings' => Url::fromRoute('book.settings')->toString()]) . '</dd>';
Chris@18 36 $output .= '<dd>' . t('Users with the <em>Add content and child pages to books</em> permission will see a link to <em>Add child page</em> when viewing a content item that is part of a book outline. This link will allow users to create a new content item of the content type you select on the <a href=":admin-settings">Book settings page</a>. By default this is the <em>Book page</em> content type.', [':admin-settings' => Url::fromRoute('book.settings')->toString()]) . '</dd>';
Chris@0 37 $output .= '<dt>' . t('Book navigation') . '</dt>';
Chris@18 38 $output .= '<dd>' . t("Book pages have a default book-specific navigation block. This navigation block contains links that lead to the previous and next pages in the book, and to the level above the current page in the book's structure. This block can be enabled on the <a href=':admin-block'>Blocks layout page</a>. For book pages to show up in the book navigation, they must be added to a book outline.", [':admin-block' => (\Drupal::moduleHandler()->moduleExists('block')) ? Url::fromRoute('block.admin_display')->toString() : '#']) . '</dd>';
Chris@0 39 $output .= '<dt>' . t('Collaboration') . '</dt>';
Chris@0 40 $output .= '<dd>' . t('Books can be created collaboratively, as they allow users with appropriate permissions to add pages into existing books, and add those pages to a custom table of contents.') . '</dd>';
Chris@0 41 $output .= '<dt>' . t('Printing books') . '</dt>';
Chris@0 42 $output .= '<dd>' . t("Users with the <em>View printer-friendly books</em> permission can select the <em>printer-friendly version</em> link visible at the bottom of a book page's content to generate a printer-friendly display of the page and all of its subsections.") . '</dd>';
Chris@0 43 $output .= '</dl>';
Chris@0 44 return $output;
Chris@0 45
Chris@0 46 case 'book.admin':
Chris@0 47 return '<p>' . t('The book module offers a means to organize a collection of related content pages, collectively known as a book. When viewed, this content automatically displays links to adjacent book pages, providing a simple navigation system for creating and reviewing structured content.') . '</p>';
Chris@0 48
Chris@0 49 case 'entity.node.book_outline_form':
Chris@18 50 return '<p>' . t('The outline feature allows you to include pages in the <a href=":book">Book hierarchy</a>, as well as move them within the hierarchy or to <a href=":book-admin">reorder an entire book</a>.', [':book' => Url::fromRoute('book.render')->toString(), ':book-admin' => Url::fromRoute('book.admin')->toString()]) . '</p>';
Chris@0 51 }
Chris@0 52 }
Chris@0 53
Chris@0 54 /**
Chris@0 55 * Implements hook_theme().
Chris@0 56 */
Chris@0 57 function book_theme() {
Chris@0 58 return [
Chris@0 59 'book_navigation' => [
Chris@0 60 'variables' => ['book_link' => NULL],
Chris@0 61 ],
Chris@0 62 'book_tree' => [
Chris@0 63 'variables' => ['items' => [], 'attributes' => []],
Chris@0 64 ],
Chris@0 65 'book_export_html' => [
Chris@0 66 'variables' => ['title' => NULL, 'contents' => NULL, 'depth' => NULL],
Chris@0 67 ],
Chris@0 68 'book_all_books_block' => [
Chris@0 69 'render element' => 'book_menus',
Chris@0 70 ],
Chris@0 71 'book_node_export_html' => [
Chris@0 72 'variables' => ['node' => NULL, 'content' => NULL, 'children' => NULL],
Chris@0 73 ],
Chris@0 74 ];
Chris@0 75 }
Chris@0 76
Chris@0 77 /**
Chris@0 78 * Implements hook_entity_type_build().
Chris@0 79 */
Chris@0 80 function book_entity_type_build(array &$entity_types) {
Chris@0 81 /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
Chris@0 82 $entity_types['node']
Chris@0 83 ->setFormClass('book_outline', 'Drupal\book\Form\BookOutlineForm')
Chris@0 84 ->setLinkTemplate('book-outline-form', '/node/{node}/outline')
Chris@0 85 ->setLinkTemplate('book-remove-form', '/node/{node}/outline/remove')
Chris@0 86 ->addConstraint('BookOutline', []);
Chris@0 87 }
Chris@0 88
Chris@0 89 /**
Chris@0 90 * Implements hook_node_links_alter().
Chris@0 91 */
Chris@0 92 function book_node_links_alter(array &$links, NodeInterface $node, array &$context) {
Chris@0 93 if ($context['view_mode'] != 'rss') {
Chris@0 94 $account = \Drupal::currentUser();
Chris@0 95
Chris@0 96 if (isset($node->book['depth'])) {
Chris@0 97 if ($context['view_mode'] == 'full' && node_is_page($node)) {
Chris@0 98 $child_type = \Drupal::config('book.settings')->get('child_type');
Chris@0 99 $access_control_handler = \Drupal::entityManager()->getAccessControlHandler('node');
Chris@0 100 if (($account->hasPermission('add content to books') || $account->hasPermission('administer book outlines')) && $access_control_handler->createAccess($child_type) && $node->isPublished() && $node->book['depth'] < BookManager::BOOK_MAX_DEPTH) {
Chris@0 101 $book_links['book_add_child'] = [
Chris@0 102 'title' => t('Add child page'),
Chris@0 103 'url' => Url::fromRoute('node.add', ['node_type' => $child_type], ['query' => ['parent' => $node->id()]]),
Chris@0 104 ];
Chris@0 105 }
Chris@0 106
Chris@0 107 if ($account->hasPermission('access printer-friendly version')) {
Chris@0 108 $book_links['book_printer'] = [
Chris@0 109 'title' => t('Printer-friendly version'),
Chris@0 110 'url' => Url::fromRoute('book.export', [
Chris@0 111 'type' => 'html',
Chris@0 112 'node' => $node->id(),
Chris@0 113 ]),
Chris@17 114 'attributes' => ['title' => t('Show a printer-friendly version of this book page and its sub-pages.')],
Chris@0 115 ];
Chris@0 116 }
Chris@0 117 }
Chris@0 118 }
Chris@0 119
Chris@0 120 if (!empty($book_links)) {
Chris@0 121 $links['book'] = [
Chris@0 122 '#theme' => 'links__node__book',
Chris@0 123 '#links' => $book_links,
Chris@0 124 '#attributes' => ['class' => ['links', 'inline']],
Chris@0 125 ];
Chris@0 126 }
Chris@0 127 }
Chris@0 128 }
Chris@0 129
Chris@0 130 /**
Chris@0 131 * Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.
Chris@0 132 *
Chris@0 133 * Adds the book form element to the node form.
Chris@0 134 *
Chris@0 135 * @see book_pick_book_nojs_submit()
Chris@0 136 */
Chris@0 137 function book_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
Chris@0 138 $node = $form_state->getFormObject()->getEntity();
Chris@0 139 $account = \Drupal::currentUser();
Chris@0 140 $access = $account->hasPermission('administer book outlines');
Chris@0 141 if (!$access) {
Chris@0 142 if ($account->hasPermission('add content to books') && ((!empty($node->book['bid']) && !$node->isNew()) || book_type_is_allowed($node->getType()))) {
Chris@0 143 // Already in the book hierarchy, or this node type is allowed.
Chris@0 144 $access = TRUE;
Chris@0 145 }
Chris@0 146 }
Chris@0 147
Chris@0 148 if ($access) {
Chris@0 149 $collapsed = !($node->isNew() && !empty($node->book['pid']));
Chris@0 150 $form = \Drupal::service('book.manager')->addFormElements($form, $form_state, $node, $account, $collapsed);
Chris@0 151 // The "js-hide" class hides submit button when Javascript is enabled.
Chris@0 152 $form['book']['pick-book'] = [
Chris@0 153 '#type' => 'submit',
Chris@0 154 '#value' => t('Change book (update list of parents)'),
Chris@0 155 '#submit' => ['book_pick_book_nojs_submit'],
Chris@0 156 '#weight' => 20,
Chris@0 157 '#attributes' => [
Chris@0 158 'class' => [
Chris@0 159 'js-hide',
Chris@0 160 ],
Chris@0 161 ],
Chris@0 162 ];
Chris@0 163 $form['#entity_builders'][] = 'book_node_builder';
Chris@0 164 }
Chris@0 165 }
Chris@0 166
Chris@0 167 /**
Chris@0 168 * Entity form builder to add the book information to the node.
Chris@0 169 *
Chris@0 170 * @todo: Remove this in favor of an entity field.
Chris@0 171 */
Chris@0 172 function book_node_builder($entity_type, NodeInterface $entity, &$form, FormStateInterface $form_state) {
Chris@0 173 $entity->book = $form_state->getValue('book');
Chris@0 174
Chris@0 175 // Always save a revision for non-administrators.
Chris@0 176 if (!empty($entity->book['bid']) && !\Drupal::currentUser()->hasPermission('administer nodes')) {
Chris@0 177 $entity->setNewRevision();
Chris@0 178 }
Chris@0 179 }
Chris@0 180
Chris@0 181 /**
Chris@0 182 * Form submission handler for node_form().
Chris@0 183 *
Chris@0 184 * This handler is run when JavaScript is disabled. It triggers the form to
Chris@0 185 * rebuild so that the "Parent item" options are changed to reflect the newly
Chris@0 186 * selected book. When JavaScript is enabled, the submit button that triggers
Chris@0 187 * this handler is hidden, and the "Book" dropdown directly triggers the
Chris@0 188 * book_form_update() Ajax callback instead.
Chris@0 189 *
Chris@0 190 * @see book_form_update()
Chris@0 191 * @see book_form_node_form_alter()
Chris@0 192 */
Chris@0 193 function book_pick_book_nojs_submit($form, FormStateInterface $form_state) {
Chris@0 194 $node = $form_state->getFormObject()->getEntity();
Chris@0 195 $node->book = $form_state->getValue('book');
Chris@0 196 $form_state->setRebuild();
Chris@0 197 }
Chris@0 198
Chris@0 199 /**
Chris@0 200 * Renders a new parent page select element when the book selection changes.
Chris@0 201 *
Chris@0 202 * This function is called via Ajax when the selected book is changed on a node
Chris@0 203 * or book outline form.
Chris@0 204 *
Chris@0 205 * @return
Chris@0 206 * The rendered parent page select element.
Chris@0 207 */
Chris@0 208 function book_form_update($form, FormStateInterface $form_state) {
Chris@0 209 return $form['book']['pid'];
Chris@0 210 }
Chris@0 211
Chris@0 212 /**
Chris@0 213 * Implements hook_ENTITY_TYPE_load() for node entities.
Chris@0 214 */
Chris@0 215 function book_node_load($nodes) {
Chris@0 216 /** @var \Drupal\book\BookManagerInterface $book_manager */
Chris@0 217 $book_manager = \Drupal::service('book.manager');
Chris@0 218 $links = $book_manager->loadBookLinks(array_keys($nodes), FALSE);
Chris@0 219 foreach ($links as $record) {
Chris@0 220 $nodes[$record['nid']]->book = $record;
Chris@0 221 $nodes[$record['nid']]->book['link_path'] = 'node/' . $record['nid'];
Chris@0 222 $nodes[$record['nid']]->book['link_title'] = $nodes[$record['nid']]->label();
Chris@0 223 }
Chris@0 224 }
Chris@0 225
Chris@0 226 /**
Chris@0 227 * Implements hook_ENTITY_TYPE_view() for node entities.
Chris@0 228 */
Chris@0 229 function book_node_view(array &$build, EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) {
Chris@0 230 if ($view_mode == 'full') {
Chris@0 231 if (!empty($node->book['bid']) && empty($node->in_preview)) {
Chris@0 232 $book_node = Node::load($node->book['bid']);
Chris@0 233 if (!$book_node->access()) {
Chris@0 234 return;
Chris@0 235 }
Chris@0 236 $build['book_navigation'] = [
Chris@0 237 '#theme' => 'book_navigation',
Chris@0 238 '#book_link' => $node->book,
Chris@0 239 '#weight' => 100,
Chris@0 240 // The book navigation is a listing of Node entities, so associate its
Chris@0 241 // list cache tag for correct invalidation.
Chris@0 242 '#cache' => [
Chris@0 243 'tags' => $node->getEntityType()->getListCacheTags(),
Chris@0 244 ],
Chris@0 245 ];
Chris@0 246 }
Chris@0 247 }
Chris@0 248 }
Chris@0 249
Chris@0 250 /**
Chris@0 251 * Implements hook_ENTITY_TYPE_presave() for node entities.
Chris@0 252 */
Chris@0 253 function book_node_presave(EntityInterface $node) {
Chris@0 254 // Make sure a new node gets a new menu link.
Chris@0 255 if ($node->isNew()) {
Chris@0 256 $node->book['nid'] = NULL;
Chris@0 257 }
Chris@0 258 }
Chris@0 259
Chris@0 260 /**
Chris@0 261 * Implements hook_ENTITY_TYPE_insert() for node entities.
Chris@0 262 */
Chris@0 263 function book_node_insert(EntityInterface $node) {
Chris@0 264 /** @var \Drupal\book\BookManagerInterface $book_manager */
Chris@0 265 $book_manager = \Drupal::service('book.manager');
Chris@0 266 $book_manager->updateOutline($node);
Chris@0 267 }
Chris@0 268
Chris@0 269 /**
Chris@0 270 * Implements hook_ENTITY_TYPE_update() for node entities.
Chris@0 271 */
Chris@0 272 function book_node_update(EntityInterface $node) {
Chris@0 273 /** @var \Drupal\book\BookManagerInterface $book_manager */
Chris@0 274 $book_manager = \Drupal::service('book.manager');
Chris@0 275 $book_manager->updateOutline($node);
Chris@0 276 }
Chris@0 277
Chris@0 278 /**
Chris@0 279 * Implements hook_ENTITY_TYPE_predelete() for node entities.
Chris@0 280 */
Chris@0 281 function book_node_predelete(EntityInterface $node) {
Chris@0 282 if (!empty($node->book['bid'])) {
Chris@0 283 /** @var \Drupal\book\BookManagerInterface $book_manager */
Chris@0 284 $book_manager = \Drupal::service('book.manager');
Chris@0 285 $book_manager->deleteFromBook($node->book['nid']);
Chris@0 286 }
Chris@0 287 }
Chris@0 288
Chris@0 289 /**
Chris@0 290 * Implements hook_ENTITY_TYPE_prepare_form() for node entities.
Chris@0 291 */
Chris@0 292 function book_node_prepare_form(NodeInterface $node, $operation, FormStateInterface $form_state) {
Chris@0 293 /** @var \Drupal\book\BookManagerInterface $book_manager */
Chris@0 294 $book_manager = \Drupal::service('book.manager');
Chris@0 295
Chris@0 296 // Prepare defaults for the add/edit form.
Chris@0 297 $account = \Drupal::currentUser();
Chris@0 298 if (empty($node->book) && ($account->hasPermission('add content to books') || $account->hasPermission('administer book outlines'))) {
Chris@0 299 $node->book = [];
Chris@0 300
Chris@0 301 $query = \Drupal::request()->query;
Chris@0 302 if ($node->isNew() && !is_null($query->get('parent')) && is_numeric($query->get('parent'))) {
Chris@0 303 // Handle "Add child page" links:
Chris@0 304 $parent = $book_manager->loadBookLink($query->get('parent'), TRUE);
Chris@0 305
Chris@0 306 if ($parent && $parent['access']) {
Chris@0 307 $node->book['bid'] = $parent['bid'];
Chris@0 308 $node->book['pid'] = $parent['nid'];
Chris@0 309 }
Chris@0 310 }
Chris@0 311 // Set defaults.
Chris@0 312 $node_ref = !$node->isNew() ? $node->id() : 'new';
Chris@0 313 $node->book += $book_manager->getLinkDefaults($node_ref);
Chris@0 314 }
Chris@0 315 else {
Chris@0 316 if (isset($node->book['bid']) && !isset($node->book['original_bid'])) {
Chris@0 317 $node->book['original_bid'] = $node->book['bid'];
Chris@0 318 }
Chris@0 319 }
Chris@0 320 // Find the depth limit for the parent select.
Chris@0 321 if (isset($node->book['bid']) && !isset($node->book['parent_depth_limit'])) {
Chris@0 322 $node->book['parent_depth_limit'] = $book_manager->getParentDepthLimit($node->book);
Chris@0 323 }
Chris@0 324 }
Chris@0 325
Chris@0 326 /**
Chris@0 327 * Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\Form\NodeDeleteForm.
Chris@0 328 *
Chris@0 329 * Alters the confirm form for a single node deletion.
Chris@0 330 */
Chris@0 331 function book_form_node_confirm_form_alter(&$form, FormStateInterface $form_state) {
Chris@0 332 // Only need to alter the delete operation form.
Chris@0 333 if ($form_state->getFormObject()->getOperation() !== 'delete') {
Chris@0 334 return;
Chris@0 335 }
Chris@0 336
Chris@0 337 /** @var \Drupal\node\NodeInterface $node */
Chris@0 338 $node = $form_state->getFormObject()->getEntity();
Chris@0 339 if (!book_type_is_allowed($node->getType())) {
Chris@0 340 // Not a book node.
Chris@0 341 return;
Chris@0 342 }
Chris@0 343
Chris@0 344 if (isset($node->book) && $node->book['has_children']) {
Chris@0 345 $form['book_warning'] = [
Chris@0 346 '#markup' => '<p>' . t('%title is part of a book outline, and has associated child pages. If you proceed with deletion, the child pages will be relocated automatically.', ['%title' => $node->label()]) . '</p>',
Chris@0 347 '#weight' => -10,
Chris@0 348 ];
Chris@0 349 }
Chris@0 350 }
Chris@0 351
Chris@0 352 /**
Chris@0 353 * Prepares variables for book listing block templates.
Chris@0 354 *
Chris@0 355 * Default template: book-all-books-block.html.twig.
Chris@0 356 *
Chris@0 357 * All non-renderable elements are removed so that the template has full access
Chris@0 358 * to the structured data but can also simply iterate over all elements and
Chris@0 359 * render them (as in the default template).
Chris@0 360 *
Chris@0 361 * @param array $variables
Chris@0 362 * An associative array containing the following key:
Chris@0 363 * - book_menus: An associative array containing renderable menu links for all
Chris@0 364 * book menus.
Chris@0 365 */
Chris@0 366 function template_preprocess_book_all_books_block(&$variables) {
Chris@0 367 // Remove all non-renderable elements.
Chris@0 368 $elements = $variables['book_menus'];
Chris@0 369 $variables['book_menus'] = [];
Chris@0 370 foreach (Element::children($elements) as $index) {
Chris@0 371 $variables['book_menus'][] = [
Chris@0 372 'id' => $index,
Chris@0 373 'menu' => $elements[$index],
Chris@0 374 'title' => $elements[$index]['#book_title'],
Chris@0 375 ];
Chris@0 376 }
Chris@0 377 }
Chris@0 378
Chris@0 379 /**
Chris@0 380 * Prepares variables for book navigation templates.
Chris@0 381 *
Chris@0 382 * Default template: book-navigation.html.twig.
Chris@0 383 *
Chris@0 384 * @param array $variables
Chris@0 385 * An associative array containing the following key:
Chris@0 386 * - book_link: An associative array of book link properties.
Chris@0 387 * Properties used: bid, link_title, depth, pid, nid.
Chris@0 388 */
Chris@0 389 function template_preprocess_book_navigation(&$variables) {
Chris@0 390 $book_link = $variables['book_link'];
Chris@0 391
Chris@0 392 // Provide extra variables for themers. Not needed by default.
Chris@0 393 $variables['book_id'] = $book_link['bid'];
Chris@0 394 $variables['book_title'] = $book_link['link_title'];
Chris@18 395 $variables['book_url'] = Url::fromRoute('entity.node.canonical', ['node' => $book_link['bid']])->toString();
Chris@0 396 $variables['current_depth'] = $book_link['depth'];
Chris@0 397 $variables['tree'] = '';
Chris@0 398
Chris@0 399 /** @var \Drupal\book\BookOutline $book_outline */
Chris@0 400 $book_outline = \Drupal::service('book.outline');
Chris@0 401
Chris@0 402 if ($book_link['nid']) {
Chris@0 403 $variables['tree'] = $book_outline->childrenLinks($book_link);
Chris@0 404
Chris@0 405 $build = [];
Chris@0 406
Chris@0 407 if ($prev = $book_outline->prevLink($book_link)) {
Chris@18 408 $prev_href = Url::fromRoute('entity.node.canonical', ['node' => $prev['nid']])->toString();
Chris@0 409 $build['#attached']['html_head_link'][][] = [
Chris@0 410 'rel' => 'prev',
Chris@0 411 'href' => $prev_href,
Chris@0 412 ];
Chris@0 413 $variables['prev_url'] = $prev_href;
Chris@0 414 $variables['prev_title'] = $prev['title'];
Chris@0 415 }
Chris@0 416
Chris@0 417 /** @var \Drupal\book\BookManagerInterface $book_manager */
Chris@0 418 $book_manager = \Drupal::service('book.manager');
Chris@0 419 if ($book_link['pid'] && $parent = $book_manager->loadBookLink($book_link['pid'])) {
Chris@18 420 $parent_href = Url::fromRoute('entity.node.canonical', ['node' => $book_link['pid']])->toString();
Chris@0 421 $build['#attached']['html_head_link'][][] = [
Chris@0 422 'rel' => 'up',
Chris@0 423 'href' => $parent_href,
Chris@0 424 ];
Chris@0 425 $variables['parent_url'] = $parent_href;
Chris@0 426 $variables['parent_title'] = $parent['title'];
Chris@0 427 }
Chris@0 428
Chris@0 429 if ($next = $book_outline->nextLink($book_link)) {
Chris@18 430 $next_href = Url::fromRoute('entity.node.canonical', ['node' => $next['nid']])->toString();
Chris@0 431 $build['#attached']['html_head_link'][][] = [
Chris@0 432 'rel' => 'next',
Chris@0 433 'href' => $next_href,
Chris@0 434 ];
Chris@0 435 $variables['next_url'] = $next_href;
Chris@0 436 $variables['next_title'] = $next['title'];
Chris@0 437 }
Chris@0 438 }
Chris@0 439
Chris@0 440 if (!empty($build)) {
Chris@0 441 \Drupal::service('renderer')->render($build);
Chris@0 442 }
Chris@0 443
Chris@0 444 $variables['has_links'] = FALSE;
Chris@0 445 // Link variables to filter for values and set state of the flag variable.
Chris@0 446 $links = ['prev_url', 'prev_title', 'parent_url', 'parent_title', 'next_url', 'next_title'];
Chris@0 447 foreach ($links as $link) {
Chris@0 448 if (isset($variables[$link])) {
Chris@0 449 // Flag when there is a value.
Chris@0 450 $variables['has_links'] = TRUE;
Chris@0 451 }
Chris@0 452 else {
Chris@0 453 // Set empty to prevent notices.
Chris@0 454 $variables[$link] = '';
Chris@0 455 }
Chris@0 456 }
Chris@0 457 }
Chris@0 458
Chris@0 459 /**
Chris@0 460 * Prepares variables for book export templates.
Chris@0 461 *
Chris@0 462 * Default template: book-export-html.html.twig.
Chris@0 463 *
Chris@0 464 * @param array $variables
Chris@0 465 * An associative array containing:
Chris@0 466 * - title: The title of the book.
Chris@0 467 * - contents: Output of each book page.
Chris@0 468 * - depth: The max depth of the book.
Chris@0 469 */
Chris@0 470 function template_preprocess_book_export_html(&$variables) {
Chris@0 471 global $base_url;
Chris@0 472 $language_interface = \Drupal::languageManager()->getCurrentLanguage();
Chris@0 473
Chris@0 474 $variables['base_url'] = $base_url;
Chris@0 475 $variables['language'] = $language_interface;
Chris@0 476 $variables['language_rtl'] = ($language_interface->getDirection() == LanguageInterface::DIRECTION_RTL);
Chris@0 477
Chris@0 478 // HTML element attributes.
Chris@0 479 $attributes = [];
Chris@0 480 $attributes['lang'] = $language_interface->getId();
Chris@0 481 $attributes['dir'] = $language_interface->getDirection();
Chris@0 482 $variables['html_attributes'] = new Attribute($attributes);
Chris@0 483 }
Chris@0 484
Chris@0 485 /**
Chris@0 486 * Prepares variables for single node export templates.
Chris@0 487 *
Chris@0 488 * Default template: book-node-export-html.html.twig.
Chris@0 489 *
Chris@0 490 * @param array $variables
Chris@0 491 * An associative array containing the following keys:
Chris@0 492 * - node: The node that will be output.
Chris@0 493 * - children: All the rendered child nodes within the current node. Defaults
Chris@0 494 * to an empty string.
Chris@0 495 */
Chris@0 496 function template_preprocess_book_node_export_html(&$variables) {
Chris@0 497 $variables['depth'] = $variables['node']->book['depth'];
Chris@0 498 $variables['title'] = $variables['node']->label();
Chris@0 499 }
Chris@0 500
Chris@0 501 /**
Chris@0 502 * Determines if a given node type is in the list of types allowed for books.
Chris@0 503 *
Chris@0 504 * @param string $type
Chris@0 505 * A node type.
Chris@0 506 *
Chris@0 507 * @return bool
Chris@0 508 * A Boolean TRUE if the node type can be included in books; otherwise, FALSE.
Chris@0 509 */
Chris@0 510 function book_type_is_allowed($type) {
Chris@0 511 return in_array($type, \Drupal::config('book.settings')->get('allowed_types'));
Chris@0 512 }
Chris@0 513
Chris@0 514 /**
Chris@0 515 * Implements hook_ENTITY_TYPE_update() for node_type entities.
Chris@0 516 *
Chris@0 517 * Updates book.settings configuration object if the machine-readable name of a
Chris@0 518 * node type is changed.
Chris@0 519 */
Chris@0 520 function book_node_type_update(NodeTypeInterface $type) {
Chris@0 521 if ($type->getOriginalId() != $type->id()) {
Chris@0 522 $config = \Drupal::configFactory()->getEditable('book.settings');
Chris@0 523 // Update the list of node types that are allowed to be added to books.
Chris@0 524 $allowed_types = $config->get('allowed_types');
Chris@0 525 $old_key = array_search($type->getOriginalId(), $allowed_types);
Chris@0 526
Chris@0 527 if ($old_key !== FALSE) {
Chris@0 528 $allowed_types[$old_key] = $type->id();
Chris@0 529 // Ensure that the allowed_types array is sorted consistently.
Chris@0 530 // @see BookSettingsForm::submitForm()
Chris@0 531 sort($allowed_types);
Chris@0 532 $config->set('allowed_types', $allowed_types);
Chris@0 533 }
Chris@0 534
Chris@0 535 // Update the setting for the "Add child page" link.
Chris@0 536 if ($config->get('child_type') == $type->getOriginalId()) {
Chris@0 537 $config->set('child_type', $type->id());
Chris@0 538 }
Chris@0 539 $config->save();
Chris@0 540 }
Chris@0 541 }
Chris@16 542
Chris@16 543 /**
Chris@16 544 * Implements hook_migration_plugins_alter().
Chris@16 545 */
Chris@16 546 function book_migration_plugins_alter(array &$migrations) {
Chris@16 547 // Book settings are migrated identically for Drupal 6 and Drupal 7. However,
Chris@16 548 // a d6_book_settings migration already existed before the consolidated
Chris@16 549 // book_settings migration existed, so to maintain backwards compatibility,
Chris@16 550 // ensure that d6_book_settings is an alias of book_settings.
Chris@16 551 if (isset($migrations['book_settings'])) {
Chris@16 552 $migrations['d6_book_settings'] = &$migrations['book_settings'];
Chris@16 553 }
Chris@16 554 }