first commit

This commit is contained in:
2024-07-15 12:33:27 +02:00
commit ce50ae282b
22084 changed files with 2623791 additions and 0 deletions

15
core/modules/book/book.info.yml Executable file
View File

@@ -0,0 +1,15 @@
name: Book
type: module
description: 'Allows users to create and organize related content in an outline.'
package: Core
# version: VERSION
lifecycle: deprecated
lifecycle_link: https://www.drupal.org/node/3223395#s-book
dependencies:
- drupal:node
configure: book.settings
# Information added by Drupal.org packaging script on 2024-07-04
version: '10.3.1'
project: 'drupal'
datestamp: 1720094222

135
core/modules/book/book.install Executable file
View File

@@ -0,0 +1,135 @@
<?php
/**
* @file
* Install, update and uninstall functions for the book module.
*/
/**
* Implements hook_uninstall().
*/
function book_uninstall() {
// Clear book data out of the cache.
\Drupal::cache('data')->deleteAll();
}
/**
* Implements hook_schema().
*/
function book_schema() {
$schema['book'] = [
'description' => 'Stores book outline information. Uniquely defines the location of each node in the book outline',
'fields' => [
'nid' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => "The book page's {node}.nid.",
],
'bid' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => "The book ID is the {book}.nid of the top-level page.",
],
'pid' => [
'description' => 'The parent ID (pid) is the id of the node above in the hierarchy, or zero if the node is at the top level in its outline.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'has_children' => [
'description' => 'Flag indicating whether any nodes have this node as a parent (1 = children exist, 0 = no children).',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'size' => 'small',
],
'weight' => [
'description' => 'Weight among book entries in the same book at the same depth.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
],
'depth' => [
'description' => 'The depth relative to the top level. A link with pid == 0 will have depth == 1.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'size' => 'small',
],
'p1' => [
'description' => 'The first nid in the materialized path. If N = depth, then pN must equal the nid. If depth > 1 then p(N-1) must equal the pid. All pX where X > depth must equal zero. The columns p1 .. p9 are also called the parents.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'p2' => [
'description' => 'The second nid in the materialized path. See p1.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'p3' => [
'description' => 'The third nid in the materialized path. See p1.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'p4' => [
'description' => 'The fourth nid in the materialized path. See p1.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'p5' => [
'description' => 'The fifth nid in the materialized path. See p1.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'p6' => [
'description' => 'The sixth nid in the materialized path. See p1.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'p7' => [
'description' => 'The seventh nid in the materialized path. See p1.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'p8' => [
'description' => 'The eighth nid in the materialized path. See p1.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'p9' => [
'description' => 'The ninth nid in the materialized path. See p1.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
],
'primary key' => ['nid'],
'indexes' => [
'book_parents' => ['bid', 'p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8', 'p9'],
],
];
return $schema;
}

33
core/modules/book/book.js Executable file
View File

@@ -0,0 +1,33 @@
/**
* @file
* JavaScript behaviors for the Book module.
*/
(function ($, Drupal) {
/**
* Adds summaries to the book outline form.
*
* @type {Drupal~behavior}
*
* @prop {Drupal~behaviorAttach} attach
* Attaches summary behavior to book outline forms.
*/
Drupal.behaviors.bookDetailsSummaries = {
attach(context) {
$(context)
.find('.book-outline-form')
.drupalSetSummary((context) => {
const $select = $(context).find('.book-title-select');
const val = $select[0].value;
if (val === '0') {
return Drupal.t('Not in book');
}
if (val === 'new') {
return Drupal.t('New book');
}
return Drupal.checkPlain($select.find(':selected')[0].textContent);
});
},
};
})(jQuery, Drupal);

View File

@@ -0,0 +1,8 @@
drupal.book:
version: VERSION
js:
book.js: {}
dependencies:
- core/jquery
- core/drupal
- core/drupal.form

View File

@@ -0,0 +1,9 @@
book.admin:
title: Books
description: 'Manage your site''s book outlines.'
parent: system.admin_structure
route_name: book.admin
book.render:
title: Books
route_name: book.render
enabled: 0

View File

@@ -0,0 +1,15 @@
book.admin:
route_name: book.admin
title: 'List'
base_route: book.admin
book.settings:
route_name: book.settings
title: 'Settings'
base_route: book.admin
weight: 100
entity.node.book_outline_form:
route_name: entity.node.book_outline_form
base_route: entity.node.canonical
title: Outline
weight: 2

554
core/modules/book/book.module Executable file
View File

@@ -0,0 +1,554 @@
<?php
/**
* @file
* Allows users to create and organize related content in an outline.
*/
use Drupal\book\BookManager;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
use Drupal\node\NodeTypeInterface;
use Drupal\node\Entity\Node;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Template\Attribute;
/**
* Implements hook_help().
*/
function book_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.book':
$output = '<h2>' . t('About') . '</h2>';
$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. Installing 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>';
$output .= '<h2>' . t('Uses') . '</h2>';
$output .= '<dl>';
$output .= '<dt>' . t('Adding and managing book content') . '</dt>';
$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>';
$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>';
$output .= '<dt>' . t('Configuring content types for books') . '</dt>';
$output .= '<dd>' . t('The <em>Book page</em> content type is the initial content type installed 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>';
$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>';
$output .= '<dt>' . t('Book navigation') . '</dt>';
$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>';
$output .= '<dt>' . t('Collaboration') . '</dt>';
$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>';
$output .= '<dt>' . t('Printing books') . '</dt>';
$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>';
$output .= '</dl>';
return $output;
case 'book.admin':
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>';
case 'entity.node.book_outline_form':
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>';
}
}
/**
* Implements hook_theme().
*/
function book_theme() {
return [
'book_navigation' => [
'variables' => ['book_link' => NULL],
],
'book_tree' => [
'variables' => ['items' => [], 'attributes' => []],
],
'book_export_html' => [
'variables' => ['title' => NULL, 'contents' => NULL, 'depth' => NULL],
],
'book_all_books_block' => [
'render element' => 'book_menus',
],
'book_node_export_html' => [
'variables' => ['node' => NULL, 'content' => NULL, 'children' => NULL],
],
];
}
/**
* Implements hook_entity_type_build().
*/
function book_entity_type_build(array &$entity_types) {
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
$entity_types['node']
->setFormClass('book_outline', 'Drupal\book\Form\BookOutlineForm')
->setLinkTemplate('book-outline-form', '/node/{node}/outline')
->setLinkTemplate('book-remove-form', '/node/{node}/outline/remove')
->addConstraint('BookOutline', []);
}
/**
* Implements hook_node_links_alter().
*/
function book_node_links_alter(array &$links, NodeInterface $node, array &$context) {
if ($context['view_mode'] != 'rss') {
$account = \Drupal::currentUser();
if (isset($node->book['depth'])) {
if ($context['view_mode'] == 'full' && node_is_page($node)) {
$child_type = \Drupal::config('book.settings')->get('child_type');
$access_control_handler = \Drupal::entityTypeManager()->getAccessControlHandler('node');
if (($account->hasPermission('add content to books') || $account->hasPermission('administer book outlines')) && $access_control_handler->createAccess($child_type) && $node->book['depth'] < BookManager::BOOK_MAX_DEPTH) {
$book_links['book_add_child'] = [
'title' => t('Add child page'),
'url' => Url::fromRoute('node.add', ['node_type' => $child_type], ['query' => ['parent' => $node->id()]]),
];
}
if ($account->hasPermission('access printer-friendly version')) {
$book_links['book_printer'] = [
'title' => t('Printer-friendly version'),
'url' => Url::fromRoute('book.export', [
'type' => 'html',
'node' => $node->id(),
]),
'attributes' => ['title' => t('Show a printer-friendly version of this book page and its sub-pages.')],
];
}
}
}
if (!empty($book_links)) {
$links['book'] = [
'#theme' => 'links__node__book',
'#links' => $book_links,
'#attributes' => ['class' => ['links', 'inline']],
];
}
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.
*
* Adds the book form element to the node form.
*
* @see book_pick_book_nojs_submit()
*/
function book_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$node = $form_state->getFormObject()->getEntity();
$account = \Drupal::currentUser();
$access = $account->hasPermission('administer book outlines');
if (!$access) {
if ($account->hasPermission('add content to books') && ((!empty($node->book['bid']) && !$node->isNew()) || book_type_is_allowed($node->getType()))) {
// Already in the book hierarchy, or this node type is allowed.
$access = TRUE;
}
}
if ($access) {
$collapsed = !($node->isNew() && !empty($node->book['pid']));
$form = \Drupal::service('book.manager')->addFormElements($form, $form_state, $node, $account, $collapsed);
// The "js-hide" class hides submit button when JavaScript is enabled.
$form['book']['pick-book'] = [
'#type' => 'submit',
'#value' => t('Change book (update list of parents)'),
'#submit' => ['book_pick_book_nojs_submit'],
'#weight' => 20,
'#attributes' => [
'class' => [
'js-hide',
],
],
];
$form['#entity_builders'][] = 'book_node_builder';
}
}
/**
* Entity form builder to add the book information to the node.
*
* @todo Remove this in favor of an entity field.
*/
function book_node_builder($entity_type, NodeInterface $entity, &$form, FormStateInterface $form_state) {
$entity->book = $form_state->getValue('book');
// Always save a revision for non-administrators.
if (!empty($entity->book['bid']) && !\Drupal::currentUser()->hasPermission('administer nodes')) {
$entity->setNewRevision();
}
}
/**
* Form submission handler for node_form().
*
* This handler is run when JavaScript is disabled. It triggers the form to
* rebuild so that the "Parent item" options are changed to reflect the newly
* selected book. When JavaScript is enabled, the submit button that triggers
* this handler is hidden, and the "Book" dropdown directly triggers the
* book_form_update() Ajax callback instead.
*
* @see book_form_update()
* @see book_form_node_form_alter()
*/
function book_pick_book_nojs_submit($form, FormStateInterface $form_state) {
$node = $form_state->getFormObject()->getEntity();
$node->book = $form_state->getValue('book');
$form_state->setRebuild();
}
/**
* Renders a new parent page select element when the book selection changes.
*
* This function is called via Ajax when the selected book is changed on a node
* or book outline form.
*
* @return array
* The rendered parent page select element.
*/
function book_form_update($form, FormStateInterface $form_state) {
return $form['book']['pid'];
}
/**
* Implements hook_ENTITY_TYPE_load() for node entities.
*/
function book_node_load($nodes) {
/** @var \Drupal\book\BookManagerInterface $book_manager */
$book_manager = \Drupal::service('book.manager');
$links = $book_manager->loadBookLinks(array_keys($nodes), FALSE);
foreach ($links as $record) {
$nodes[$record['nid']]->book = $record;
$nodes[$record['nid']]->book['link_path'] = 'node/' . $record['nid'];
$nodes[$record['nid']]->book['link_title'] = $nodes[$record['nid']]->label();
}
}
/**
* Implements hook_ENTITY_TYPE_view() for node entities.
*/
function book_node_view(array &$build, EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) {
if ($view_mode == 'full') {
if (!empty($node->book['bid']) && empty($node->in_preview)) {
$book_node = Node::load($node->book['bid']);
if (!$book_node->access()) {
return;
}
$build['book_navigation'] = [
'#theme' => 'book_navigation',
'#book_link' => $node->book,
'#weight' => 100,
// The book navigation is a listing of Node entities, so associate its
// list cache tag for correct invalidation.
'#cache' => [
'tags' => $node->getEntityType()->getListCacheTags(),
],
];
}
}
}
/**
* Implements hook_ENTITY_TYPE_presave() for node entities.
*/
function book_node_presave(EntityInterface $node) {
// Make sure a new node gets a new menu link.
if ($node->isNew()) {
$node->book['nid'] = NULL;
}
}
/**
* Implements hook_ENTITY_TYPE_insert() for node entities.
*/
function book_node_insert(EntityInterface $node) {
/** @var \Drupal\book\BookManagerInterface $book_manager */
$book_manager = \Drupal::service('book.manager');
$book_manager->updateOutline($node);
}
/**
* Implements hook_ENTITY_TYPE_update() for node entities.
*/
function book_node_update(EntityInterface $node) {
/** @var \Drupal\book\BookManagerInterface $book_manager */
$book_manager = \Drupal::service('book.manager');
$book_manager->updateOutline($node);
}
/**
* Implements hook_ENTITY_TYPE_predelete() for node entities.
*/
function book_node_predelete(EntityInterface $node) {
if (!empty($node->book['bid'])) {
/** @var \Drupal\book\BookManagerInterface $book_manager */
$book_manager = \Drupal::service('book.manager');
$book_manager->deleteFromBook($node->book['nid']);
}
}
/**
* Implements hook_ENTITY_TYPE_prepare_form() for node entities.
*/
function book_node_prepare_form(NodeInterface $node, $operation, FormStateInterface $form_state) {
/** @var \Drupal\book\BookManagerInterface $book_manager */
$book_manager = \Drupal::service('book.manager');
// Prepare defaults for the add/edit form.
$account = \Drupal::currentUser();
if (empty($node->book) && ($account->hasPermission('add content to books') || $account->hasPermission('administer book outlines'))) {
$node->book = [];
$query = \Drupal::request()->query;
if ($node->isNew() && !is_null($query->get('parent')) && is_numeric($query->get('parent'))) {
// Handle "Add child page" links:
$parent = $book_manager->loadBookLink($query->get('parent'), TRUE);
if ($parent && $parent['access']) {
$node->book['bid'] = $parent['bid'];
$node->book['pid'] = $parent['nid'];
}
}
// Set defaults.
$node_ref = !$node->isNew() ? $node->id() : 'new';
$node->book += $book_manager->getLinkDefaults($node_ref);
}
else {
if (isset($node->book['bid']) && !isset($node->book['original_bid'])) {
$node->book['original_bid'] = $node->book['bid'];
}
}
// Find the depth limit for the parent select.
if (isset($node->book['bid']) && !isset($node->book['parent_depth_limit'])) {
$node->book['parent_depth_limit'] = $book_manager->getParentDepthLimit($node->book);
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\Form\NodeDeleteForm.
*
* Alters the confirm form for a single node deletion.
*/
function book_form_node_confirm_form_alter(&$form, FormStateInterface $form_state) {
// Only need to alter the delete operation form.
if ($form_state->getFormObject()->getOperation() !== 'delete') {
return;
}
/** @var \Drupal\node\NodeInterface $node */
$node = $form_state->getFormObject()->getEntity();
if (!book_type_is_allowed($node->getType())) {
// Not a book node.
return;
}
if (isset($node->book) && $node->book['has_children']) {
$form['book_warning'] = [
'#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>',
'#weight' => -10,
];
}
}
/**
* Prepares variables for book listing block templates.
*
* Default template: book-all-books-block.html.twig.
*
* All non-renderable elements are removed so that the template has full access
* to the structured data but can also simply iterate over all elements and
* render them (as in the default template).
*
* @param array $variables
* An associative array containing the following key:
* - book_menus: An associative array containing renderable menu links for all
* book menus.
*/
function template_preprocess_book_all_books_block(&$variables) {
// Remove all non-renderable elements.
$elements = $variables['book_menus'];
$variables['book_menus'] = [];
foreach (Element::children($elements) as $index) {
$variables['book_menus'][] = [
'id' => $index,
'menu' => $elements[$index],
'title' => $elements[$index]['#book_title'],
];
}
}
/**
* Prepares variables for book navigation templates.
*
* Default template: book-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_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] = '';
}
}
}
/**
* Prepares variables for book export templates.
*
* Default template: book-export-html.html.twig.
*
* @param array $variables
* An associative array containing:
* - title: The title of the book.
* - contents: Output of each book page.
* - depth: The max depth of the book.
*/
function template_preprocess_book_export_html(&$variables) {
global $base_url;
$language_interface = \Drupal::languageManager()->getCurrentLanguage();
$variables['base_url'] = $base_url;
$variables['language'] = $language_interface;
$variables['language_rtl'] = ($language_interface->getDirection() == LanguageInterface::DIRECTION_RTL);
// HTML element attributes.
$attributes = [];
$attributes['lang'] = $language_interface->getId();
$attributes['dir'] = $language_interface->getDirection();
$variables['html_attributes'] = new Attribute($attributes);
}
/**
* Prepares variables for single node export templates.
*
* Default template: book-node-export-html.html.twig.
*
* @param array $variables
* An associative array containing the following keys:
* - node: The node that will be output.
* - children: All the rendered child nodes within the current node. Defaults
* to an empty string.
*/
function template_preprocess_book_node_export_html(&$variables) {
$variables['depth'] = $variables['node']->book['depth'];
$variables['title'] = $variables['node']->label();
}
/**
* Determines if a given node type is in the list of types allowed for books.
*
* @param string $type
* A node type.
*
* @return bool
* A Boolean TRUE if the node type can be included in books; otherwise, FALSE.
*/
function book_type_is_allowed($type) {
return in_array($type, \Drupal::config('book.settings')->get('allowed_types'));
}
/**
* Implements hook_ENTITY_TYPE_update() for node_type entities.
*
* Updates book.settings configuration object if the machine-readable name of a
* node type is changed.
*/
function book_node_type_update(NodeTypeInterface $type) {
if ($type->getOriginalId() != $type->id()) {
$config = \Drupal::configFactory()->getEditable('book.settings');
// Update the list of node types that are allowed to be added to books.
$allowed_types = $config->get('allowed_types');
$old_key = array_search($type->getOriginalId(), $allowed_types);
if ($old_key !== FALSE) {
$allowed_types[$old_key] = $type->id();
// Ensure that the allowed_types array is sorted consistently.
// @see BookSettingsForm::submitForm()
sort($allowed_types);
$config->set('allowed_types', $allowed_types);
}
// Update the setting for the "Add child page" link.
if ($config->get('child_type') == $type->getOriginalId()) {
$config->set('child_type', $type->id());
}
$config->save();
}
}
/**
* Implements hook_migration_plugins_alter().
*/
function book_migration_plugins_alter(array &$migrations) {
// Book settings are migrated identically for Drupal 6 and Drupal 7. However,
// a d6_book_settings migration already existed before the consolidated
// book_settings migration existed, so to maintain backwards compatibility,
// ensure that d6_book_settings is an alias of book_settings.
if (isset($migrations['book_settings'])) {
$migrations['d6_book_settings'] = &$migrations['book_settings'];
}
}

View File

@@ -0,0 +1,9 @@
administer book outlines:
title: 'Administer book outlines'
create new books:
title: 'Create new books'
add content to books:
title: 'Add content and child pages to books and manage their hierarchies.'
access printer-friendly version:
title: 'View printer-friendly books'
description: 'View a book page and all of its sub-pages as a single document for ease of printing. Can be performance heavy.'

View File

@@ -0,0 +1,67 @@
book.render:
path: '/book'
defaults:
_controller: '\Drupal\book\Controller\BookController::bookRender'
_title: 'Books'
requirements:
_permission: 'access content'
book.admin:
path: '/admin/structure/book'
defaults:
_controller: '\Drupal\book\Controller\BookController::adminOverview'
_title: 'Books'
requirements:
_permission: 'administer book outlines'
book.settings:
path: '/admin/structure/book/settings'
defaults:
_form: '\Drupal\book\Form\BookSettingsForm'
_title: 'Books'
requirements:
_permission: 'administer site configuration'
book.export:
path: '/book/export/{type}/{node}'
defaults:
_controller: '\Drupal\book\Controller\BookController::bookExport'
requirements:
_permission: 'access printer-friendly version'
_entity_access: 'node.view'
node: \d+
entity.node.book_outline_form:
path: '/node/{node}/outline'
defaults:
_entity_form: 'node.book_outline'
_title: 'Outline'
requirements:
_permission: 'administer book outlines'
_entity_access: 'node.view'
node: \d+
options:
_node_operation_route: TRUE
book.admin_edit:
path: '/admin/structure/book/{node}'
defaults:
_form: '\Drupal\book\Form\BookAdminEditForm'
_title: 'Re-order book pages and change titles'
requirements:
_permission: 'administer book outlines'
_entity_access: 'node.view'
node: \d+
entity.node.book_remove_form:
path: '/node/{node}/outline/remove'
defaults:
_form: '\Drupal\book\Form\BookRemoveForm'
_title: 'Remove from outline'
options:
_node_operation_route: TRUE
requirements:
_permission: 'administer book outlines'
_entity_access: 'node.view'
_access_book_removable: 'TRUE'
node: \d+

View File

@@ -0,0 +1,57 @@
services:
book.breadcrumb:
class: Drupal\book\BookBreadcrumbBuilder
arguments: ['@entity_type.manager', '@current_user', '@entity.repository', '@language_manager']
tags:
- { name: breadcrumb_builder, priority: 701 }
book.manager:
class: Drupal\book\BookManager
arguments: ['@entity_type.manager', '@string_translation', '@config.factory', '@book.outline_storage', '@renderer', '@language_manager', '@entity.repository', '@book.backend_chained_cache', '@book.memory_cache']
lazy: true
Drupal\book\BookManagerInterface: '@book.manager'
book.outline:
class: Drupal\book\BookOutline
arguments: ['@book.manager']
Drupal\book\BookOutline: '@book.outline'
book.export:
class: Drupal\book\BookExport
arguments: ['@entity_type.manager', '@book.manager', '@entity.repository']
Drupal\book\BookExport: '@book.export'
book.outline_storage:
class: Drupal\book\BookOutlineStorage
arguments: ['@database']
tags:
- { name: backend_overridable }
Drupal\book\BookOutlineStorageInterface: '@book.outline_storage'
access_check.book.removable:
class: Drupal\book\Access\BookNodeIsRemovableAccessCheck
arguments: ['@book.manager']
tags:
- { name: access_check, applies_to: _access_book_removable }
cache_context.route.book_navigation:
class: Drupal\book\Cache\BookNavigationCacheContext
arguments: ['@current_route_match', '@book.manager']
tags:
- { name: cache.context}
book.uninstall_validator:
class: Drupal\book\BookUninstallValidator
tags:
- { name: module_install.uninstall_validator }
arguments: ['@book.outline_storage', '@entity_type.manager', '@string_translation']
lazy: true
book.memory_cache:
class: Drupal\Core\Cache\MemoryCache\MemoryCacheInterface
tags:
- { name: cache.bin.memory, default_backend: cache.backend.memory.memory }
factory: ['@cache_factory', 'get']
arguments: [memory_cache]
book.backend_chained_cache:
class: Drupal\Core\Cache\BackendChain
calls:
- [appendBackend, ['@book.memory_cache']]
- [appendBackend, ['@cache.data']]
tags:
# This tag ensures that Drupal's cache_tags.invalidator service
# invalidates also this cache data.
- { name: cache.bin.memory }

125
core/modules/book/book.views.inc Executable file
View File

@@ -0,0 +1,125 @@
<?php
/**
* @file
* Provide views data for book.module.
*
* @ingroup views_module_handlers
*/
/**
* Implements hook_views_data().
*/
function book_views_data() {
$data = [];
$data['book'] = [];
$data['book']['table'] = [];
$data['book']['table']['group'] = t('Book');
$data['book']['table']['join'] = [
'node_field_data' => [
'left_field' => 'nid',
'field' => 'nid',
],
];
$data['book']['nid'] = [
'title' => t('Page'),
'help' => t('The book page node.'),
'relationship' => [
'base' => 'node_field_data',
'id' => 'standard',
'label' => t('Book Page'),
],
];
$data['book']['bid'] = [
'title' => t('Top level book'),
'help' => t('The book the node is in.'),
'relationship' => [
'base' => 'node_field_data',
'id' => 'standard',
'label' => t('Book'),
],
];
$data['book']['pid'] = [
'title' => t('Parent'),
'help' => t('The parent book node.'),
'relationship' => [
'base' => 'node_field_data',
'id' => 'standard',
'label' => t('Book Parent'),
],
];
$data['book']['has_children'] = [
'title' => t('Page has Children'),
'help' => t('Flag indicating whether this book page has children'),
'field' => [
'id' => 'boolean',
],
'sort' => [
'id' => 'standard',
],
'filter' => [
'id' => 'boolean',
'label' => t('Has Children'),
],
'argument' => [
'id' => 'numeric',
],
];
$data['book']['weight'] = [
'title' => t('Weight'),
'help' => t('The weight of the book page.'),
'field' => [
'id' => 'numeric',
],
'sort' => [
'id' => 'standard',
],
];
$data['book']['depth'] = [
'title' => t('Depth'),
'help' => t('The depth of the book page in the hierarchy; top level book pages have a depth of 1.'),
'field' => [
'id' => 'numeric',
],
'sort' => [
'id' => 'standard',
],
'filter' => [
'id' => 'numeric',
],
'argument' => [
'id' => 'standard',
],
];
$parents = [
1 => t('1st parent'),
2 => t('2nd parent'),
3 => t('3rd parent'),
4 => t('4th parent'),
5 => t('5th parent'),
6 => t('6th parent'),
7 => t('7th parent'),
8 => t('8th parent'),
9 => t('9th parent'),
];
foreach ($parents as $i => $parent_label) {
$data['book']["p$i"] = [
'title' => $parent_label,
'help' => t('The @parent of book node.', ['@parent' => $parent_label]),
'relationship' => [
'base' => 'node_field_data',
'id' => 'standard',
'label' => t('Book @parent', ['@parent' => $parent_label]),
],
];
}
return $data;
}

View File

@@ -0,0 +1,25 @@
---
label: 'Managing books'
top_level: true
---
{% set user_overview_topic = render_var(help_topic_link('user.overview')) %}
<h2>{% trans %}What is a book?{% endtrans %}</h2>
<p>{% trans %}A book is a structured group of content pages, arranged in a hierarchical structure called a <em>book outline</em>. A book hierarchy can be up to nine levels deep, and a book can include <em>Book page</em> content items or other content items. Every book has a default book-specific navigation block, which 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.{% endtrans %}</p>
<h2>{% trans %}What are the permissions for books?{% endtrans %}</h2>
<p>{% trans %}The following permissions are needed to create and manage books; see {{ user_overview_topic }} and its related topics for more about permissions.{% endtrans %}</p>
<dl>
<dt>{% trans %}Create new books{% endtrans %}</dt>
<dd>{% trans %}Allows users to add new books to the site.{% endtrans %}</dd>
<dt>{% trans %}Add content and child pages to books and manage their hierarchies{% endtrans %}</dt>
<dd>{% trans %}Allows users to add configured types of content to existing books.{% endtrans %}</dd>
<dt>{% trans %}Administer book outlines{% endtrans %}</dt>
<dd>{% trans %}Allows users to add <em>any</em> type of content to a book, use the book overview administration page, and rearrange the pages of a book from the book outline page.{% endtrans %}</dd>
<dt>{% trans %}Administer site configuration (in the System module section){% endtrans %}</dt>
<dd>{% trans %}Allows users to do many site configuration tasks, including configuring books. This permission has security implications.{% endtrans %}</dd>
<dt>{% trans %}View printer-friendly books{% endtrans %}</dt>
<dd>{% trans %}Allows users to click the <em>printer-friendly version</em> link to generate a printer-friendly display of the page, which includes pages below it in the book outline.{% endtrans %}</dd>
<dt>{% trans %}<em>Book page</em> content permissions (in the Node module section){% endtrans %}</dt>
<dd>{% trans %}Like other content types, the <em>Book page</em> content type has separate permissions for creating, editing, and deleting a user's own and any content items of this type.{% endtrans %}</dd>
</dl>
<h2>{% trans %}Managing books{% endtrans %}</h2>
<p>{% trans %}Book management is handled by the core Book module. The topics listed below will help you create, edit, and configure books.{% endtrans %}</p>

View File

@@ -0,0 +1,21 @@
---
label: 'Adding content to a book'
related:
- book.about
- book.configuring
- book.creating
- book.organizing
---
{% set node_add_link_text %}{% trans %}Add content{% endtrans %}{% endset %}
{% set node_add_link = render_var(help_route_link(node_add_link_text, 'node.add_page')) %}
{% set configuring_topic = render_var(help_topic_link('book.configuring')) %}
<h2>{% trans %}Goal{% endtrans %}</h2>
<p>{% trans %}Add a page to an existing book.{% endtrans %}</p>
<h2>{% trans %}Steps{% endtrans %}</h2>
<ol>
<li>{% trans %}In the <em>Manage</em> administrative menu, navigate to <em>Content</em> &gt; <em>{{ node_add_link }}</em> &gt; <em>Book page</em>. If you have configured additional content types that can be added to books, you can substitute a different content type for <em>Book page</em>; see {{ configuring_topic }} for more information.{% endtrans %}</li>
<li>{% trans %}Enter a title for the page and some text for the body of the page.{% endtrans %}</li>
<li>{% trans %}In the vertical tabs area, click <em>Book Outline</em>. Select the book you want to add the page to in the <em>Book</em> select list. If you want to insert this page into the book hierarchy, also select the desired parent page in the <em>Parent item</em> select list.{% endtrans %}</li>
<li>{% trans %}Select the desired weight for the page in the <em>Weight</em> select list (pages with the same parent item are ordered from lowest to highest weight).{% endtrans %}</li>
<li>{% trans %}Click <em>Save</em> to add the page to the book.{% endtrans %}</li>
</ol>

View File

@@ -0,0 +1,19 @@
---
label: 'Configuring books'
related:
- book.about
- book.adding
- book.creating
- book.organizing
---
{% set settings_link_text %}{% trans %}Settings{% endtrans %}{% endset %}
{% set settings_link = render_var(help_route_link(settings_link_text, 'book.settings')) %}
<h2>{% trans %}Goal{% endtrans %}</h2>
<p>{% trans %}Configure settings related to books.{% endtrans %}</p>
<h2>{% trans %}Steps{% endtrans %}</h2>
<ol>
<li>{% trans %}In the <em>Manage</em> administrative menu, navigate to <em>Structure</em> &gt; <em>Books</em> &gt; <em>{{ settings_link }}</em>.{% endtrans %}</li>
<li>{% trans %}Check all of the content types that you would like to use as book pages in the <em>Content types allowed in book outlines</em> field.{% endtrans %}</li>
<li>{% trans %}In the <em>Content type for the Add child page link</em> field, select the content type that will be created from the <em>Add child page</em> link on a book page.{% endtrans %}</li>
<li>{% trans %}Click <em>Save configuration</em>.{% endtrans %}</li>
</ol>

View File

@@ -0,0 +1,17 @@
---
label: 'Creating a book'
related:
- book.about
- book.adding
- book.organizing
- book.configuring
---
<h2>{% trans %}Goal{% endtrans %}</h2>
<p>{% trans %}Create a new book.{% endtrans %}</p>
<h2>{% trans %}Steps{% endtrans %}</h2>
<ol>
<li>{% trans %}In the <em>Manage</em> administrative menu, navigate to <em>Content</em> &gt; <em>Add content</em> &gt; <em>Book page</em>. If you have configured additional content types that can be added to books, you can substitute a different content type for <em>Book page</em>.{% endtrans %}</li>
<li>{% trans %}Enter a title for the book, and if desired, some text for the body of the book's title page.{% endtrans %}</li>
<li>{% trans %}In the vertical tabs area, click <em>Book Outline</em>. Select <em>- Create a new book -</em> from the <em>Book</em> select list.{% endtrans %}</li>
<li>{% trans %}Click <em>Save</em> to create the book.{% endtrans %}</li>
</ol>

View File

@@ -0,0 +1,20 @@
---
label: 'Changing the outline of a book'
related:
- book.about
- book.adding
- book.creating
- book.configuring
---
{% set overview_link_text %}{% trans %}Books{% endtrans %}{% endset %}
{% set overview_link = render_var(help_route_link(overview_link_text, 'book.admin')) %}
<h2>{% trans %}Goal{% endtrans %}</h2>
<p>{% trans %}Change the order and titles of pages within a book.{% endtrans %}</p>
<h2>{% trans %}Steps{% endtrans %}</h2>
<ol>
<li>{% trans %}In the <em>Manage</em> administrative menu, navigate to <em>Structure</em> &gt; <em>{{ overview_link }}</em>.{% endtrans %}</li>
<li>{% trans %}Click <em>Edit order and titles</em> for the book you would like to change.{% endtrans %}</li>
<li>{% trans %}Drag the book pages to the desired order.{% endtrans %}</li>
<li>{% trans %}If desired, enter new text for one or more of the page titles within the book.{% endtrans %}</li>
<li>{% trans %}Click <em>Save book pages</em>.{% endtrans %}</li>
</ol>

View File

@@ -0,0 +1,20 @@
id: book_settings
label: Book configuration
migration_tags:
- Drupal 6
- Drupal 7
- Configuration
source:
plugin: variable
variables:
- book_child_type
- book_block_mode
- book_allowed_types
source_module: book
process:
child_type: book_child_type
'block/navigation/mode': book_block_mode
allowed_types: book_allowed_types
destination:
plugin: config
config_name: book.settings

View File

@@ -0,0 +1,25 @@
# cspell:ignore plid
id: d6_book
label: Books
migration_tags:
- Drupal 6
- Content
source:
plugin: book
process:
nid: nid
'book/bid': bid
'book/weight': weight
'book/pid':
-
plugin: skip_on_empty
method: process
source: plid
-
plugin: migration_lookup
migration: d6_book
destination:
plugin: book
migration_dependencies:
required:
- d6_node

View File

@@ -0,0 +1,25 @@
# cspell:ignore plid
id: d7_book
label: Books
migration_tags:
- Drupal 7
- Content
source:
plugin: book
process:
nid: nid
'book/bid': bid
'book/weight': weight
'book/pid':
-
plugin: skip_on_empty
method: process
source: plid
-
plugin: migration_lookup
migration: d7_book
destination:
plugin: book
migration_dependencies:
required:
- d7_node

View File

@@ -0,0 +1,5 @@
finished:
6:
book: book
7:
book: book

View File

@@ -0,0 +1,45 @@
<?php
namespace Drupal\book\Access;
use Drupal\book\BookManagerInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\node\NodeInterface;
/**
* Determines whether the requested node can be removed from its book.
*/
class BookNodeIsRemovableAccessCheck implements AccessInterface {
/**
* Book Manager Service.
*
* @var \Drupal\book\BookManagerInterface
*/
protected $bookManager;
/**
* Constructs a BookNodeIsRemovableAccessCheck object.
*
* @param \Drupal\book\BookManagerInterface $book_manager
* Book Manager Service.
*/
public function __construct(BookManagerInterface $book_manager) {
$this->bookManager = $book_manager;
}
/**
* Checks access for removing the node from its book.
*
* @param \Drupal\node\NodeInterface $node
* The node requested to be removed from its book.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
public function access(NodeInterface $node) {
return AccessResult::allowedIf($this->bookManager->checkNodeIsRemovable($node))->addCacheableDependency($node);
}
}

View File

@@ -0,0 +1,119 @@
<?php
namespace Drupal\book;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\node\NodeInterface;
/**
* Provides a breadcrumb builder for nodes in a book.
*/
class BookBreadcrumbBuilder implements BreadcrumbBuilderInterface {
use StringTranslationTrait;
/**
* The node storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $nodeStorage;
/**
* The current user account.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;
/**
* The entity repository service.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* The language manager service.
*
* @var \Drupal\Core\Language\LanguageManagerInterface|null
*/
protected $languageManager;
/**
* Constructs the BookBreadcrumbBuilder.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Drupal\Core\Session\AccountInterface $account
* The current user account.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, AccountInterface $account, EntityRepositoryInterface $entity_repository, LanguageManagerInterface $language_manager) {
$this->nodeStorage = $entity_type_manager->getStorage('node');
$this->account = $account;
$this->entityRepository = $entity_repository;
$this->languageManager = $language_manager;
}
/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match) {
$node = $route_match->getParameter('node');
return $node instanceof NodeInterface && !empty($node->book);
}
/**
* {@inheritdoc}
*/
public function build(RouteMatchInterface $route_match) {
$book_nids = [];
$breadcrumb = new Breadcrumb();
$links = [Link::createFromRoute($this->t('Home'), '<front>', [], [
'language' => $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT),
]),
];
$breadcrumb->addCacheContexts(['languages:' . LanguageInterface::TYPE_CONTENT]);
$book = $route_match->getParameter('node')->book;
$depth = 1;
// We skip the current node.
while (!empty($book['p' . ($depth + 1)])) {
$book_nids[] = $book['p' . $depth];
$depth++;
}
/** @var \Drupal\node\NodeInterface[] $parent_books */
$parent_books = $this->nodeStorage->loadMultiple($book_nids);
$parent_books = array_map([$this->entityRepository, 'getTranslationFromContext'], $parent_books);
if (count($parent_books) > 0) {
$depth = 1;
while (!empty($book['p' . ($depth + 1)])) {
if (!empty($parent_books[$book['p' . $depth]]) && ($parent_book = $parent_books[$book['p' . $depth]])) {
$access = $parent_book->access('view', $this->account, TRUE);
$breadcrumb->addCacheableDependency($access);
if ($access->isAllowed()) {
$breadcrumb->addCacheableDependency($parent_book);
$links[] = $parent_book->toLink();
}
}
$depth++;
}
}
$breadcrumb->setLinks($links);
$breadcrumb->addCacheContexts(['route.book_navigation']);
return $breadcrumb;
}
}

View File

@@ -0,0 +1,157 @@
<?php
namespace Drupal\book;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\node\NodeInterface;
/**
* Provides methods for exporting book to different formats.
*
* If you would like to add another format, swap this class in container.
*/
class BookExport {
/**
* The node storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $nodeStorage;
/**
* The node view builder.
*
* @var \Drupal\Core\Entity\EntityViewBuilderInterface
*/
protected $viewBuilder;
/**
* The book manager.
*
* @var \Drupal\book\BookManagerInterface
*/
protected $bookManager;
/**
* The entity repository service.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* Constructs a BookExport object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\book\BookManagerInterface $book_manager
* The book manager.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, BookManagerInterface $book_manager, EntityRepositoryInterface $entity_repository) {
$this->nodeStorage = $entity_type_manager->getStorage('node');
$this->viewBuilder = $entity_type_manager->getViewBuilder('node');
$this->bookManager = $book_manager;
$this->entityRepository = $entity_repository;
}
/**
* Generates HTML for export when invoked by book_export().
*
* The given node is embedded to its absolute depth in a top level section. For
* example, a child node with depth 2 in the hierarchy is contained in
* (otherwise empty) <div> elements corresponding to depth 0 and depth 1.
* This is intended to support WYSIWYG output; for instance, level 3 sections
* always look like level 3 sections, no matter their depth relative to the
* node selected to be exported as printer-friendly HTML.
*
* @param \Drupal\node\NodeInterface $node
* The node to export.
*
* @return array
* A render array representing the HTML for a node and its children in the
* book hierarchy.
*
* @throws \Exception
* Thrown when the node was not attached to a book.
*/
public function bookExportHtml(NodeInterface $node) {
if (!isset($node->book)) {
throw new \Exception();
}
$tree = $this->bookManager->bookSubtreeData($node->book);
$contents = $this->exportTraverse($tree, [$this, 'bookNodeExport']);
$node = $this->entityRepository->getTranslationFromContext($node);
return [
'#theme' => 'book_export_html',
'#title' => $node->label(),
'#contents' => $contents,
'#depth' => $node->book['depth'],
'#cache' => [
'tags' => $node->getEntityType()->getListCacheTags(),
],
];
}
/**
* Traverses the book tree to build printable or exportable output.
*
* During the traversal, the callback is applied to each node and is called
* recursively for each child of the node (in weight, title order).
*
* @param array $tree
* A subtree of the book menu hierarchy, rooted at the current page.
* @param callable $callable
* A callback to be called upon visiting a node in the tree.
*
* @return array
* The render array generated in visiting each node.
*/
protected function exportTraverse(array $tree, $callable) {
// If there is no valid callable, use the default callback.
$callable = !empty($callable) ? $callable : [$this, 'bookNodeExport'];
$build = [];
foreach ($tree as $data) {
// Access checking is already performed when building the tree.
if ($node = $this->nodeStorage->load($data['link']['nid'])) {
$node = $this->entityRepository->getTranslationFromContext($node);
$children = $data['below'] ? $this->exportTraverse($data['below'], $callable) : '';
$build[] = call_user_func($callable, $node, $children);
}
}
return $build;
}
/**
* Generates printer-friendly HTML for a node.
*
* @param \Drupal\node\NodeInterface $node
* The node that will be output.
* @param string $children
* (optional) All the rendered child nodes within the current node. Defaults
* to an empty string.
*
* @return array
* A render array for the exported HTML of a given node.
*
* @see \Drupal\book\BookExport::exportTraverse()
*/
protected function bookNodeExport(NodeInterface $node, $children = '') {
$build = $this->viewBuilder->view($node, 'print', NULL);
unset($build['#theme']);
return [
'#theme' => 'book_node_export_html',
'#content' => $build,
'#node' => $node,
'#children' => $children,
];
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,301 @@
<?php
namespace Drupal\book;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
/**
* Provides an interface defining a book manager.
*/
interface BookManagerInterface {
/**
* Gets the data structure representing a named menu tree.
*
* Since this can be the full tree including hidden items, the data returned
* may be used for generating an admin interface or a select.
*
* Note: based on menu_tree_all_data().
*
* @param int $bid
* The Book ID to find links for.
* @param array|null $link
* (optional) A fully loaded menu link, or NULL. If a link is supplied, only
* the path to root will be included in the returned tree - as if this link
* represented the current page in a visible menu.
* @param int|null $max_depth
* (optional) Maximum depth of links to retrieve. Typically useful if only
* one or two levels of a sub tree are needed in conjunction with a non-NULL
* $link, in which case $max_depth should be greater than $link['depth'].
*
* @return array
* A tree of menu links in an array, in the order they should be rendered.
*/
public function bookTreeAllData($bid, $link = NULL, $max_depth = NULL);
/**
* Gets the active trail IDs for the specified book at the provided path.
*
* @param string $bid
* The Book ID to find links for.
* @param array $link
* A fully loaded menu link.
*
* @return array
* An array containing the active trail: a list of menu link IDs.
*/
public function getActiveTrailIds($bid, $link);
/**
* Loads a single book entry.
*
* The entries of a book entry is documented in
* \Drupal\book\BookOutlineStorageInterface::loadMultiple.
*
* If $translate is TRUE, it also checks access ('access' key) and
* loads the title from the node itself.
*
* @param int $nid
* The node ID of the book.
* @param bool $translate
* If TRUE, set access, title, and other elements.
*
* @return array
* The book data of that node.
*
* @see \Drupal\book\BookOutlineStorageInterface::loadMultiple
*/
public function loadBookLink($nid, $translate = TRUE);
/**
* Loads multiple book entries.
*
* The entries of a book entry is documented in
* \Drupal\book\BookOutlineStorageInterface::loadMultiple.
*
* If $translate is TRUE, it also checks access ('access' key) and
* loads the title from the node itself.
*
* @param int[] $nids
* An array of nids to load.
* @param bool $translate
* If TRUE, set access, title, and other elements.
*
* @return array[]
* The book data of each node keyed by NID.
*
* @see \Drupal\book\BookOutlineStorageInterface::loadMultiple
*/
public function loadBookLinks($nids, $translate = TRUE);
/**
* Returns an array of book pages in table of contents order.
*
* @param int $bid
* The ID of the book whose pages are to be listed.
* @param int $depth_limit
* Any link deeper than this value will be excluded (along with its
* children).
* @param array $exclude
* (optional) An array of menu link ID values. Any link whose menu link ID
* is in this array will be excluded (along with its children). Defaults to
* an empty array.
*
* @return array
* An array of (menu link ID, title) pairs for use as options for selecting
* a book page.
*/
public function getTableOfContents($bid, $depth_limit, array $exclude = []);
/**
* Finds the depth limit for items in the parent select.
*
* @param array $book_link
* A fully loaded menu link that is part of the book hierarchy.
*
* @return int
* The depth limit for items in the parent select.
*/
public function getParentDepthLimit(array $book_link);
/**
* Collects node links from a given menu tree recursively.
*
* @param array $tree
* The menu tree you wish to collect node links from.
* @param array $node_links
* An array in which to store the collected node links.
*/
public function bookTreeCollectNodeLinks(&$tree, &$node_links);
/**
* Provides book loading, access control and translation.
*
* @param array $link
* A book link.
*/
public function bookLinkTranslate(&$link);
/**
* Gets the book for a page and returns it as a linear array.
*
* @param array $book_link
* A fully loaded book link that is part of the book hierarchy.
*
* @return array
* A linear array of book links in the order that the links are shown in the
* book, so the previous and next pages are the elements before and after the
* element corresponding to the current node. The children of the current node
* (if any) will come immediately after it in the array, and links will only
* be fetched as deep as one level deeper than $book_link.
*/
public function bookTreeGetFlat(array $book_link);
/**
* Returns an array of all books.
*
* This list may be used for generating a list of all the books, or for
* building the options for a form select.
*
* @return array
* An array of all books.
*/
public function getAllBooks();
/**
* Handles additions and updates to the book outline.
*
* This common helper function performs all additions and updates to the book
* outline through node addition, node editing, node deletion, or the outline
* tab.
*
* @param \Drupal\node\NodeInterface $node
* The node that is being saved, added, deleted, or moved.
*
* @return bool
* TRUE if the book link was saved; FALSE otherwise.
*/
public function updateOutline(NodeInterface $node);
/**
* Saves a link for a single book entry to the book.
*
* @param array $link
* The link data to save. $link['nid'] must be set. Other keys in this array
* get default values from
* \Drupal\book\BookManagerInterface::getLinkDefaults(). The array keys
* available to be set are documented in
* \Drupal\book\BookOutlineStorageInterface::loadMultiple().
* @param bool $new
* Whether this is a link to a new book entry.
*
* @return array
* The book entry link information. This is $link with values added or
* updated.
*
* @see \Drupal\book\BookManagerInterface::getLinkDefaults()
* @see \Drupal\book\BookOutlineStorageInterface::loadMultiple()
*/
public function saveBookLink(array $link, $new);
/**
* Returns an array with default values for a book page's menu link.
*
* @param string|int $nid
* The ID of the node whose menu link is being created.
*
* @return array
* The default values for the menu link.
*/
public function getLinkDefaults($nid);
public function getBookParents(array $item, array $parent = []);
/**
* Builds the common elements of the book form for the node and outline forms.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param \Drupal\node\NodeInterface $node
* The node whose form is being viewed.
* @param \Drupal\Core\Session\AccountInterface $account
* The account viewing the form.
* @param bool $collapsed
* If TRUE, the fieldset starts out collapsed.
*
* @return array
* The form structure, with the book elements added.
*/
public function addFormElements(array $form, FormStateInterface $form_state, NodeInterface $node, AccountInterface $account, $collapsed = TRUE);
/**
* Deletes node's entry from book table.
*
* @param int $nid
* The nid to delete.
*/
public function deleteFromBook($nid);
/**
* Returns a rendered menu tree.
*
* The menu item's LI element is given one of the following classes:
* - expanded: The menu item is showing its submenu.
* - collapsed: The menu item has a submenu which is not shown.
*
* @param array $tree
* A data structure representing the tree as returned from buildBookOutlineData.
*
* @return array
* A structured array to be rendered by
* \Drupal\Core\Render\RendererInterface::render().
*
* @see \Drupal\Core\Menu\MenuLinkTree::build
*/
public function bookTreeOutput(array $tree);
/**
* Checks access and performs dynamic operations for each link in the tree.
*
* @param array $tree
* The book tree you wish to operate on.
* @param array $node_links
* A collection of node link references generated from $tree by
* menu_tree_collect_node_links().
*/
public function bookTreeCheckAccess(&$tree, $node_links = []);
/**
* Gets the data representing a subtree of the book hierarchy.
*
* The root of the subtree will be the link passed as a parameter, so the
* returned tree will contain this item and all its descendants in the menu
* tree.
*
* @param array $link
* A fully loaded book link.
*
* @return array
* A subtree of book links in an array, in the order they should be rendered.
*/
public function bookSubtreeData($link);
/**
* Determines if a node can be removed from the book.
*
* A node can be removed from a book if it is actually in a book and it either
* is not a top-level page or is a top-level page with no children.
*
* @param \Drupal\node\NodeInterface $node
* The node to remove from the outline.
*
* @return bool
* TRUE if a node can be removed from the book, FALSE otherwise.
*/
public function checkNodeIsRemovable(NodeInterface $node);
}

View File

@@ -0,0 +1,132 @@
<?php
namespace Drupal\book;
/**
* Provides handling to render the book outline.
*/
class BookOutline {
/**
* The book manager.
*
* @var \Drupal\book\BookManagerInterface
*/
protected $bookManager;
/**
* Constructs a new BookOutline.
*
* @param \Drupal\book\BookManagerInterface $book_manager
* The book manager.
*/
public function __construct(BookManagerInterface $book_manager) {
$this->bookManager = $book_manager;
}
/**
* Fetches the book link for the previous page of the book.
*
* @param array $book_link
* A fully loaded book link that is part of the book hierarchy.
*
* @return array
* A fully loaded book link for the page before the one represented in
* $book_link.
*/
public function prevLink(array $book_link) {
// If the parent is zero, we are at the start of a book.
if ($book_link['pid'] == 0) {
return NULL;
}
$flat = $this->bookManager->bookTreeGetFlat($book_link);
reset($flat);
$curr = NULL;
do {
$prev = $curr;
$key = key($flat);
$curr = current($flat);
next($flat);
} while ($key && $key != $book_link['nid']);
if ($key == $book_link['nid']) {
// The previous page in the book may be a child of the previous visible link.
if ($prev['depth'] == $book_link['depth']) {
// The subtree will have only one link at the top level - get its data.
$tree = $this->bookManager->bookSubtreeData($prev);
$data = array_shift($tree);
// The link of interest is the last child - iterate to find the deepest one.
while ($data['below']) {
$data = end($data['below']);
}
$this->bookManager->bookLinkTranslate($data['link']);
return $data['link'];
}
else {
$this->bookManager->bookLinkTranslate($prev);
return $prev;
}
}
}
/**
* Fetches the book link for the next page of the book.
*
* @param array $book_link
* A fully loaded book link that is part of the book hierarchy.
*
* @return array
* A fully loaded book link for the page after the one represented in
* $book_link.
*/
public function nextLink(array $book_link) {
$flat = $this->bookManager->bookTreeGetFlat($book_link);
reset($flat);
do {
$key = key($flat);
next($flat);
} while ($key && $key != $book_link['nid']);
if ($key == $book_link['nid']) {
$next = current($flat);
if ($next) {
$this->bookManager->bookLinkTranslate($next);
}
return $next;
}
}
/**
* Formats the book links for the child pages of the current page.
*
* @param array $book_link
* A fully loaded book link that is part of the book hierarchy.
*
* @return array
* HTML for the links to the child pages of the current page.
*/
public function childrenLinks(array $book_link) {
$flat = $this->bookManager->bookTreeGetFlat($book_link);
$children = [];
if ($book_link['has_children']) {
// Walk through the array until we find the current page.
do {
$link = array_shift($flat);
} while ($link && ($link['nid'] != $book_link['nid']));
// Continue though the array and collect the links whose parent is this page.
while (($link = array_shift($flat)) && $link['pid'] == $book_link['nid']) {
$data['link'] = $link;
$data['below'] = '';
$children[] = $data;
}
}
if ($children) {
return $this->bookManager->bookTreeOutput($children);
}
return '';
}
}

View File

@@ -0,0 +1,201 @@
<?php
namespace Drupal\book;
use Drupal\Core\Database\Connection;
/**
* Defines a storage class for books outline.
*/
class BookOutlineStorage implements BookOutlineStorageInterface {
/**
* Database Service Object.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* Constructs a BookOutlineStorage object.
*/
public function __construct(Connection $connection) {
$this->connection = $connection;
}
/**
* {@inheritdoc}
*/
public function getBooks() {
return $this->connection->query("SELECT DISTINCT([bid]) FROM {book}")->fetchCol();
}
/**
* {@inheritdoc}
*/
public function hasBooks() {
return (bool) $this->connection
->query('SELECT count([bid]) FROM {book}')
->fetchField();
}
/**
* {@inheritdoc}
*/
public function loadMultiple($nids, $access = TRUE) {
$query = $this->connection->select('book', 'b', ['fetch' => \PDO::FETCH_ASSOC]);
$query->fields('b');
$query->condition('b.nid', $nids, 'IN');
if ($access) {
$query->addTag('node_access');
$query->addMetaData('base_table', 'book');
}
return $query->execute();
}
/**
* {@inheritdoc}
*/
public function getChildRelativeDepth($book_link, $max_depth) {
$query = $this->connection->select('book');
$query->addField('book', 'depth');
$query->condition('bid', $book_link['bid']);
$query->orderBy('depth', 'DESC');
$query->range(0, 1);
$i = 1;
$p = 'p1';
while ($i <= $max_depth && $book_link[$p]) {
$query->condition($p, $book_link[$p]);
$p = 'p' . ++$i;
}
return $query->execute()->fetchField();
}
/**
* {@inheritdoc}
*/
public function delete($nid) {
return $this->connection->delete('book')
->condition('nid', $nid)
->execute();
}
/**
* {@inheritdoc}
*/
public function loadBookChildren($pid) {
return $this->connection
->query("SELECT * FROM {book} WHERE [pid] = :pid", [':pid' => $pid])
->fetchAllAssoc('nid', \PDO::FETCH_ASSOC);
}
/**
* {@inheritdoc}
*/
public function getBookMenuTree($bid, $parameters, $min_depth, $max_depth) {
$query = $this->connection->select('book');
$query->fields('book');
for ($i = 1; $i <= $max_depth; $i++) {
$query->orderBy('p' . $i, 'ASC');
}
$query->condition('bid', $bid);
if (!empty($parameters['expanded'])) {
$query->condition('pid', $parameters['expanded'], 'IN');
}
if ($min_depth != 1) {
$query->condition('depth', $min_depth, '>=');
}
if (isset($parameters['max_depth'])) {
$query->condition('depth', $parameters['max_depth'], '<=');
}
// Add custom query conditions, if any were passed.
if (isset($parameters['conditions'])) {
foreach ($parameters['conditions'] as $column => $value) {
$query->condition($column, $value);
}
}
return $query->execute();
}
/**
* {@inheritdoc}
*/
public function insert($link, $parents) {
return $this->connection
->insert('book')
->fields([
'nid' => $link['nid'],
'bid' => $link['bid'],
'pid' => $link['pid'],
'weight' => $link['weight'],
] + $parents)
->execute();
}
/**
* {@inheritdoc}
*/
public function update($nid, $fields) {
return $this->connection
->update('book')
->fields($fields)
->condition('nid', $nid)
->execute();
}
/**
* {@inheritdoc}
*/
public function updateMovedChildren($bid, $original, $expressions, $shift) {
$query = $this->connection->update('book');
$query->fields(['bid' => $bid]);
foreach ($expressions as $expression) {
$query->expression($expression[0], $expression[1], $expression[2]);
}
$query->expression('depth', '[depth] + :depth', [':depth' => $shift]);
$query->condition('bid', $original['bid']);
$p = 'p1';
for ($i = 1; !empty($original[$p]); $p = 'p' . ++$i) {
$query->condition($p, $original[$p]);
}
return $query->execute();
}
/**
* {@inheritdoc}
*/
public function countOriginalLinkChildren($original) {
return $this->connection->select('book', 'b')
->condition('bid', $original['bid'])
->condition('pid', $original['pid'])
->condition('nid', $original['nid'], '<>')
->countQuery()
->execute()->fetchField();
}
/**
* {@inheritdoc}
*/
public function getBookSubtree($link, $max_depth) {
$query = $this->connection->select('book', 'b', ['fetch' => \PDO::FETCH_ASSOC]);
$query->fields('b');
$query->condition('b.bid', $link['bid']);
for ($i = 1; $i <= $max_depth && $link["p$i"]; ++$i) {
$query->condition("p$i", $link["p$i"]);
}
for ($i = 1; $i <= $max_depth; ++$i) {
$query->orderBy("p$i");
}
return $query->execute();
}
}

View File

@@ -0,0 +1,168 @@
<?php
namespace Drupal\book;
/**
* Defines a common interface for book outline storage classes.
*/
interface BookOutlineStorageInterface {
/**
* Gets books (the highest positioned book links).
*
* @return array
* An array of book IDs.
*/
public function getBooks();
/**
* Checks if there are any books.
*
* @return bool
* TRUE if there are books, FALSE if not.
*/
public function hasBooks();
/**
* Loads books.
*
* Each book entry consists of the following keys:
* - bid: The node ID of the main book.
* - nid: The node ID of the book entry itself.
* - pid: The parent node ID of the book.
* - has_children: A boolean to indicate whether the book has children.
* - weight: The weight of the book entry to order siblings.
* - depth: The depth in the menu hierarchy the entry is placed into.
*
* @param array $nids
* An array of node IDs.
* @param bool $access
* Whether access checking should be taken into account.
*
* @return array
* Array of loaded book items.
*/
public function loadMultiple($nids, $access = TRUE);
/**
* Gets child relative depth.
*
* @param array $book_link
* The book link.
* @param int $max_depth
* The maximum supported depth of the book tree.
*
* @return int
* The depth of the searched book.
*/
public function getChildRelativeDepth($book_link, $max_depth);
/**
* Deletes a book entry.
*
* @param int $nid
* Deletes a book entry.
*
* @return mixed
* Number of deleted book entries.
*/
public function delete($nid);
/**
* Loads book's children using its parent ID.
*
* @param int $pid
* The book's parent ID.
*
* @return array
* Array of loaded book items.
*/
public function loadBookChildren($pid);
/**
* Builds tree data used for the menu tree.
*
* @param int $bid
* The ID of the book that we are building the tree for.
* @param array $parameters
* An associative array of build parameters. For info about individual
* parameters see BookManager::bookTreeBuild().
* @param int $min_depth
* The minimum depth of book links in the resulting tree.
* @param int $max_depth
* The maximum supported depth of the book tree.
*
* @return array
* Array of loaded book links.
*/
public function getBookMenuTree($bid, $parameters, $min_depth, $max_depth);
/**
* Inserts a book link.
*
* @param array $link
* The link array to be inserted in the database.
* @param array $parents
* The array of parent ids for the link to be inserted.
*
* @return mixed
* The last insert ID of the query, if one exists.
*/
public function insert($link, $parents);
/**
* Updates book reference for links that were moved between books.
*
* @param int $nid
* The nid of the book entry to be updated.
* @param array $fields
* The array of fields to be updated.
*
* @return mixed
* The number of rows matched by the update query.
*/
public function update($nid, $fields);
/**
* Update the book ID of the book link that it's being moved.
*
* @param int $bid
* The ID of the book whose children we move.
* @param array $original
* The original parent of the book link.
* @param array $expressions
* Array of expressions to be added to the query.
* @param int $shift
* The difference in depth between the old and the new position of the
* element being moved.
*
* @return mixed
* The number of rows matched by the update query.
*/
public function updateMovedChildren($bid, $original, $expressions, $shift);
/**
* Count the number of original link children.
*
* @param array $original
* The book link array.
*
* @return int
* Number of children.
*/
public function countOriginalLinkChildren($original);
/**
* Get book subtree.
*
* @param array $link
* A fully loaded book link.
* @param int $max_depth
* The maximum supported depth of the book tree.
*
* @return array
* Array of unordered subtree book items.
*/
public function getBookSubtree($link, $max_depth);
}

View File

@@ -0,0 +1,95 @@
<?php
namespace Drupal\book;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleUninstallValidatorInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
/**
* Prevents book module from being uninstalled under certain conditions.
*
* These conditions are when any book nodes exist or there are any book outline
* stored.
*/
class BookUninstallValidator implements ModuleUninstallValidatorInterface {
use StringTranslationTrait;
/**
* The book outline storage.
*
* @var \Drupal\book\BookOutlineStorageInterface
*/
protected $bookOutlineStorage;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a new BookUninstallValidator.
*
* @param \Drupal\book\BookOutlineStorageInterface $book_outline_storage
* The book outline storage.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The string translation service.
*/
public function __construct(BookOutlineStorageInterface $book_outline_storage, EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation) {
$this->bookOutlineStorage = $book_outline_storage;
$this->entityTypeManager = $entity_type_manager;
$this->stringTranslation = $string_translation;
}
/**
* {@inheritdoc}
*/
public function validate($module) {
$reasons = [];
if ($module == 'book') {
if ($this->hasBookOutlines()) {
$reasons[] = $this->t('To uninstall Book, delete all content that is part of a book');
}
else {
// The book node type is provided by the Book module. Prevent uninstall
// if there are any nodes of that type.
if ($this->hasBookNodes()) {
$reasons[] = $this->t('To uninstall Book, delete all content that has the Book content type');
}
}
}
return $reasons;
}
/**
* Checks if there are any books in an outline.
*
* @return bool
* TRUE if there are books, FALSE if not.
*/
protected function hasBookOutlines() {
return $this->bookOutlineStorage->hasBooks();
}
/**
* Determines if there is any book nodes or not.
*
* @return bool
* TRUE if there are book nodes, FALSE otherwise.
*/
protected function hasBookNodes() {
$nodes = $this->entityTypeManager->getStorage('node')->getQuery()
->condition('type', 'book')
->accessCheck(FALSE)
->range(0, 1)
->execute();
return !empty($nodes);
}
}

View File

@@ -0,0 +1,95 @@
<?php
namespace Drupal\book\Cache;
use Drupal\book\BookManagerInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\Context\CacheContextInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\NodeInterface;
/**
* Defines the book navigation cache context service.
*
* Cache context ID: 'route.book_navigation'.
*
* This allows for book navigation location-aware caching. It depends on:
* - whether the current route represents a book node at all
* - and if so, where in the book hierarchy we are
*/
class BookNavigationCacheContext implements CacheContextInterface {
/**
* The current route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* Constructs a new BookNavigationCacheContext service.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
* @param \Drupal\book\BookManagerInterface|null $bookManagerService
* The book manager service.
*/
public function __construct(RouteMatchInterface $route_match, public ?BookManagerInterface $bookManagerService = NULL) {
$this->routeMatch = $route_match;
if ($this->bookManagerService === NULL) {
@trigger_error('Calling ' . __METHOD__ . ' without the $bookManagerService argument is deprecated in drupal:10.2.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3397515', E_USER_DEPRECATED);
$this->bookManagerService = \Drupal::service('book.manager');
}
}
/**
* {@inheritdoc}
*/
public static function getLabel() {
return t("Book navigation");
}
/**
* {@inheritdoc}
*/
public function getContext() {
// Find the current book's ID.
$current_bid = 0;
$node = $this->routeMatch->getParameter('node');
if ($node instanceof NodeInterface && !empty($node->book['bid'])) {
$current_bid = $node->book['bid'];
}
// If we're not looking at a book node, then we're not navigating a book.
if ($current_bid === 0) {
return 'book.none';
}
// If we're looking at a book node, get the trail for that node.
$active_trail = $this->bookManagerService
->getActiveTrailIds($node->book['bid'], $node->book);
return implode('|', $active_trail);
}
/**
* {@inheritdoc}
*/
public function getCacheableMetadata() {
// The book active trail depends on the node and data attached to it.
// That information is however not stored as part of the node.
$cacheable_metadata = new CacheableMetadata();
$node = $this->routeMatch->getParameter('node');
if ($node instanceof NodeInterface) {
// If the node is part of a book then we can use the cache tag for that
// book. If not, then it can't be optimized away.
if (!empty($node->book['bid'])) {
$cacheable_metadata->addCacheTags(['bid:' . $node->book['bid']]);
}
else {
$cacheable_metadata->setCacheMaxAge(0);
}
}
return $cacheable_metadata;
}
}

View File

@@ -0,0 +1,154 @@
<?php
namespace Drupal\book\Controller;
use Drupal\book\BookExport;
use Drupal\book\BookManagerInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Link;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Controller routines for book routes.
*/
class BookController extends ControllerBase {
/**
* The book manager.
*
* @var \Drupal\book\BookManagerInterface
*/
protected $bookManager;
/**
* The book export service.
*
* @var \Drupal\book\BookExport
*/
protected $bookExport;
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Constructs a BookController object.
*
* @param \Drupal\book\BookManagerInterface $bookManager
* The book manager.
* @param \Drupal\book\BookExport $bookExport
* The book export service.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
*/
public function __construct(BookManagerInterface $bookManager, BookExport $bookExport, RendererInterface $renderer) {
$this->bookManager = $bookManager;
$this->bookExport = $bookExport;
$this->renderer = $renderer;
}
/**
* Returns an administrative overview of all books.
*
* @return array
* A render array representing the administrative page content.
*/
public function adminOverview() {
$rows = [];
$headers = [t('Book'), t('Operations')];
// Add any recognized books to the table list.
foreach ($this->bookManager->getAllBooks() as $book) {
/** @var \Drupal\Core\Url $url */
$url = $book['url'];
if (isset($book['options'])) {
$url->setOptions($book['options']);
}
$row = [
Link::fromTextAndUrl($book['title'], $url),
];
$links = [];
$links['edit'] = [
'title' => t('Edit order and titles'),
'url' => Url::fromRoute('book.admin_edit', ['node' => $book['nid']]),
];
$row[] = [
'data' => [
'#type' => 'operations',
'#links' => $links,
],
];
$rows[] = $row;
}
return [
'#type' => 'table',
'#header' => $headers,
'#rows' => $rows,
'#empty' => t('No books available.'),
];
}
/**
* Prints a listing of all books.
*
* @return array
* A render array representing the listing of all books content.
*/
public function bookRender() {
$book_list = [];
foreach ($this->bookManager->getAllBooks() as $book) {
$book_list[] = Link::fromTextAndUrl($book['title'], $book['url']);
}
return [
'#theme' => 'item_list',
'#items' => $book_list,
'#cache' => [
'tags' => $this->entityTypeManager()->getDefinition('node')->getListCacheTags(),
],
];
}
/**
* Generates representations of a book page and its children.
*
* The method delegates the generation of output to helper methods. The method
* name is derived by prepending 'bookExport' to the camelized form of given
* output type. For example, a type of 'html' results in a call to the method
* bookExportHtml().
*
* @param string $type
* A string encoding the type of output requested. The following types are
* currently supported in book module:
* - html: Printer-friendly HTML.
* Other types may be supported in contributed modules.
* @param \Drupal\node\NodeInterface $node
* The node to export.
*
* @return array
* A render array representing the node and its children in the book
* hierarchy in a format determined by the $type parameter.
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function bookExport($type, NodeInterface $node) {
$method = 'bookExport' . Container::camelize($type);
// @todo Convert the custom export functionality to serializer.
if (!method_exists($this->bookExport, $method)) {
$this->messenger()->addStatus(t('Unknown export format.'));
throw new NotFoundHttpException();
}
$exported_book = $this->bookExport->{$method}($node);
return new Response($this->renderer->renderRoot($exported_book));
}
}

View File

@@ -0,0 +1,310 @@
<?php
namespace Drupal\book\Form;
use Drupal\book\BookManager;
use Drupal\book\BookManagerInterface;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for administering a single book's hierarchy.
*
* @internal
*/
class BookAdminEditForm extends FormBase {
/**
* The node storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $nodeStorage;
/**
* The book manager.
*
* @var \Drupal\book\BookManagerInterface
*/
protected $bookManager;
/**
* The entity repository service.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* Constructs a new BookAdminEditForm.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $node_storage
* The content block storage.
* @param \Drupal\book\BookManagerInterface $book_manager
* The book manager.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository service.
*/
public function __construct(EntityStorageInterface $node_storage, BookManagerInterface $book_manager, EntityRepositoryInterface $entity_repository) {
$this->nodeStorage = $node_storage;
$this->bookManager = $book_manager;
$this->entityRepository = $entity_repository;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$entity_type_manager = $container->get('entity_type.manager');
return new static(
$entity_type_manager->getStorage('node'),
$container->get('book.manager'),
$container->get('entity.repository')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'book_admin_edit';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, ?NodeInterface $node = NULL) {
$form['#title'] = $node->label();
$form['#node'] = $node;
$this->bookAdminTable($node, $form);
$form['save'] = [
'#type' => 'submit',
'#value' => $this->t('Save book pages'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($form_state->getValue('tree_hash') != $form_state->getValue('tree_current_hash')) {
$form_state->setErrorByName('', $this->t('This book has been modified by another user, the changes could not be saved.'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Save elements in the same order as defined in post rather than the form.
// This ensures parents are updated before their children, preventing orphans.
$user_input = $form_state->getUserInput();
if (isset($user_input['table'])) {
$order = array_flip(array_keys($user_input['table']));
$form['table'] = array_merge($order, $form['table']);
foreach (Element::children($form['table']) as $key) {
if ($form['table'][$key]['#item']) {
$row = $form['table'][$key];
$values = $form_state->getValue(['table', $key]);
// Update menu item if moved.
if ($row['parent']['pid']['#default_value'] != $values['pid'] || $row['weight']['#default_value'] != $values['weight']) {
$link = $this->bookManager->loadBookLink($values['nid'], FALSE);
$link['weight'] = $values['weight'];
$link['pid'] = $values['pid'];
$this->bookManager->saveBookLink($link, FALSE);
}
// Update the title if changed.
if ($row['title']['#default_value'] != $values['title']) {
$node = $this->nodeStorage->load($values['nid']);
$node = $this->entityRepository->getTranslationFromContext($node);
$node->revision_log = $this->t('Title changed from %original to %current.', ['%original' => $node->label(), '%current' => $values['title']]);
$node->title = $values['title'];
$node->book['link_title'] = $values['title'];
$node->setNewRevision();
$node->save();
$this->logger('content')->info('book: updated %title.', ['%title' => $node->label(), 'link' => $node->toLink($this->t('View'))->toString()]);
}
}
}
}
$this->messenger()->addStatus($this->t('Updated book %title.', ['%title' => $form['#node']->label()]));
}
/**
* Builds the table portion of the form for the book administration page.
*
* @param \Drupal\node\NodeInterface $node
* The node of the top-level page in the book.
* @param array $form
* The form that is being modified, passed by reference.
*
* @see self::buildForm()
*/
protected function bookAdminTable(NodeInterface $node, array &$form) {
$form['table'] = [
'#type' => 'table',
'#header' => [
$this->t('Title'),
$this->t('Weight'),
$this->t('Parent'),
$this->t('Operations'),
],
'#empty' => $this->t('No book content available.'),
'#tabledrag' => [
[
'action' => 'match',
'relationship' => 'parent',
'group' => 'book-pid',
'subgroup' => 'book-pid',
'source' => 'book-nid',
'hidden' => TRUE,
'limit' => BookManager::BOOK_MAX_DEPTH - 2,
],
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'book-weight',
],
],
];
$tree = $this->bookManager->bookSubtreeData($node->book);
// Do not include the book item itself.
$tree = array_shift($tree);
if ($tree['below']) {
$hash = Crypt::hashBase64(serialize($tree['below']));
// Store the hash value as a hidden form element so that we can detect
// if another user changed the book hierarchy.
$form['tree_hash'] = [
'#type' => 'hidden',
'#default_value' => $hash,
];
$form['tree_current_hash'] = [
'#type' => 'value',
'#value' => $hash,
];
$this->bookAdminTableTree($tree['below'], $form['table']);
}
}
/**
* Helps build the main table in the book administration page form.
*
* @param array $tree
* A subtree of the book menu hierarchy.
* @param array $form
* The form that is being modified, passed by reference.
*
* @see self::buildForm()
*/
protected function bookAdminTableTree(array $tree, array &$form) {
// The delta must be big enough to give each node a distinct value.
$count = count($tree);
$delta = ($count < 30) ? 15 : intval($count / 2) + 1;
$access = \Drupal::currentUser()->hasPermission('administer nodes');
$destination = $this->getDestinationArray();
foreach ($tree as $data) {
$nid = $data['link']['nid'];
$id = 'book-admin-' . $nid;
$form[$id]['#item'] = $data['link'];
$form[$id]['#nid'] = $nid;
$form[$id]['#attributes']['class'][] = 'draggable';
$form[$id]['#weight'] = $data['link']['weight'];
if (isset($data['link']['depth']) && $data['link']['depth'] > 2) {
$indentation = [
'#theme' => 'indentation',
'#size' => $data['link']['depth'] - 2,
];
}
$form[$id]['title'] = [
'#prefix' => !empty($indentation) ? \Drupal::service('renderer')->render($indentation) : '',
'#type' => 'textfield',
'#default_value' => $data['link']['title'],
'#maxlength' => 255,
'#size' => 40,
];
$form[$id]['weight'] = [
'#type' => 'weight',
'#default_value' => $data['link']['weight'],
'#delta' => max($delta, abs($data['link']['weight'])),
'#title' => $this->t('Weight for @title', ['@title' => $data['link']['title']]),
'#title_display' => 'invisible',
'#attributes' => [
'class' => ['book-weight'],
],
];
$form[$id]['parent']['nid'] = [
'#parents' => ['table', $id, 'nid'],
'#type' => 'hidden',
'#value' => $nid,
'#attributes' => [
'class' => ['book-nid'],
],
];
$form[$id]['parent']['pid'] = [
'#parents' => ['table', $id, 'pid'],
'#type' => 'hidden',
'#default_value' => $data['link']['pid'],
'#attributes' => [
'class' => ['book-pid'],
],
];
$form[$id]['parent']['bid'] = [
'#parents' => ['table', $id, 'bid'],
'#type' => 'hidden',
'#default_value' => $data['link']['bid'],
'#attributes' => [
'class' => ['book-bid'],
],
];
$form[$id]['operations'] = [
'#type' => 'operations',
];
$form[$id]['operations']['#links']['view'] = [
'title' => $this->t('View'),
'url' => new Url('entity.node.canonical', ['node' => $nid]),
];
if ($access) {
$form[$id]['operations']['#links']['edit'] = [
'title' => $this->t('Edit'),
'url' => new Url('entity.node.edit_form', ['node' => $nid]),
'query' => $destination,
];
$form[$id]['operations']['#links']['delete'] = [
'title' => $this->t('Delete'),
'url' => new Url('entity.node.delete_form', ['node' => $nid]),
'query' => $destination,
];
}
if ($data['below']) {
$this->bookAdminTableTree($data['below'], $form);
}
}
}
}

View File

@@ -0,0 +1,136 @@
<?php
namespace Drupal\book\Form;
use Drupal\book\BookManagerInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Displays the book outline form.
*
* @internal
*/
class BookOutlineForm extends ContentEntityForm {
/**
* The book being displayed.
*
* @var \Drupal\node\NodeInterface
*/
protected $entity;
/**
* BookManager service.
*
* @var \Drupal\book\BookManagerInterface
*/
protected $bookManager;
/**
* Constructs a BookOutlineForm object.
*
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
* @param \Drupal\book\BookManagerInterface $book_manager
* The BookManager service.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle service.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
*/
public function __construct(EntityRepositoryInterface $entity_repository, BookManagerInterface $book_manager, ?EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, ?TimeInterface $time = NULL) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->bookManager = $book_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.repository'),
$container->get('book.manager'),
$container->get('entity_type.bundle.info'),
$container->get('datetime.time')
);
}
/**
* {@inheritdoc}
*/
public function getBaseFormId() {
return NULL;
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form['#title'] = $this->entity->label();
if (!isset($this->entity->book)) {
// The node is not part of any book yet - set default options.
$this->entity->book = $this->bookManager->getLinkDefaults($this->entity->id());
}
else {
$this->entity->book['original_bid'] = $this->entity->book['bid'];
}
// Find the depth limit for the parent select.
if (!isset($this->entity->book['parent_depth_limit'])) {
$this->entity->book['parent_depth_limit'] = $this->bookManager->getParentDepthLimit($this->entity->book);
}
$form = $this->bookManager->addFormElements($form, $form_state, $this->entity, $this->currentUser(), FALSE);
return $form;
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this->entity->book['original_bid'] ? $this->t('Update book outline') : $this->t('Add to book outline');
$actions['delete']['#title'] = $this->t('Remove from book outline');
$actions['delete']['#url'] = new Url('entity.node.book_remove_form', ['node' => $this->entity->book['nid']]);
$actions['delete']['#access'] = $this->bookManager->checkNodeIsRemovable($this->entity);
return $actions;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$form_state->setRedirect(
'entity.node.canonical',
['node' => $this->entity->id()]
);
$book_link = $form_state->getValue('book');
if (!$book_link['bid']) {
$this->messenger()->addStatus($this->t('No changes were made'));
return;
}
$this->entity->book = $book_link;
if ($this->bookManager->updateOutline($this->entity)) {
if (isset($this->entity->book['parent_mismatch']) && $this->entity->book['parent_mismatch']) {
// This will usually only happen when JS is disabled.
$this->messenger()->addStatus($this->t('The post has been added to the selected book. You may now position it relative to other pages.'));
$form_state->setRedirectUrl($this->entity->toUrl('book-outline-form'));
}
else {
$this->messenger()->addStatus($this->t('The book outline has been updated.'));
}
}
else {
$this->messenger()->addError($this->t('There was an error adding the post to the book.'));
}
}
}

View File

@@ -0,0 +1,111 @@
<?php
namespace Drupal\book\Form;
use Drupal\book\BookManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Remove form for book module.
*
* @internal
*/
class BookRemoveForm extends ConfirmFormBase {
/**
* The book manager.
*
* @var \Drupal\book\BookManagerInterface
*/
protected $bookManager;
/**
* The node representing the book.
*
* @var \Drupal\node\NodeInterface
*/
protected $node;
/**
* Constructs a BookRemoveForm object.
*
* @param \Drupal\book\BookManagerInterface $book_manager
* The book manager.
*/
public function __construct(BookManagerInterface $book_manager) {
$this->bookManager = $book_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('book.manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'book_remove_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, ?NodeInterface $node = NULL) {
$this->node = $node;
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function getDescription() {
$title = ['%title' => $this->node->label()];
if ($this->node->book['has_children']) {
return $this->t('%title has associated child pages, which will be relocated automatically to maintain their connection to the book. To recreate the hierarchy (as it was before removing this page), %title may be added again using the Outline tab, and each of its former child pages will need to be relocated manually.', $title);
}
else {
return $this->t('%title may be added to hierarchy again using the Outline tab.', $title);
}
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Remove');
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to remove %title from the book hierarchy?', ['%title' => $this->node->label()]);
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return $this->node->toUrl();
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($this->bookManager->checkNodeIsRemovable($this->node)) {
$this->bookManager->deleteFromBook($this->node->id());
$this->messenger()->addStatus($this->t('The post has been removed from the book.'));
}
$form_state->setRedirectUrl($this->getCancelUrl());
}
}

View File

@@ -0,0 +1,84 @@
<?php
namespace Drupal\book\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\ConfigTarget;
use Drupal\Core\Form\FormStateInterface;
/**
* Configure book settings for this site.
*
* @internal
*/
class BookSettingsForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'book_admin_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['book.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$types = node_type_get_names();
$form['book_allowed_types'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Content types allowed in book outlines'),
'#config_target' => new ConfigTarget('book.settings', 'allowed_types', toConfig: static::class . '::filterAndSortAllowedTypes'),
'#options' => $types,
'#description' => $this->t('Users with the %outline-perm permission can add all content types.', ['%outline-perm' => $this->t('Administer book outlines')]),
'#required' => TRUE,
];
$form['book_child_type'] = [
'#type' => 'radios',
'#title' => $this->t('Content type for the <em>Add child page</em> link'),
'#config_target' => 'book.settings:child_type',
'#options' => $types,
'#required' => TRUE,
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$child_type = $form_state->getValue('book_child_type');
if ($form_state->isValueEmpty(['book_allowed_types', $child_type])) {
$form_state->setErrorByName('book_child_type', $this->t('The content type for the %add-child link must be one of those selected as an allowed book outline type.', ['%add-child' => $this->t('Add child page')]));
}
parent::validateForm($form, $form_state);
}
/**
* Transformation callback for the book_allowed_types config value.
*
* @param array $allowed_types
* The config value to transform.
*
* @return array
* The transformed value.
*/
public static function filterAndSortAllowedTypes(array $allowed_types): array {
$allowed_types = array_filter($allowed_types);
// We need to save the allowed types in an array ordered by machine_name so
// that we can save them in the correct order if node type changes.
// @see book_node_type_update().
sort($allowed_types);
return $allowed_types;
}
}

View File

@@ -0,0 +1,200 @@
<?php
namespace Drupal\book\Plugin\Block;
use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\book\BookManagerInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityStorageInterface;
/**
* Provides a 'Book navigation' block.
*/
#[Block(
id: "book_navigation",
admin_label: new TranslatableMarkup("Book navigation"),
category: new TranslatableMarkup("Menus")
)]
class BookNavigationBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* The current route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* The book manager.
*
* @var \Drupal\book\BookManagerInterface
*/
protected $bookManager;
/**
* The node storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $nodeStorage;
/**
* Constructs a new BookNavigationBlock instance.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
* @param \Drupal\book\BookManagerInterface $book_manager
* The book manager.
* @param \Drupal\Core\Entity\EntityStorageInterface $node_storage
* The node storage.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match, BookManagerInterface $book_manager, EntityStorageInterface $node_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->routeMatch = $route_match;
$this->bookManager = $book_manager;
$this->nodeStorage = $node_storage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('current_route_match'),
$container->get('book.manager'),
$container->get('entity_type.manager')->getStorage('node')
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'block_mode' => "all pages",
];
}
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$options = [
'all pages' => $this->t('Show block on all pages'),
'book pages' => $this->t('Show block only on book pages'),
];
$form['book_block_mode'] = [
'#type' => 'radios',
'#title' => $this->t('Book navigation block display'),
'#options' => $options,
'#default_value' => $this->configuration['block_mode'],
'#description' => $this->t("If <em>Show block on all pages</em> is selected, the block will contain the automatically generated menus for all of the site's books. If <em>Show block only on book pages</em> is selected, the block will contain only the one menu corresponding to the current page's book. In this case, if the current page is not in a book, no block will be displayed. The <em>Page specific visibility settings</em> or other visibility settings can be used in addition to selectively display this block."),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['block_mode'] = $form_state->getValue('book_block_mode');
}
/**
* {@inheritdoc}
*/
public function build() {
$current_bid = 0;
$node = $this->routeMatch->getParameter('node');
if ($node instanceof NodeInterface && !empty($node->book['bid'])) {
$current_bid = $node->book['bid'];
}
if ($this->configuration['block_mode'] == 'all pages') {
$book_menus = [];
$pseudo_tree = [0 => ['below' => FALSE]];
foreach ($this->bookManager->getAllBooks() as $book_id => $book) {
if ($book['bid'] == $current_bid) {
// If the current page is a node associated with a book, the menu
// needs to be retrieved.
$data = $this->bookManager->bookTreeAllData($node->book['bid'], $node->book);
$book_menus[$book_id] = $this->bookManager->bookTreeOutput($data);
}
else {
// Since we know we will only display a link to the top node, there
// is no reason to run an additional menu tree query for each book.
$book['in_active_trail'] = FALSE;
// Check whether user can access the book link.
$book_node = $this->nodeStorage->load($book['nid']);
$book['access'] = $book_node->access('view');
$pseudo_tree[0]['link'] = $book;
$book_menus[$book_id] = $this->bookManager->bookTreeOutput($pseudo_tree);
}
$book_menus[$book_id] += [
'#book_title' => $book['title'],
];
}
if ($book_menus) {
return [
'#theme' => 'book_all_books_block',
] + $book_menus;
}
}
elseif ($current_bid) {
// Only display this block when the user is browsing a book and do
// not show unpublished books.
$nid = \Drupal::entityQuery('node')
->accessCheck(TRUE)
->condition('nid', $node->book['bid'], '=')
->condition('status', NodeInterface::PUBLISHED)
->execute();
// Only show the block if the user has view access for the top-level node.
if ($nid) {
$tree = $this->bookManager->bookTreeAllData($node->book['bid'], $node->book);
// There should only be one element at the top level.
$data = array_shift($tree);
$below = $this->bookManager->bookTreeOutput($data['below']);
if (!empty($below)) {
return $below;
}
}
}
return [];
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
return Cache::mergeContexts(parent::getCacheContexts(), ['route.book_navigation']);
}
/**
* {@inheritdoc}
*
* @todo Make cacheable in https://www.drupal.org/node/2483181
*/
public function getCacheMaxAge() {
return 0;
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Drupal\book\Plugin\Validation\Constraint;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Validation\Attribute\Constraint;
use Symfony\Component\Validator\Constraint as SymfonyConstraint;
/**
* Validation constraint for changing the book outline in pending revisions.
*/
#[Constraint(
id: 'BookOutline',
label: new TranslatableMarkup('Book outline.', [], ['context' => 'Validation'])
)]
class BookOutlineConstraint extends SymfonyConstraint {
public $message = 'You can only change the book outline for the <em>published</em> version of this content.';
}

View File

@@ -0,0 +1,77 @@
<?php
namespace Drupal\book\Plugin\Validation\Constraint;
use Drupal\book\BookManagerInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Constraint validator for changing the book outline in pending revisions.
*/
class BookOutlineConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
/**
* The book manager.
*
* @var \Drupal\book\BookManagerInterface
*/
protected $bookManager;
/**
* Creates a new BookOutlineConstraintValidator instance.
*
* @param \Drupal\book\BookManagerInterface $book_manager
* The book manager.
*/
public function __construct(BookManagerInterface $book_manager) {
$this->bookManager = $book_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('book.manager')
);
}
/**
* {@inheritdoc}
*/
public function validate($entity, Constraint $constraint) {
if (isset($entity) && !$entity->isNew() && !$entity->isDefaultRevision()) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $original */
$original = $this->bookManager->loadBookLink($entity->id(), FALSE) ?: [
'bid' => 0,
'weight' => 0,
];
if (empty($original['pid'])) {
$original['pid'] = -1;
}
if ($entity->book['bid'] != $original['bid']) {
$this->context->buildViolation($constraint->message)
->atPath('book.bid')
->setInvalidValue($entity)
->addViolation();
}
if ($entity->book['pid'] != $original['pid']) {
$this->context->buildViolation($constraint->message)
->atPath('book.pid')
->setInvalidValue($entity)
->addViolation();
}
if ($entity->book['weight'] != $original['weight']) {
$this->context->buildViolation($constraint->message)
->atPath('book.weight')
->setInvalidValue($entity)
->addViolation();
}
}
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace Drupal\book\Plugin\migrate\destination;
use Drupal\Core\Entity\EntityInterface;
use Drupal\migrate\Attribute\MigrateDestination;
use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
use Drupal\migrate\Row;
/**
* Provides migrate destination plugin for Book content.
*/
#[MigrateDestination('book')]
class Book extends EntityContentBase {
/**
* {@inheritdoc}
*/
protected static function getEntityTypeId($plugin_id) {
return 'node';
}
/**
* {@inheritdoc}
*/
protected function updateEntity(EntityInterface $entity, Row $row) {
if ($entity->book) {
$book = $row->getDestinationProperty('book');
foreach ($book as $key => $value) {
$entity->book[$key] = $value;
}
}
else {
$entity->book = $row->getDestinationProperty('book');
}
return parent::updateEntity($entity, $row);
}
}

View File

@@ -0,0 +1,70 @@
<?php
namespace Drupal\book\Plugin\migrate\source;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
// cspell:ignore mlid plid
/**
* Drupal 6/7 book source from database.
*
* For available configuration keys, refer to the parent classes.
*
* @see \Drupal\migrate\Plugin\migrate\source\SqlBase
* @see \Drupal\migrate\Plugin\migrate\source\SourcePluginBase
*
* @MigrateSource(
* id = "book",
* source_module = "book",
* )
*/
class Book extends DrupalSqlBase {
/**
* {@inheritdoc}
*/
public function query() {
$query = $this->select('book', 'b')->fields('b', ['nid', 'bid']);
$query->join('menu_links', 'ml', '[b].[mlid] = [ml].[mlid]');
$ml_fields = ['mlid', 'plid', 'weight', 'has_children', 'depth'];
foreach (range(1, 9) as $i) {
$field = "p$i";
$ml_fields[] = $field;
$query->orderBy('ml.' . $field);
}
return $query->fields('ml', $ml_fields);
}
/**
* {@inheritdoc}
*/
public function getIds() {
$ids['mlid']['type'] = 'integer';
$ids['mlid']['alias'] = 'ml';
return $ids;
}
/**
* {@inheritdoc}
*/
public function fields() {
return [
'nid' => $this->t('Node ID'),
'bid' => $this->t('Book ID'),
'mlid' => $this->t('Menu link ID'),
'plid' => $this->t('Parent link ID'),
'weight' => $this->t('Weight'),
'p1' => $this->t('The first mlid in the materialized path. If N = depth, then pN must equal the mlid. If depth > 1 then p(N-1) must equal the parent link mlid. All pX where X > depth must equal zero. The columns p1 .. p9 are also called the parents.'),
'p2' => $this->t('The second mlid in the materialized path. See p1.'),
'p3' => $this->t('The third mlid in the materialized path. See p1.'),
'p4' => $this->t('The fourth mlid in the materialized path. See p1.'),
'p5' => $this->t('The fifth mlid in the materialized path. See p1.'),
'p6' => $this->t('The sixth mlid in the materialized path. See p1.'),
'p7' => $this->t('The seventh mlid in the materialized path. See p1.'),
'p8' => $this->t('The eighth mlid in the materialized path. See p1.'),
'p9' => $this->t('The ninth mlid in the materialized path. See p1.'),
];
}
}

View File

@@ -0,0 +1,74 @@
<?php
namespace Drupal\book\Plugin\views\argument_default;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\node\NodeStorageInterface;
use Drupal\node\Plugin\views\argument_default\Node;
use Drupal\views\Attribute\ViewsArgumentDefault;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Default argument plugin to get the current node's top level book.
*/
#[ViewsArgumentDefault(
id: 'top_level_book',
title: new TranslatableMarkup('Top Level Book from current node"'),
)]
class TopLevelBook extends Node {
/**
* The node storage controller.
*
* @var \Drupal\node\NodeStorageInterface
*/
protected $nodeStorage;
/**
* Constructs a Drupal\book\Plugin\views\argument_default\TopLevelBook object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
* @param \Drupal\node\NodeStorageInterface $node_storage
* The node storage controller.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, RouteMatchInterface $route_match, NodeStorageInterface $node_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $route_match);
$this->nodeStorage = $node_storage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('current_route_match'),
$container->get('entity_type.manager')->getStorage('node')
);
}
/**
* {@inheritdoc}
*/
public function getArgument() {
// Use the argument_default_node plugin to get the nid argument.
$nid = parent::getArgument();
if (!empty($nid)) {
$node = $this->nodeStorage->load($nid);
if (isset($node->book['bid'])) {
return $node->book['bid'];
}
}
}
}

View File

@@ -0,0 +1,243 @@
<?php
// phpcs:ignoreFile
/**
* This file was generated via php core/scripts/generate-proxy-class.php 'Drupal\book\BookManager' "core/modules/book/src".
*/
namespace Drupal\book\ProxyClass {
/**
* Provides a proxy class for \Drupal\book\BookManager.
*
* @see \Drupal\Component\ProxyBuilder
*/
class BookManager implements \Drupal\book\BookManagerInterface
{
use \Drupal\Core\DependencyInjection\DependencySerializationTrait;
/**
* The id of the original proxied service.
*
* @var string
*/
protected $drupalProxyOriginalServiceId;
/**
* The real proxied service, after it was lazy loaded.
*
* @var \Drupal\book\BookManager
*/
protected $service;
/**
* The service container.
*
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;
/**
* Constructs a ProxyClass Drupal proxy object.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The container.
* @param string $drupal_proxy_original_service_id
* The service ID of the original service.
*/
public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $container, $drupal_proxy_original_service_id)
{
$this->container = $container;
$this->drupalProxyOriginalServiceId = $drupal_proxy_original_service_id;
}
/**
* Lazy loads the real service from the container.
*
* @return object
* Returns the constructed real service.
*/
protected function lazyLoadItself()
{
if (!isset($this->service)) {
$this->service = $this->container->get($this->drupalProxyOriginalServiceId);
}
return $this->service;
}
/**
* {@inheritdoc}
*/
public function getAllBooks()
{
return $this->lazyLoadItself()->getAllBooks();
}
/**
* {@inheritdoc}
*/
public function getLinkDefaults($nid)
{
return $this->lazyLoadItself()->getLinkDefaults($nid);
}
/**
* {@inheritdoc}
*/
public function getParentDepthLimit(array $book_link)
{
return $this->lazyLoadItself()->getParentDepthLimit($book_link);
}
/**
* {@inheritdoc}
*/
public function addFormElements(array $form, \Drupal\Core\Form\FormStateInterface $form_state, \Drupal\node\NodeInterface $node, \Drupal\Core\Session\AccountInterface $account, $collapsed = true)
{
return $this->lazyLoadItself()->addFormElements($form, $form_state, $node, $account, $collapsed);
}
/**
* {@inheritdoc}
*/
public function checkNodeIsRemovable(\Drupal\node\NodeInterface $node)
{
return $this->lazyLoadItself()->checkNodeIsRemovable($node);
}
/**
* {@inheritdoc}
*/
public function updateOutline(\Drupal\node\NodeInterface $node)
{
return $this->lazyLoadItself()->updateOutline($node);
}
/**
* {@inheritdoc}
*/
public function getBookParents(array $item, array $parent = array (
))
{
return $this->lazyLoadItself()->getBookParents($item, $parent);
}
/**
* {@inheritdoc}
*/
public function getTableOfContents($bid, $depth_limit, array $exclude = array (
))
{
return $this->lazyLoadItself()->getTableOfContents($bid, $depth_limit, $exclude);
}
/**
* {@inheritdoc}
*/
public function deleteFromBook($nid)
{
return $this->lazyLoadItself()->deleteFromBook($nid);
}
/**
* {@inheritdoc}
*/
public function bookTreeAllData($bid, $link = NULL, $max_depth = NULL)
{
return $this->lazyLoadItself()->bookTreeAllData($bid, $link, $max_depth);
}
/**
* {@inheritdoc}
*/
public function getActiveTrailIds($bid, $link)
{
return $this->lazyLoadItself()->getActiveTrailIds($bid, $link);
}
/**
* {@inheritdoc}
*/
public function bookTreeOutput(array $tree)
{
return $this->lazyLoadItself()->bookTreeOutput($tree);
}
/**
* {@inheritdoc}
*/
public function bookTreeCollectNodeLinks(&$tree, &$node_links)
{
return $this->lazyLoadItself()->bookTreeCollectNodeLinks($tree, $node_links);
}
/**
* {@inheritdoc}
*/
public function bookTreeGetFlat(array $book_link)
{
return $this->lazyLoadItself()->bookTreeGetFlat($book_link);
}
/**
* {@inheritdoc}
*/
public function loadBookLink($nid, $translate = true)
{
return $this->lazyLoadItself()->loadBookLink($nid, $translate);
}
/**
* {@inheritdoc}
*/
public function loadBookLinks($nids, $translate = true)
{
return $this->lazyLoadItself()->loadBookLinks($nids, $translate);
}
/**
* {@inheritdoc}
*/
public function saveBookLink(array $link, $new)
{
return $this->lazyLoadItself()->saveBookLink($link, $new);
}
/**
* {@inheritdoc}
*/
public function bookTreeCheckAccess(&$tree, $node_links = array (
))
{
return $this->lazyLoadItself()->bookTreeCheckAccess($tree, $node_links);
}
/**
* {@inheritdoc}
*/
public function bookLinkTranslate(&$link)
{
return $this->lazyLoadItself()->bookLinkTranslate($link);
}
/**
* {@inheritdoc}
*/
public function bookSubtreeData($link)
{
return $this->lazyLoadItself()->bookSubtreeData($link);
}
/**
* {@inheritdoc}
*/
public function setStringTranslation(\Drupal\Core\StringTranslation\TranslationInterface $translation)
{
return $this->lazyLoadItself()->setStringTranslation($translation);
}
}
}

View File

@@ -0,0 +1,88 @@
<?php
// phpcs:ignoreFile
/**
* This file was generated via php core/scripts/generate-proxy-class.php 'Drupal\book\BookUninstallValidator' "core/modules/book/src".
*/
namespace Drupal\book\ProxyClass {
/**
* Provides a proxy class for \Drupal\book\BookUninstallValidator.
*
* @see \Drupal\Component\ProxyBuilder
*/
class BookUninstallValidator implements \Drupal\Core\Extension\ModuleUninstallValidatorInterface
{
use \Drupal\Core\DependencyInjection\DependencySerializationTrait;
/**
* The id of the original proxied service.
*
* @var string
*/
protected $drupalProxyOriginalServiceId;
/**
* The real proxied service, after it was lazy loaded.
*
* @var \Drupal\book\BookUninstallValidator
*/
protected $service;
/**
* The service container.
*
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;
/**
* Constructs a ProxyClass Drupal proxy object.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The container.
* @param string $drupal_proxy_original_service_id
* The service ID of the original service.
*/
public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $container, $drupal_proxy_original_service_id)
{
$this->container = $container;
$this->drupalProxyOriginalServiceId = $drupal_proxy_original_service_id;
}
/**
* Lazy loads the real service from the container.
*
* @return object
* Returns the constructed real service.
*/
protected function lazyLoadItself()
{
if (!isset($this->service)) {
$this->service = $this->container->get($this->drupalProxyOriginalServiceId);
}
return $this->service;
}
/**
* {@inheritdoc}
*/
public function validate($module)
{
return $this->lazyLoadItself()->validate($module);
}
/**
* {@inheritdoc}
*/
public function setStringTranslation(\Drupal\Core\StringTranslation\TranslationInterface $translation)
{
return $this->lazyLoadItself()->setStringTranslation($translation);
}
}
}

View File

@@ -0,0 +1,24 @@
{#
/**
* @file
* Default theme implementation for rendering book outlines within a block.
*
* This template is used only when the block is configured to "show block on all
* pages", which presents multiple independent books on all pages.
*
* Available variables:
* - book_menus: Book outlines.
* - id: The parent book ID.
* - title: The parent book title.
* - menu: The top-level book links.
*
* @see template_preprocess_book_all_books_block()
*
* @ingroup themeable
*/
#}
{% for book in book_menus %}
<nav role="navigation" aria-label="{% trans %}Book outline for {{ book.title }}{% endtrans %}">
{{ book.menu }}
</nav>
{% endfor %}

View File

@@ -0,0 +1,47 @@
{#
/**
* @file
* Default theme implementation for printed version of book outline.
*
* Available variables:
* - title: Top level node title.
* - head: Header tags.
* - language: Language object.
* - language_rtl: A flag indicating whether the current display language is a
* right to left language.
* - base_url: URL to the home page.
* - contents: Nodes within the current outline rendered through
* book-node-export-html.html.twig.
*
* @see template_preprocess_book_export_html()
*
* @ingroup themeable
*/
#}
<!DOCTYPE html>
<html{{ html_attributes }}>
<head>
<title>{{ title }}</title>
{{ page.head }}
<base href="{{ base_url }}" />
<link type="text/css" rel="stylesheet" href="misc/print.css" />
</head>
<body>
{#
The given node is embedded to its absolute depth in a top level section.
For example, a child node with depth 2 in the hierarchy is contained in
(otherwise empty) div elements corresponding to depth 0 and depth 1. This
is intended to support WYSIWYG output - e.g., level 3 sections always look
like level 3 sections, no matter their depth relative to the node selected
to be exported as printer-friendly HTML.
#}
{% if depth > 1 %}{% for i in 1..depth - 1 %}
<div>
{% endfor %}{% endif %}
{{ contents }}
{% if depth > 1 %}{% for i in 1..depth - 1 %}
</div>
{% endfor %}{% endif %}
</body>
</html>

View File

@@ -0,0 +1,57 @@
{#
/**
* @file
* Default theme implementation to navigate books.
*
* Presented under nodes that are a part of book outlines.
*
* Available variables:
* - tree: The immediate children of the current node rendered as an unordered
* list.
* - current_depth: Depth of the current node within the book outline. Provided
* for context.
* - prev_url: URL to the previous node.
* - prev_title: Title of the previous node.
* - parent_url: URL to the parent node.
* - parent_title: Title of the parent node. Not printed by default. Provided
* as an option.
* - next_url: URL to the next node.
* - next_title: Title of the next node.
* - has_links: Flags TRUE whenever the previous, parent or next data has a
* value.
* - book_id: The book ID of the current outline being viewed. Same as the node
* ID containing the entire outline. Provided for context.
* - book_url: The book/node URL of the current outline being viewed. Provided
* as an option. Not used by default.
* - book_title: The book/node title of the current outline being viewed.
*
* @see template_preprocess_book_navigation()
*
* @ingroup themeable
*/
#}
{% if tree or has_links %}
<nav role="navigation" aria-labelledby="book-label-{{ book_id }}">
{{ tree }}
{% if has_links %}
<h2>{{ 'Book traversal links for'|t }} {{ book_title }}</h2>
<ul>
{% if prev_url %}
<li>
<a href="{{ prev_url }}" rel="prev" title="{{ 'Go to previous page'|t }}"><b>{{ ''|t }}</b> {{ prev_title }}</a>
</li>
{% endif %}
{% if parent_url %}
<li>
<a href="{{ parent_url }}" title="{{ 'Go to parent page'|t }}">{{ 'Up'|t }}</a>
</li>
{% endif %}
{% if next_url %}
<li>
<a href="{{ next_url }}" rel="next" title="{{ 'Go to next page'|t }}">{{ next_title }} <b>{{ ''|t }}</b></a>
</li>
{% endif %}
</ul>
{% endif %}
</nav>
{% endif %}

View File

@@ -0,0 +1,22 @@
{#
/**
* @file
* Default theme implementation for a single node in a printer-friendly outline.
*
* Available variables:
* - node: Fully loaded node.
* - depth: Depth of the current node inside the outline.
* - title: Node title.
* - content: Node content.
* - children: All the child nodes recursively rendered through this file.
*
* @see template_preprocess_book_node_export_html()
*
* @ingroup themeable
*/
#}
<article>
<h1>{{ title }}</h1>
{{ content }}
{{ children }}
</article>

View File

@@ -0,0 +1,49 @@
{#
/**
* @file
* Default theme implementation to display a book tree.
*
* Returns HTML for a wrapper for a book sub-tree.
*
* Available variables:
* - items: A nested list of book items. Each book item contains:
* - attributes: HTML attributes for the book item.
* - below: The book item child items.
* - title: The book link title.
* - url: The book link URL, instance of \Drupal\Core\Url.
* - is_expanded: TRUE if the link has visible children within the current
* book tree.
* - is_collapsed: TRUE if the link has children within the current book tree
* that are not currently visible.
* - in_active_trail: TRUE if the link is in the active trail.
*
* @ingroup themeable
*/
#}
{% import _self as book_tree %}
{#
We call a macro which calls itself to render the full tree.
@see https://twig.symfony.com/doc/3.x/tags/macro.html
#}
{{ book_tree.book_links(items, attributes, 0) }}
{% macro book_links(items, attributes, menu_level) %}
{% import _self as book_tree %}
{% if items %}
{% if menu_level == 0 %}
<ul{{ attributes }}>
{% else %}
<ul>
{% endif %}
{% for item in items %}
<li{{ item.attributes }}>
{{ link(item.title, item.url) }}
{% if item.below %}
{{ book_tree.book_links(item.below, attributes, menu_level + 1) }}
{% endif %}
</li>
{% endfor %}
</ul>
{% endif %}
{% endmacro %}

11813
core/modules/book/tests/fixtures/drupal6.php vendored Executable file

File diff suppressed because it is too large Load Diff

27248
core/modules/book/tests/fixtures/drupal7.php vendored Executable file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,10 @@
name: 'Book module breadcrumb tests'
type: module
description: 'Support module for book module breadcrumb testing.'
package: Testing
# version: VERSION
# Information added by Drupal.org packaging script on 2024-07-04
version: '10.3.1'
project: 'drupal'
datestamp: 1720094222

View File

@@ -0,0 +1,27 @@
<?php
/**
* @file
* Test module for testing the book module breadcrumb.
*/
use Drupal\Core\Access\AccessResultForbidden;
use Drupal\Core\Access\AccessResultNeutral;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
/**
* Implements hook_ENTITY_TYPE_access().
*/
function book_breadcrumb_test_node_access(NodeInterface $node, $operation, AccountInterface $account) {
$config = \Drupal::config('book_breadcrumb_test.settings');
if ($config->get('hide') && $node->getTitle() == "you can't see me" && $operation == 'view') {
$access = new AccessResultForbidden();
}
else {
$access = new AccessResultNeutral();
}
$access->addCacheableDependency($config);
$access->addCacheableDependency($node);
return $access;
}

View File

@@ -0,0 +1,10 @@
name: 'Book module tests'
type: module
description: 'Support module for book module testing.'
package: Testing
# version: VERSION
# Information added by Drupal.org packaging script on 2024-07-04
version: '10.3.1'
project: 'drupal'
datestamp: 1720094222

View File

@@ -0,0 +1,22 @@
<?php
/**
* @file
* Test module for testing the book module.
*
* This module's functionality depends on the following state variables:
* - book_test.debug_book_navigation_cache_context: Used in NodeQueryAlterTest to enable the
* node_access_all grant realm.
*
* @see \Drupal\book\Tests\BookTest::testBookNavigationCacheContext()
*/
/**
* Implements hook_page_attachments().
*/
function book_test_page_attachments(array &$page) {
$page['#cache']['tags'][] = 'book_test.debug_book_navigation_cache_context';
if (\Drupal::state()->get('book_test.debug_book_navigation_cache_context', FALSE)) {
\Drupal::messenger()->addStatus(\Drupal::service('cache_contexts_manager')->convertTokensToKeys(['route.book_navigation'])->getKeys()[0]);
}
}

View File

@@ -0,0 +1,13 @@
name: 'Book test views'
type: module
description: 'Provides default views for views book tests.'
package: Testing
# version: VERSION
dependencies:
- drupal:book
- drupal:views
# Information added by Drupal.org packaging script on 2024-07-04
version: '10.3.1'
project: 'drupal'
datestamp: 1720094222

View File

@@ -0,0 +1,220 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Functional;
use Drupal\Tests\BrowserTestBase;
/**
* Create a book, add pages, and test book interface.
*
* @group book
* @group legacy
*/
class BookBreadcrumbTest extends BrowserTestBase {
/**
* Modules to install.
*
* @var array
*/
protected static $modules = ['book', 'block', 'book_breadcrumb_test'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* A book node.
*
* @var \Drupal\node\NodeInterface
*/
protected $book;
/**
* A user with permission to create and edit books.
*
* @var \Drupal\user\Entity\User
*/
protected $bookAuthor;
/**
* A user without the 'node test view' permission.
*
* @var \Drupal\user\UserInterface
*/
protected $webUserWithoutNodeAccess;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->drupalPlaceBlock('system_breadcrumb_block');
$this->drupalPlaceBlock('page_title_block');
// Create users.
$this->bookAuthor = $this->drupalCreateUser([
'create new books',
'create book content',
'edit own book content',
'add content to books',
]);
}
/**
* Creates a new book with a page hierarchy.
*
* @return \Drupal\node\NodeInterface[]
* The created book nodes.
*/
protected function createBreadcrumbBook() {
// Create new book.
$this->drupalLogin($this->bookAuthor);
$this->book = $this->createBookNode('new');
$book = $this->book;
/*
* Add page hierarchy to book.
* Book
* |- Node 0
* |- Node 1
* |- Node 2
* |- Node 3
* |- Node 4
* |- Node 5
* |- Node 6
*/
$nodes = [];
$nodes[0] = $this->createBookNode($book->id());
$nodes[1] = $this->createBookNode($book->id(), $nodes[0]->id());
$nodes[2] = $this->createBookNode($book->id(), $nodes[0]->id());
$nodes[3] = $this->createBookNode($book->id(), $nodes[2]->id());
$nodes[4] = $this->createBookNode($book->id(), $nodes[3]->id());
$nodes[5] = $this->createBookNode($book->id(), $nodes[4]->id());
$nodes[6] = $this->createBookNode($book->id());
$this->drupalLogout();
return $nodes;
}
/**
* Creates a book node.
*
* @param int|string $book_nid
* A book node ID or set to 'new' to create a new book.
* @param int|null $parent
* (optional) Parent book reference ID. Defaults to NULL.
*
* @return \Drupal\node\NodeInterface
* The created node.
*/
protected function createBookNode($book_nid, $parent = NULL) {
// $number does not use drupal_static as it should not be reset since it
// uniquely identifies each call to createBookNode(). It is used to ensure
// that when sorted nodes stay in same order.
static $number = 0;
$edit = [];
$edit['title[0][value]'] = str_pad((string) $number, 2, '0', STR_PAD_LEFT) . ' - test node ' . $this->randomMachineName(10);
$edit['body[0][value]'] = 'test body ' . $this->randomMachineName(32) . ' ' . $this->randomMachineName(32);
$edit['book[bid]'] = $book_nid;
if ($parent !== NULL) {
$this->drupalGet('node/add/book');
$this->submitForm($edit, 'Change book (update list of parents)');
$edit['book[pid]'] = $parent;
$this->submitForm($edit, 'Save');
// Make sure the parent was flagged as having children.
$parent_node = \Drupal::entityTypeManager()->getStorage('node')->loadUnchanged($parent);
$this->assertNotEmpty($parent_node->book['has_children'], 'Parent node is marked as having children');
}
else {
$this->drupalGet('node/add/book');
$this->submitForm($edit, 'Save');
}
// Check to make sure the book node was created.
$node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
$this->assertNotNull(($node === FALSE ? NULL : $node), 'Book node found in database.');
$number++;
return $node;
}
/**
* Tests that the breadcrumb is updated when book content changes.
*/
public function testBreadcrumbTitleUpdates(): void {
// Create a new book.
$nodes = $this->createBreadcrumbBook();
$book = $this->book;
$this->drupalLogin($this->bookAuthor);
$this->drupalGet($nodes[4]->toUrl());
// Fetch each node title in the current breadcrumb.
$links = $this->xpath('//nav[@aria-labelledby="system-breadcrumb"]/ol/li/a');
$got_breadcrumb = [];
foreach ($links as $link) {
$got_breadcrumb[] = $link->getText();
}
// Home link and four parent book nodes should be in the breadcrumb.
$this->assertCount(5, $got_breadcrumb);
$this->assertEquals($nodes[3]->getTitle(), end($got_breadcrumb));
$edit = [
'title[0][value]' => 'Updated node5 title',
];
$this->drupalGet($nodes[3]->toUrl('edit-form'));
$this->submitForm($edit, 'Save');
$this->drupalGet($nodes[4]->toUrl());
// Fetch each node title in the current breadcrumb.
$links = $this->xpath('//nav[@aria-labelledby="system-breadcrumb"]/ol/li/a');
$got_breadcrumb = [];
foreach ($links as $link) {
$got_breadcrumb[] = $link->getText();
}
$this->assertCount(5, $got_breadcrumb);
$this->assertEquals($edit['title[0][value]'], end($got_breadcrumb));
}
/**
* Tests that the breadcrumb is updated when book access changes.
*/
public function testBreadcrumbAccessUpdates(): void {
// Create a new book.
$nodes = $this->createBreadcrumbBook();
$this->drupalLogin($this->bookAuthor);
$edit = [
'title[0][value]' => "you can't see me",
];
$this->drupalGet($nodes[3]->toUrl('edit-form'));
$this->submitForm($edit, 'Save');
$this->drupalGet($nodes[4]->toUrl());
$links = $this->xpath('//nav[@aria-labelledby="system-breadcrumb"]/ol/li/a');
$got_breadcrumb = [];
foreach ($links as $link) {
$got_breadcrumb[] = $link->getText();
}
$this->assertCount(5, $got_breadcrumb);
$this->assertEquals($edit['title[0][value]'], end($got_breadcrumb));
$config = $this->container->get('config.factory')->getEditable('book_breadcrumb_test.settings');
$config->set('hide', TRUE)->save();
$this->drupalGet($nodes[4]->toUrl());
$links = $this->xpath('//nav[@aria-labelledby="system-breadcrumb"]/ol/li/a');
$got_breadcrumb = [];
foreach ($links as $link) {
$got_breadcrumb[] = $link->getText();
}
$this->assertCount(4, $got_breadcrumb);
$this->assertEquals($nodes[2]->getTitle(), end($got_breadcrumb));
$this->drupalGet($nodes[3]->toUrl());
$this->assertSession()->statusCodeEquals(403);
}
}

View File

@@ -0,0 +1,169 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
/**
* Tests Book and Content Moderation integration.
*
* @group book
* @group legacy
*/
class BookContentModerationTest extends BrowserTestBase {
use BookTestTrait;
use ContentModerationTestTrait;
/**
* Modules to install.
*
* @var array
*/
protected static $modules = [
'book',
'block',
'book_test',
'content_moderation',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->drupalPlaceBlock('system_breadcrumb_block');
$this->drupalPlaceBlock('page_title_block');
$workflow = $this->createEditorialWorkflow();
$workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'book');
$workflow->save();
// We need a user with additional content moderation permissions.
$this->bookAuthor = $this->drupalCreateUser([
'create new books',
'create book content',
'edit own book content',
'add content to books',
'access printer-friendly version',
'view any unpublished content',
'use editorial transition create_new_draft',
'use editorial transition publish',
]);
}
/**
* Tests that book drafts can not modify the book outline.
*/
public function testBookWithPendingRevisions(): void {
// Create two books.
$book_1_nodes = $this->createBook(['moderation_state[0][state]' => 'published']);
$book_1 = $this->book;
$this->createBook(['moderation_state[0][state]' => 'published']);
$book_2 = $this->book;
$this->drupalLogin($this->bookAuthor);
// Check that book pages display along with the correct outlines.
$this->book = $book_1;
$this->checkBookNode($book_1, [$book_1_nodes[0], $book_1_nodes[3], $book_1_nodes[4]], FALSE, FALSE, $book_1_nodes[0], []);
$this->checkBookNode($book_1_nodes[0], [$book_1_nodes[1], $book_1_nodes[2]], $book_1, $book_1, $book_1_nodes[1], [$book_1]);
// Create a new book page without actually attaching it to a book and create
// a draft.
$edit = [
'title[0][value]' => $this->randomString(),
'moderation_state[0][state]' => 'published',
];
$this->drupalGet('node/add/book');
$this->submitForm($edit, 'Save');
$node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
$this->assertNotEmpty($node);
$edit = [
'moderation_state[0][state]' => 'draft',
];
$this->drupalGet('node/' . $node->id() . '/edit');
$this->submitForm($edit, 'Save');
$this->assertSession()->pageTextNotContains('You can only change the book outline for the published version of this content.');
// Create a book draft with no changes, then publish it.
$edit = [
'moderation_state[0][state]' => 'draft',
];
$this->drupalGet('node/' . $book_1->id() . '/edit');
$this->submitForm($edit, 'Save');
$this->assertSession()->pageTextNotContains('You can only change the book outline for the published version of this content.');
$edit = [
'moderation_state[0][state]' => 'published',
];
$this->drupalGet('node/' . $book_1->id() . '/edit');
$this->submitForm($edit, 'Save');
// Try to move Node 2 to a different parent.
$edit = [
'book[pid]' => $book_1_nodes[3]->id(),
'moderation_state[0][state]' => 'draft',
];
$this->drupalGet('node/' . $book_1_nodes[1]->id() . '/edit');
$this->submitForm($edit, 'Save');
$this->assertSession()->pageTextContains('You can only change the book outline for the published version of this content.');
// Check that the book outline did not change.
$this->book = $book_1;
$this->checkBookNode($book_1, [$book_1_nodes[0], $book_1_nodes[3], $book_1_nodes[4]], FALSE, FALSE, $book_1_nodes[0], []);
$this->checkBookNode($book_1_nodes[0], [$book_1_nodes[1], $book_1_nodes[2]], $book_1, $book_1, $book_1_nodes[1], [$book_1]);
// Try to move Node 2 to a different book.
$edit = [
'book[bid]' => $book_2->id(),
'moderation_state[0][state]' => 'draft',
];
$this->drupalGet('node/' . $book_1_nodes[1]->id() . '/edit');
$this->submitForm($edit, 'Save');
$this->assertSession()->pageTextContains('You can only change the book outline for the published version of this content.');
// Check that the book outline did not change.
$this->book = $book_1;
$this->checkBookNode($book_1, [$book_1_nodes[0], $book_1_nodes[3], $book_1_nodes[4]], FALSE, FALSE, $book_1_nodes[0], []);
$this->checkBookNode($book_1_nodes[0], [$book_1_nodes[1], $book_1_nodes[2]], $book_1, $book_1, $book_1_nodes[1], [$book_1]);
// Try to change the weight of Node 2.
$edit = [
'book[weight]' => 2,
'moderation_state[0][state]' => 'draft',
];
$this->drupalGet('node/' . $book_1_nodes[1]->id() . '/edit');
$this->submitForm($edit, 'Save');
$this->assertSession()->pageTextContains('You can only change the book outline for the published version of this content.');
// Check that the book outline did not change.
$this->book = $book_1;
$this->checkBookNode($book_1, [$book_1_nodes[0], $book_1_nodes[3], $book_1_nodes[4]], FALSE, FALSE, $book_1_nodes[0], []);
$this->checkBookNode($book_1_nodes[0], [$book_1_nodes[1], $book_1_nodes[2]], $book_1, $book_1, $book_1_nodes[1], [$book_1]);
// Save a new draft revision for the node without any changes and check that
// the error message is not displayed.
$edit = [
'moderation_state[0][state]' => 'draft',
];
$this->drupalGet('node/' . $book_1_nodes[1]->id() . '/edit');
$this->submitForm($edit, 'Save');
$this->assertSession()->pageTextNotContains('You can only change the book outline for the published version of this content.');
}
}

View File

@@ -0,0 +1,728 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Functional;
use Drupal\Core\Cache\Cache;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\RoleInterface;
/**
* Create a book, add pages, and test book interface.
*
* @group book
* @group legacy
* @group #slow
*/
class BookTest extends BrowserTestBase {
use BookTestTrait;
/**
* Modules to install.
*
* @var array
*/
protected static $modules = [
'content_moderation',
'book',
'block',
'node_access_test',
'book_test',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* A user with permission to view a book and access printer-friendly version.
*
* @var \Drupal\user\UserInterface
*/
protected $webUser;
/**
* A user with permission to create and edit books and to administer blocks.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* A user without the 'node test view' permission.
*
* @var \Drupal\user\UserInterface
*/
protected $webUserWithoutNodeAccess;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->drupalPlaceBlock('system_breadcrumb_block');
$this->drupalPlaceBlock('page_title_block');
// node_access_test requires a node_access_rebuild().
node_access_rebuild();
// Create users.
$this->bookAuthor = $this->drupalCreateUser([
'create new books',
'create book content',
'edit own book content',
'add content to books',
'view own unpublished content',
]);
$this->webUser = $this->drupalCreateUser([
'access printer-friendly version',
'node test view',
]);
$this->webUserWithoutNodeAccess = $this->drupalCreateUser([
'access printer-friendly version',
]);
$this->adminUser = $this->drupalCreateUser([
'create new books',
'create book content',
'edit any book content',
'delete any book content',
'add content to books',
'administer blocks',
'administer permissions',
'administer book outlines',
'node test view',
'administer content types',
'administer site configuration',
'view any unpublished content',
]);
}
/**
* Tests the book navigation cache context.
*
* @see \Drupal\book\Cache\BookNavigationCacheContext
*/
public function testBookNavigationCacheContext(): void {
// Create a page node.
$this->drupalCreateContentType(['type' => 'page']);
$page = $this->drupalCreateNode();
// Create a book, consisting of book nodes.
$book_nodes = $this->createBook();
// Enable the debug output.
\Drupal::state()->set('book_test.debug_book_navigation_cache_context', TRUE);
Cache::invalidateTags(['book_test.debug_book_navigation_cache_context']);
$this->drupalLogin($this->bookAuthor);
// On non-node route.
$this->drupalGet($this->adminUser->toUrl());
$this->assertSession()->responseContains('[route.book_navigation]=book.none');
// On non-book node route.
$this->drupalGet($page->toUrl());
$this->assertSession()->responseContains('[route.book_navigation]=book.none');
// On book node route.
$this->drupalGet($book_nodes[0]->toUrl());
$this->assertSession()->responseContains('[route.book_navigation]=0|2|3');
$this->drupalGet($book_nodes[1]->toUrl());
$this->assertSession()->responseContains('[route.book_navigation]=0|2|3|4');
$this->drupalGet($book_nodes[2]->toUrl());
$this->assertSession()->responseContains('[route.book_navigation]=0|2|3|5');
$this->drupalGet($book_nodes[3]->toUrl());
$this->assertSession()->responseContains('[route.book_navigation]=0|2|6');
$this->drupalGet($book_nodes[4]->toUrl());
$this->assertSession()->responseContains('[route.book_navigation]=0|2|7');
}
/**
* Tests saving the book outline on an empty book.
*/
public function testEmptyBook(): void {
// Create a new empty book.
$this->drupalLogin($this->bookAuthor);
$book = $this->createBookNode('new');
$this->drupalLogout();
// Log in as a user with access to the book outline and save the form.
$this->drupalLogin($this->adminUser);
$this->drupalGet('admin/structure/book/' . $book->id());
$this->submitForm([], 'Save book pages');
$this->assertSession()->pageTextContains('Updated book ' . $book->label() . '.');
}
/**
* Tests book functionality through node interfaces.
*/
public function testBook(): void {
// Create new book.
$nodes = $this->createBook();
$book = $this->book;
$this->drupalLogin($this->webUser);
// Check that book pages display along with the correct outlines and
// previous/next links.
$this->checkBookNode($book, [$nodes[0], $nodes[3], $nodes[4]], FALSE, FALSE, $nodes[0], []);
$this->checkBookNode($nodes[0], [$nodes[1], $nodes[2]], $book, $book, $nodes[1], [$book]);
$this->checkBookNode($nodes[1], NULL, $nodes[0], $nodes[0], $nodes[2], [$book, $nodes[0]]);
$this->checkBookNode($nodes[2], NULL, $nodes[1], $nodes[0], $nodes[3], [$book, $nodes[0]]);
$this->checkBookNode($nodes[3], NULL, $nodes[2], $book, $nodes[4], [$book]);
$this->checkBookNode($nodes[4], NULL, $nodes[3], $book, FALSE, [$book]);
$this->drupalLogout();
$this->drupalLogin($this->bookAuthor);
// Check the presence of expected cache tags.
$this->drupalGet('node/add/book');
$this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', 'config:book.settings');
/*
* Add Node 5 under Node 3.
* Book
* |- Node 0
* |- Node 1
* |- Node 2
* |- Node 3
* |- Node 5
* |- Node 4
*/
// Node 5.
$nodes[] = $this->createBookNode($book->id(), $nodes[3]->book['nid']);
$this->drupalLogout();
$this->drupalLogin($this->webUser);
// Verify the new outline - make sure we don't get stale cached data.
$this->checkBookNode($nodes[3], [$nodes[5]], $nodes[2], $book, $nodes[5], [$book]);
$this->checkBookNode($nodes[4], NULL, $nodes[5], $book, FALSE, [$book]);
$this->drupalLogout();
// Create a second book, and move an existing book page into it.
$this->drupalLogin($this->bookAuthor);
$other_book = $this->createBookNode('new');
$node = $this->createBookNode($book->id());
$edit = ['book[bid]' => $other_book->id()];
$this->drupalGet('node/' . $node->id() . '/edit');
$this->submitForm($edit, 'Save');
$this->drupalLogout();
$this->drupalLogin($this->webUser);
// Check that the nodes in the second book are displayed correctly.
// First we must set $this->book to the second book, so that the
// correct regex will be generated for testing the outline.
$this->book = $other_book;
$this->checkBookNode($other_book, [$node], FALSE, FALSE, $node, []);
$this->checkBookNode($node, NULL, $other_book, $other_book, FALSE, [$other_book]);
// Test that we can save a book programmatically.
$this->drupalLogin($this->bookAuthor);
$book = $this->createBookNode('new');
$book->save();
// Confirm that an unpublished book page has the 'Add child page' link.
$this->drupalGet('node/' . $nodes[4]->id());
$this->assertSession()->linkExists('Add child page');
$nodes[4]->setUnPublished();
$nodes[4]->save();
$this->drupalGet('node/' . $nodes[4]->id());
$this->assertSession()->linkExists('Add child page');
}
/**
* Tests book export ("printer-friendly version") functionality.
*/
public function testBookExport(): void {
// Create a book.
$nodes = $this->createBook();
// Log in as web user and view printer-friendly version.
$this->drupalLogin($this->webUser);
$this->drupalGet('node/' . $this->book->id());
$this->clickLink('Printer-friendly version');
// Make sure each part of the book is there.
foreach ($nodes as $node) {
$this->assertSession()->pageTextContains($node->label());
$this->assertSession()->responseContains($node->body->processed);
}
// Make sure we can't export an unsupported format.
$this->drupalGet('book/export/foobar/' . $this->book->id());
$this->assertSession()->statusCodeEquals(404);
// Make sure we get a 404 on a non-existent book node.
$this->drupalGet('book/export/html/123');
$this->assertSession()->statusCodeEquals(404);
// Make sure an anonymous user cannot view printer-friendly version.
$this->drupalLogout();
// Load the book and verify there is no printer-friendly version link.
$this->drupalGet('node/' . $this->book->id());
$this->assertSession()->linkNotExists('Printer-friendly version', 'Anonymous user is not shown link to printer-friendly version.');
// Try getting the URL directly, and verify it fails.
$this->drupalGet('book/export/html/' . $this->book->id());
$this->assertSession()->statusCodeEquals(403);
// Now grant anonymous users permission to view the printer-friendly
// version and verify that node access restrictions still prevent them from
// seeing it.
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access printer-friendly version']);
$this->drupalGet('book/export/html/' . $this->book->id());
$this->assertSession()->statusCodeEquals(403);
}
/**
* Tests the functionality of the book navigation block.
*/
public function testBookNavigationBlock(): void {
$this->drupalLogin($this->adminUser);
// Enable the block.
$block = $this->drupalPlaceBlock('book_navigation');
// Give anonymous users the permission 'node test view'.
$edit = [];
$edit[RoleInterface::ANONYMOUS_ID . '[node test view]'] = TRUE;
$this->drupalGet('admin/people/permissions/' . RoleInterface::ANONYMOUS_ID);
$this->submitForm($edit, 'Save permissions');
$this->assertSession()->pageTextContains('The changes have been saved.');
// Test correct display of the block.
$nodes = $this->createBook();
$this->drupalGet('<front>');
// Book navigation block.
$this->assertSession()->pageTextContains($block->label());
// Link to book root.
$this->assertSession()->pageTextContains($this->book->label());
// No links to individual book pages.
$this->assertSession()->pageTextNotContains($nodes[0]->label());
// Ensure that an unpublished node does not appear in the navigation for a
// user without access. By unpublishing a parent page, child pages should
// not appear in the navigation. The node_access_test module is disabled
// since it interferes with this logic.
/** @var \Drupal\Core\Extension\ModuleInstaller $installer */
$installer = \Drupal::service('module_installer');
$installer->uninstall(['node_access_test']);
node_access_rebuild();
$nodes[0]->setUnPublished();
$nodes[0]->save();
// Verify the user does not have access to the unpublished node.
$this->assertFalse($nodes[0]->access('view', $this->webUser));
// Verify the unpublished book page does not appear in the navigation.
$this->drupalLogin($this->webUser);
$this->drupalGet($nodes[0]->toUrl());
$this->assertSession()->statusCodeEquals(403);
$this->drupalGet($this->book->toUrl());
$this->assertSession()->responseNotContains($nodes[0]->getTitle());
$this->assertSession()->responseNotContains($nodes[1]->getTitle());
$this->assertSession()->responseNotContains($nodes[2]->getTitle());
}
/**
* Tests BookManager::getTableOfContents().
*/
public function testGetTableOfContents(): void {
// Create new book.
$nodes = $this->createBook();
$book = $this->book;
$this->drupalLogin($this->bookAuthor);
/*
* Add Node 5 under Node 2.
* Add Node 6, 7, 8, 9, 10, 11 under Node 3.
* Book
* |- Node 0
* |- Node 1
* |- Node 2
* |- Node 5
* |- Node 3
* |- Node 6
* |- Node 7
* |- Node 8
* |- Node 9
* |- Node 10
* |- Node 11
* |- Node 4
*/
foreach ([5 => 2, 6 => 3, 7 => 6, 8 => 7, 9 => 8, 10 => 9, 11 => 10] as $child => $parent) {
$nodes[$child] = $this->createBookNode($book->id(), $nodes[$parent]->id());
}
$this->drupalGet($nodes[0]->toUrl('edit-form'));
// Since Node 0 has children 2 levels deep, nodes 10 and 11 should not
// appear in the selector.
$this->assertSession()->optionNotExists('edit-book-pid', $nodes[10]->id());
$this->assertSession()->optionNotExists('edit-book-pid', $nodes[11]->id());
// Node 9 should be available as an option.
$this->assertSession()->optionExists('edit-book-pid', $nodes[9]->id());
// Get a shallow set of options.
/** @var \Drupal\book\BookManagerInterface $manager */
$manager = $this->container->get('book.manager');
$options = $manager->getTableOfContents($book->id(), 3);
// Verify that all expected option keys are present.
$expected_nids = [$book->id(), $nodes[0]->id(), $nodes[1]->id(), $nodes[2]->id(), $nodes[3]->id(), $nodes[6]->id(), $nodes[4]->id()];
$this->assertEquals($expected_nids, array_keys($options));
// Exclude Node 3.
$options = $manager->getTableOfContents($book->id(), 3, [$nodes[3]->id()]);
// Verify that expected option keys are present after excluding Node 3.
$expected_nids = [$book->id(), $nodes[0]->id(), $nodes[1]->id(), $nodes[2]->id(), $nodes[4]->id()];
$this->assertEquals($expected_nids, array_keys($options));
}
/**
* Tests the book navigation block when an access module is installed.
*/
public function testNavigationBlockOnAccessModuleInstalled(): void {
$this->drupalLogin($this->adminUser);
$block = $this->drupalPlaceBlock('book_navigation', ['block_mode' => 'book pages']);
// Give anonymous users the permission 'node test view'.
$edit = [];
$edit[RoleInterface::ANONYMOUS_ID . '[node test view]'] = TRUE;
$this->drupalGet('admin/people/permissions/' . RoleInterface::ANONYMOUS_ID);
$this->submitForm($edit, 'Save permissions');
$this->assertSession()->pageTextContains('The changes have been saved.');
// Create a book.
$this->createBook();
// Test correct display of the block to registered users.
$this->drupalLogin($this->webUser);
$this->drupalGet('node/' . $this->book->id());
$this->assertSession()->pageTextContains($block->label());
$this->drupalLogout();
// Test correct display of the block to anonymous users.
$this->drupalGet('node/' . $this->book->id());
$this->assertSession()->pageTextContains($block->label());
// Test the 'book pages' block_mode setting.
$this->drupalGet('<front>');
$this->assertSession()->pageTextNotContains($block->label());
}
/**
* Tests the access for deleting top-level book nodes.
*/
public function testBookDelete(): void {
$node_storage = $this->container->get('entity_type.manager')->getStorage('node');
$nodes = $this->createBook();
$this->drupalLogin($this->adminUser);
$edit = [];
// Ensure that the top-level book node cannot be deleted.
$this->drupalGet('node/' . $this->book->id() . '/outline/remove');
$this->assertSession()->statusCodeEquals(403);
// Ensure that a child book node can be deleted.
$this->drupalGet('node/' . $nodes[4]->id() . '/outline/remove');
$this->submitForm($edit, 'Remove');
$node_storage->resetCache([$nodes[4]->id()]);
$node4 = $node_storage->load($nodes[4]->id());
$this->assertEmpty($node4->book, 'Deleting child book node properly allowed.');
// $nodes[4] is stale, trying to delete it directly will cause an error.
$node4->delete();
unset($nodes[4]);
// Delete all child book nodes and retest top-level node deletion.
$node_storage->delete($nodes);
$this->drupalGet('node/' . $this->book->id() . '/outline/remove');
$this->submitForm($edit, 'Remove');
$node_storage->resetCache([$this->book->id()]);
$node = $node_storage->load($this->book->id());
$this->assertEmpty($node->book, 'Deleting childless top-level book node properly allowed.');
// Tests directly deleting a book parent.
$nodes = $this->createBook();
$this->drupalLogin($this->adminUser);
$this->drupalGet($this->book->toUrl('delete-form'));
$this->assertSession()->pageTextContains($this->book->label() . ' is part of a book outline, and has associated child pages. If you proceed with deletion, the child pages will be relocated automatically.');
// Delete parent, and visit a child page.
$this->drupalGet($this->book->toUrl('delete-form'));
$this->submitForm([], 'Delete');
$this->drupalGet($nodes[0]->toUrl());
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains($nodes[0]->label());
// The book parents should be updated.
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
$node_storage->resetCache();
$child = $node_storage->load($nodes[0]->id());
$this->assertEquals($child->id(), $child->book['bid'], 'Child node book ID updated when parent is deleted.');
// 3rd-level children should now be 2nd-level.
$second = $node_storage->load($nodes[1]->id());
$this->assertEquals($child->id(), $second->book['bid'], '3rd-level child node is now second level when top-level node is deleted.');
}
/**
* Tests outline of a book.
*/
public function testBookOutline(): void {
$this->drupalLogin($this->bookAuthor);
// Create new node not yet a book.
$empty_book = $this->drupalCreateNode(['type' => 'book']);
$this->drupalGet('node/' . $empty_book->id() . '/outline');
$this->assertSession()->linkNotExists('Book outline', 'Book Author is not allowed to outline');
$this->drupalLogin($this->adminUser);
$this->drupalGet('node/' . $empty_book->id() . '/outline');
$this->assertSession()->pageTextContains('Book outline');
// Verify that the node does not belong to a book.
$this->assertTrue($this->assertSession()->optionExists('edit-book-bid', 0)->isSelected());
$this->assertSession()->linkNotExists('Remove from book outline');
$edit = [];
$edit['book[bid]'] = '1';
$this->drupalGet('node/' . $empty_book->id() . '/outline');
$this->submitForm($edit, 'Add to book outline');
$node = \Drupal::entityTypeManager()->getStorage('node')->load($empty_book->id());
// Test the book array.
$this->assertEquals($empty_book->id(), $node->book['nid']);
$this->assertEquals($empty_book->id(), $node->book['bid']);
$this->assertEquals(1, $node->book['depth']);
$this->assertEquals($empty_book->id(), $node->book['p1']);
$this->assertEquals('0', $node->book['pid']);
// Create new book.
$this->drupalLogin($this->bookAuthor);
$book = $this->createBookNode('new');
$this->drupalLogin($this->adminUser);
$this->drupalGet('node/' . $book->id() . '/outline');
$this->assertSession()->pageTextContains('Book outline');
$this->clickLink('Remove from book outline');
$this->assertSession()->pageTextContains('Are you sure you want to remove ' . $book->label() . ' from the book hierarchy?');
// Create a new node and set the book after the node was created.
$node = $this->drupalCreateNode(['type' => 'book']);
$edit = [];
$edit['book[bid]'] = $node->id();
$this->drupalGet('node/' . $node->id() . '/edit');
$this->submitForm($edit, 'Save');
$node = \Drupal::entityTypeManager()->getStorage('node')->load($node->id());
// Test the book array.
$this->assertEquals($node->id(), $node->book['nid']);
$this->assertEquals($node->id(), $node->book['bid']);
$this->assertEquals(1, $node->book['depth']);
$this->assertEquals($node->id(), $node->book['p1']);
$this->assertEquals('0', $node->book['pid']);
// Test the form itself.
$this->drupalGet('node/' . $node->id() . '/edit');
$this->assertTrue($this->assertSession()->optionExists('edit-book-bid', $node->id())->isSelected());
}
/**
* Tests that saveBookLink() returns something.
*/
public function testSaveBookLink(): void {
$book_manager = \Drupal::service('book.manager');
// Mock a link for a new book.
$link = ['nid' => 1, 'has_children' => 0, 'original_bid' => 0, 'pid' => 0, 'weight' => 0, 'bid' => 0];
$new = TRUE;
// Save the link.
$return = $book_manager->saveBookLink($link, $new);
// Add the link defaults to $link so we have something to compare to the return from saveBookLink().
$link = $book_manager->getLinkDefaults($link['nid']);
// Test the return from saveBookLink.
$this->assertEquals($return, $link);
}
/**
* Tests the listing of all books.
*/
public function testBookListing(): void {
// Uninstall 'node_access_test' as this interferes with the test.
\Drupal::service('module_installer')->uninstall(['node_access_test']);
// Create a new book.
$nodes = $this->createBook();
// Load the book page and assert the created book title is displayed.
$this->drupalGet('book');
$this->assertSession()->pageTextContains($this->book->label());
// Unpublish the top book page and confirm that the created book title is
// not displayed for anonymous.
$this->book->setUnpublished();
$this->book->save();
$this->drupalGet('book');
$this->assertSession()->pageTextNotContains($this->book->label());
// Publish the top book page and unpublish a page in the book and confirm
// that the created book title is displayed for anonymous.
$this->book->setPublished();
$this->book->save();
$nodes[0]->setUnpublished();
$nodes[0]->save();
$this->drupalGet('book');
$this->assertSession()->pageTextContains($this->book->label());
// Unpublish the top book page and confirm that the created book title is
// displayed for user which has 'view own unpublished content' permission.
$this->drupalLogin($this->bookAuthor);
$this->book->setUnpublished();
$this->book->save();
$this->drupalGet('book');
$this->assertSession()->pageTextContains($this->book->label());
// Ensure the user doesn't see the book if they don't own it.
$this->book->setOwner($this->webUser)->save();
$this->drupalGet('book');
$this->assertSession()->pageTextNotContains($this->book->label());
// Confirm that the created book title is displayed for user which has
// 'view any unpublished content' permission.
$this->drupalLogin($this->adminUser);
$this->drupalGet('book');
$this->assertSession()->pageTextContains($this->book->label());
}
/**
* Tests the administrative listing of all books.
*/
public function testAdminBookListing(): void {
// Create a new book.
$nodes = $this->createBook();
// Load the book page and assert the created book title is displayed.
$this->drupalLogin($this->adminUser);
$this->drupalGet('admin/structure/book');
$this->assertSession()->pageTextContains($this->book->label());
}
/**
* Tests the administrative listing of all book pages in a book.
*/
public function testAdminBookNodeListing(): void {
// Create a new book.
$nodes = $this->createBook();
$this->drupalLogin($this->adminUser);
// Load the book page list and assert the created book title is displayed
// and action links are shown on list items.
$this->drupalGet('admin/structure/book/' . $this->book->id());
$this->assertSession()->pageTextContains($this->book->label());
// Test that the view link is found from the list.
$this->assertSession()->elementTextEquals('xpath', '//table//ul[@class="dropbutton"]/li/a', 'View');
// Test that all the book pages are displayed on the book outline page.
$this->assertSession()->elementsCount('xpath', '//table//ul[@class="dropbutton"]/li/a', count($nodes));
// Unpublish a book in the hierarchy.
$nodes[0]->setUnPublished();
$nodes[0]->save();
// Node should still appear on the outline for admins.
$this->drupalGet('admin/structure/book/' . $this->book->id());
$this->assertSession()->elementsCount('xpath', '//table//ul[@class="dropbutton"]/li/a', count($nodes));
// Saving a book page not as the current version shouldn't effect the book.
$old_title = $nodes[1]->getTitle();
$new_title = $this->getRandomGenerator()->name();
$nodes[1]->isDefaultRevision(FALSE);
$nodes[1]->setNewRevision(TRUE);
$nodes[1]->setTitle($new_title);
$nodes[1]->save();
$this->drupalGet('admin/structure/book/' . $this->book->id());
$this->assertSession()->elementsCount('xpath', '//table//ul[@class="dropbutton"]/li/a', count($nodes));
$this->assertSession()->responseNotContains($new_title);
$this->assertSession()->responseContains($old_title);
}
/**
* Ensure the loaded book in hook_node_load() does not depend on the user.
*/
public function testHookNodeLoadAccess(): void {
\Drupal::service('module_installer')->install(['node_access_test']);
// Ensure that the loaded book in hook_node_load() does NOT depend on the
// current user.
$this->drupalLogin($this->bookAuthor);
$this->book = $this->createBookNode('new');
// Reset any internal static caching.
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
$node_storage->resetCache();
// Log in as user without access to the book node, so no 'node test view'
// permission.
// @see node_access_test_node_grants().
$this->drupalLogin($this->webUserWithoutNodeAccess);
$book_node = $node_storage->load($this->book->id());
$this->assertNotEmpty($book_node->book);
$this->assertEquals($this->book->id(), $book_node->book['bid']);
// Reset the internal cache to retrigger the hook_node_load() call.
$node_storage->resetCache();
$this->drupalLogin($this->webUser);
$book_node = $node_storage->load($this->book->id());
$this->assertNotEmpty($book_node->book);
$this->assertEquals($this->book->id(), $book_node->book['bid']);
}
/**
* Tests the book navigation block when book is unpublished.
*
* There was a fatal error with "Show block only on book pages" block mode.
*/
public function testBookNavigationBlockOnUnpublishedBook(): void {
// Create a new book.
$this->createBook();
// Create administrator user.
$administratorUser = $this->drupalCreateUser([
'administer blocks',
'administer nodes',
'bypass node access',
]);
$this->drupalLogin($administratorUser);
// Enable the block with "Show block only on book pages" mode.
$this->drupalPlaceBlock('book_navigation', ['block_mode' => 'book pages']);
// Unpublish book node.
$edit = ['status[value]' => FALSE];
$this->drupalGet('node/' . $this->book->id() . '/edit');
$this->submitForm($edit, 'Save');
// Test node page.
$this->drupalGet('node/' . $this->book->id());
// Unpublished book with "Show block only on book pages" book navigation
// settings.
$this->assertSession()->pageTextContains($this->book->label());
}
/**
* Tests that the book settings form can be saved without error.
*/
public function testSettingsForm(): void {
$this->drupalLogin($this->adminUser);
$this->drupalGet('admin/structure/book/settings');
$this->submitForm([], 'Save configuration');
}
}

View File

@@ -0,0 +1,224 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Functional;
use Drupal\Core\Url;
use Drupal\Core\Entity\EntityInterface;
/**
* Provides common functionality for Book test classes.
*/
trait BookTestTrait {
/**
* A book node.
*
* @var \Drupal\node\NodeInterface
*/
protected $book;
/**
* A user with permission to create and edit books.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $bookAuthor;
/**
* Creates a new book with a page hierarchy.
*
* @param array $edit
* (optional) Field data in an associative array. Changes the current input
* fields (where possible) to the values indicated. Defaults to an empty
* array.
*
* @return \Drupal\node\NodeInterface[]
*/
public function createBook($edit = []) {
// Create new book.
$this->drupalLogin($this->bookAuthor);
$this->book = $this->createBookNode('new', NULL, $edit);
$book = $this->book;
/*
* Add page hierarchy to book.
* Book
* |- Node 0
* |- Node 1
* |- Node 2
* |- Node 3
* |- Node 4
*/
$nodes = [];
// Node 0.
$nodes[] = $this->createBookNode($book->id(), NULL, $edit);
// Node 1.
$nodes[] = $this->createBookNode($book->id(), $nodes[0]->book['nid'], $edit);
// Node 2.
$nodes[] = $this->createBookNode($book->id(), $nodes[0]->book['nid'], $edit);
// Node 3.
$nodes[] = $this->createBookNode($book->id(), NULL, $edit);
// Node 4.
$nodes[] = $this->createBookNode($book->id(), NULL, $edit);
$this->drupalLogout();
return $nodes;
}
/**
* Checks the outline of sub-pages; previous, up, and next.
*
* Also checks the printer friendly version of the outline.
*
* @param \Drupal\Core\Entity\EntityInterface $node
* Node to check.
* @param $nodes
* Nodes that should be in outline.
* @param $previous
* Previous link node.
* @param $up
* Up link node.
* @param $next
* Next link node.
* @param array $breadcrumb
* The nodes that should be displayed in the breadcrumb.
*/
public function checkBookNode(EntityInterface $node, $nodes, $previous, $up, $next, array $breadcrumb) {
$this->drupalGet('node/' . $node->id());
// Check outline structure.
if ($nodes !== NULL) {
$book_navigation = $this->getSession()->getPage()->find('css', sprintf('nav[aria-labelledby="book-label-%s"] ul', $this->book->id()));
$this->assertNotNull($book_navigation);
$links = $book_navigation->findAll('css', 'a');
$this->assertCount(count($nodes), $links);
foreach ($nodes as $delta => $node) {
$link = $links[$delta];
$this->assertEquals($node->label(), $link->getText());
$this->assertEquals($node->toUrl()->toString(), $link->getAttribute('href'));
}
}
// Check previous, up, and next links.
if ($previous) {
$previous_element = $this->assertSession()->elementExists('named_exact', [
'link',
'Go to previous page',
]);
$this->assertEquals($previous->toUrl()->toString(), $previous_element->getAttribute('href'));
}
if ($up) {
$parent_element = $this->assertSession()->elementExists('named_exact', [
'link',
'Go to parent page',
]);
$this->assertEquals($up->toUrl()->toString(), $parent_element->getAttribute('href'));
}
if ($next) {
$next_element = $this->assertSession()->elementExists('named_exact', [
'link',
'Go to next page',
]);
$this->assertEquals($next->toUrl()->toString(), $next_element->getAttribute('href'));
}
// Compute the expected breadcrumb.
$expected_breadcrumb = [];
$expected_breadcrumb[] = Url::fromRoute('<front>')->toString();
foreach ($breadcrumb as $a_node) {
$expected_breadcrumb[] = $a_node->toUrl()->toString();
}
// Fetch links in the current breadcrumb.
$links = $this->xpath('//nav[@aria-labelledby="system-breadcrumb"]/ol/li/a');
$got_breadcrumb = [];
foreach ($links as $link) {
$got_breadcrumb[] = $link->getAttribute('href');
}
// Compare expected and got breadcrumbs.
$this->assertSame($expected_breadcrumb, $got_breadcrumb, 'The breadcrumb is correctly displayed on the page.');
// Check printer friendly version.
$this->drupalGet('book/export/html/' . $node->id());
$this->assertSession()->pageTextContains($node->label());
$this->assertSession()->responseContains($node->body->processed);
}
/**
* Creates a regular expression to check for the sub-nodes in the outline.
*
* @param array $nodes
* An array of nodes to check in outline.
*
* @return string
* A regular expression that locates sub-nodes of the outline.
*
* @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use
* methods from \Drupal\Tests\WebAssert instead.
*
* @see https://www.drupal.org/node/3325904
*/
public function generateOutlinePattern($nodes) {
@trigger_error(__METHOD__ . ' is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use methods from \Drupal\Tests\WebAssert instead. See https://www.drupal.org/node/3325904', E_USER_DEPRECATED);
$outline = '';
foreach ($nodes as $node) {
$outline .= '(node\/' . $node->id() . ')(.*?)(' . $node->label() . ')(.*?)';
}
return '/<nav role="navigation" aria-labelledby="book-label-' . $this->book->id() . '"(.*?)<ul(.*?)' . $outline . '<\/ul>/s';
}
/**
* Creates a book node.
*
* @param int|string $book_nid
* A book node ID or set to 'new' to create a new book.
* @param int|null $parent
* (optional) Parent book reference ID. Defaults to NULL.
* @param array $edit
* (optional) Field data in an associative array. Changes the current input
* fields (where possible) to the values indicated. Defaults to an empty
* array.
*
* @return \Drupal\node\NodeInterface
* The created node.
*/
public function createBookNode($book_nid, $parent = NULL, $edit = []) {
// $number does not use drupal_static as it should not be reset
// since it uniquely identifies each call to createBookNode().
// Used to ensure that when sorted nodes stay in same order.
static $number = 0;
$edit['title[0][value]'] = str_pad((string) $number, 2, '0', STR_PAD_LEFT) . ' - test node ' . $this->randomMachineName(10);
$edit['body[0][value]'] = 'test body ' . $this->randomMachineName(32) . ' ' . $this->randomMachineName(32);
$edit['book[bid]'] = $book_nid;
if ($parent !== NULL) {
$this->drupalGet('node/add/book');
$this->submitForm($edit, 'Change book (update list of parents)');
$edit['book[pid]'] = $parent;
$this->submitForm($edit, 'Save');
// Make sure the parent was flagged as having children.
$parent_node = \Drupal::entityTypeManager()->getStorage('node')->loadUnchanged($parent);
$this->assertNotEmpty($parent_node->book['has_children'], 'Parent node is marked as having children');
}
else {
$this->drupalGet('node/add/book');
$this->submitForm($edit, 'Save');
}
// Check to make sure the book node was created.
$node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
$this->assertNotNull(($node === FALSE ? NULL : $node), 'Book node found in database.');
$number++;
return $node;
}
}

View File

@@ -0,0 +1,95 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Functional\Comment;
use Drupal\comment\CommentInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\node\Entity\Node;
use Drupal\Tests\BrowserTestBase;
use Drupal\comment\Entity\Comment;
/**
* Tests visibility of comments on book pages.
*
* @group book
* @group legacy
*/
class CommentBookTest extends BrowserTestBase {
use CommentTestTrait;
/**
* Modules to install.
*
* @var array
*/
protected static $modules = ['book', 'comment'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Create comment field on book.
$this->addDefaultCommentField('node', 'book');
}
/**
* Tests comments in book export.
*/
public function testBookCommentPrint(): void {
$book_node = Node::create([
'type' => 'book',
'title' => 'Book title',
'body' => 'Book body',
]);
$book_node->book['bid'] = 'new';
$book_node->save();
$comment_subject = $this->randomMachineName(8);
$comment_body = $this->randomMachineName(8);
$comment = Comment::create([
'subject' => $comment_subject,
'comment_body' => $comment_body,
'entity_id' => $book_node->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'status' => CommentInterface::PUBLISHED,
]);
$comment->save();
$commenting_user = $this->drupalCreateUser([
'access printer-friendly version',
'access comments',
'post comments',
]);
$this->drupalLogin($commenting_user);
$this->drupalGet('node/' . $book_node->id());
$this->assertSession()->pageTextContains($comment_subject);
$this->assertSession()->pageTextContains($comment_body);
$this->assertSession()->pageTextContains('Add new comment');
// Ensure that the comment form subject field exists.
$this->assertSession()->fieldExists('subject[0][value]');
$this->drupalGet('book/export/html/' . $book_node->id());
$this->assertSession()->pageTextContains('Comments');
$this->assertSession()->pageTextContains($comment_subject);
$this->assertSession()->pageTextContains($comment_body);
$this->assertSession()->pageTextNotContains('Add new comment');
// Verify that the comment form subject field is not found.
$this->assertSession()->fieldNotExists('subject[0][value]');
}
}

View File

@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Functional;
use Drupal\Tests\system\Functional\Module\GenericModuleTestBase;
/**
* Generic module test for book.
*
* @group book
* @group legacy
*/
class GenericTest extends GenericModuleTestBase {}

View File

@@ -0,0 +1,75 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Functional\Migrate\d6;
use Drupal\Tests\migrate_drupal_ui\Functional\NoMultilingualReviewPageTestBase;
/**
* Tests Review page.
*
* @group book
* @group legacy
*/
class ReviewPageTest extends NoMultilingualReviewPageTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['book'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->loadFixture($this->getModulePath('book') . '/tests/fixtures/drupal6.php');
}
/**
* Tests the review page.
*/
public function testMigrateUpgradeReviewPage(): void {
$this->prepare();
// Start the upgrade process.
$this->submitCredentialForm();
$session = $this->assertSession();
$this->submitForm([], 'I acknowledge I may lose data. Continue anyway.');
$session->statusCodeEquals(200);
// Confirm that Book will be upgraded.
$session->elementExists('xpath', "//td[contains(@class, 'checked') and text() = 'Book']");
$session->elementNotExists('xpath', "//td[contains(@class, 'error') and text() = 'Book']");
}
/**
* {@inheritdoc}
*/
protected function getSourceBasePath() {
return __DIR__;
}
/**
* {@inheritdoc}
*/
protected function getAvailablePaths() {
return [];
}
/**
* {@inheritdoc}
*/
protected function getIncompletePaths() {
return [];
}
/**
* {@inheritdoc}
*/
protected function getMissingPaths() {
return [];
}
}

View File

@@ -0,0 +1,75 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Functional\Migrate\d7;
use Drupal\Tests\migrate_drupal_ui\Functional\NoMultilingualReviewPageTestBase;
/**
* Tests Review page.
*
* @group book
* @group legacy
*/
class ReviewPageTest extends NoMultilingualReviewPageTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['book'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->loadFixture($this->getModulePath('book') . '/tests/fixtures/drupal7.php');
}
/**
* Tests the review page.
*/
public function testMigrateUpgradeReviewPage(): void {
$this->prepare();
// Start the upgrade process.
$this->submitCredentialForm();
$session = $this->assertSession();
$this->submitForm([], 'I acknowledge I may lose data. Continue anyway.');
$session->statusCodeEquals(200);
// Confirm that Book will be upgraded.
$session->elementExists('xpath', "//td[contains(@class, 'checked') and text() = 'Book']");
$session->elementNotExists('xpath', "//td[contains(@class, 'error') and text() = 'Book']");
}
/**
* {@inheritdoc}
*/
protected function getSourceBasePath() {
return __DIR__;
}
/**
* {@inheritdoc}
*/
protected function getAvailablePaths() {
return [];
}
/**
* {@inheritdoc}
*/
protected function getIncompletePaths() {
return [];
}
/**
* {@inheritdoc}
*/
protected function getMissingPaths() {
return [];
}
}

View File

@@ -0,0 +1,166 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Functional\Views;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
/**
* Tests entity reference relationship data.
*
* @group book
* @group legacy
*
* @see book_views_data()
*/
class BookRelationshipTest extends ViewTestBase {
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = ['test_book_view'];
/**
* Modules to install.
*
* @var array
*/
protected static $modules = ['book_test_views', 'book', 'views'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* A book node.
*
* @var object
*/
protected $book;
/**
* A user with permission to create and edit books.
*
* @var object
*/
protected $bookAuthor;
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE, $modules = []): void {
parent::setUp($import_test_views, $modules);
// Create users.
$this->bookAuthor = $this->drupalCreateUser(
[
'create new books',
'create book content',
'edit own book content',
'add content to books',
]
);
ViewTestData::createTestViews(static::class, ['book_test_views']);
}
/**
* Creates a new book with a page hierarchy.
*/
protected function createBook() {
// Create new book.
$this->drupalLogin($this->bookAuthor);
$this->book = $this->createBookNode('new');
$book = $this->book;
$nodes = [];
// Node 0.
$nodes[] = $this->createBookNode($book->id());
// Node 1.
$nodes[] = $this->createBookNode($book->id(), $nodes[0]->book['nid']);
// Node 2.
$nodes[] = $this->createBookNode($book->id(), $nodes[1]->book['nid']);
// Node 3.
$nodes[] = $this->createBookNode($book->id(), $nodes[2]->book['nid']);
// Node 4.
$nodes[] = $this->createBookNode($book->id(), $nodes[3]->book['nid']);
// Node 5.
$nodes[] = $this->createBookNode($book->id(), $nodes[4]->book['nid']);
// Node 6.
$nodes[] = $this->createBookNode($book->id(), $nodes[5]->book['nid']);
// Node 7.
$nodes[] = $this->createBookNode($book->id(), $nodes[6]->book['nid']);
$this->drupalLogout();
return $nodes;
}
/**
* Creates a book node.
*
* @param int|string $book_nid
* A book node ID or set to 'new' to create a new book.
* @param int|null $parent
* (optional) Parent book reference ID. Defaults to NULL.
*
* @return \Drupal\node\NodeInterface
* The book node.
*/
protected function createBookNode($book_nid, $parent = NULL) {
// $number does not use drupal_static as it should not be reset
// since it uniquely identifies each call to createBookNode().
// Used to ensure that when sorted nodes stay in same order.
static $number = 0;
$edit = [];
$edit['title[0][value]'] = $number . ' - test node ' . $this->randomMachineName(10);
$edit['body[0][value]'] = 'test body ' . $this->randomMachineName(32) . ' ' . $this->randomMachineName(32);
$edit['book[bid]'] = $book_nid;
if ($parent !== NULL) {
$this->drupalGet('node/add/book');
$this->submitForm($edit, 'Change book (update list of parents)');
$edit['book[pid]'] = $parent;
$this->submitForm($edit, 'Save');
// Make sure the parent was flagged as having children.
$parent_node = \Drupal::entityTypeManager()->getStorage('node')->loadUnchanged($parent);
$this->assertNotEmpty($parent_node->book['has_children'], 'Parent node is marked as having children');
}
else {
$this->drupalGet('node/add/book');
$this->submitForm($edit, 'Save');
}
// Check to make sure the book node was created.
$node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
$this->assertNotNull(($node === FALSE ? NULL : $node), 'Book node found in database.');
$number++;
return $node;
}
/**
* Tests using the views relationship.
*/
public function testRelationship(): void {
// Create new book.
/** @var \Drupal\node\NodeInterface[] $nodes */
$nodes = $this->createBook();
for ($i = 0; $i < 8; $i++) {
$this->drupalGet('test-book/' . $nodes[$i]->id());
for ($j = 0; $j < $i; $j++) {
$this->assertSession()->linkExists($nodes[$j]->label());
}
}
}
}

View File

@@ -0,0 +1,192 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\FunctionalJavascript;
use Behat\Mink\Exception\ExpectationException;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\node\Entity\Node;
/**
* Tests Book javascript functionality.
*
* @group book
* @group legacy
*/
class BookJavascriptTest extends WebDriverTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['book'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Tests re-ordering of books.
*/
public function testBookOrdering(): void {
$book = Node::create([
'type' => 'book',
'title' => 'Book',
'book' => ['bid' => 'new'],
]);
$book->save();
$page1 = Node::create([
'type' => 'book',
'title' => '1st page',
'book' => ['bid' => $book->id(), 'pid' => $book->id(), 'weight' => 0],
]);
$page1->save();
$page2 = Node::create([
'type' => 'book',
'title' => '2nd page',
'book' => ['bid' => $book->id(), 'pid' => $book->id(), 'weight' => 1],
]);
$page2->save();
// Head to admin screen and attempt to re-order.
$this->drupalLogin($this->drupalCreateUser(['administer book outlines']));
$this->drupalGet('admin/structure/book/' . $book->id());
$page = $this->getSession()->getPage();
$weight_select1 = $page->findField("table[book-admin-{$page1->id()}][weight]");
$weight_select2 = $page->findField("table[book-admin-{$page2->id()}][weight]");
// Check that rows weight selects are hidden.
$this->assertFalse($weight_select1->isVisible());
$this->assertFalse($weight_select2->isVisible());
// Check that '2nd page' row is heavier than '1st page' row.
$this->assertGreaterThan($weight_select1->getValue(), $weight_select2->getValue());
// Check that '1st page' precedes the '2nd page'.
$this->assertOrderInPage(['1st page', '2nd page']);
// Check that the 'unsaved changes' text is not present in the message area.
$this->assertSession()->pageTextNotContains('You have unsaved changes.');
// Drag and drop the '1st page' row over the '2nd page' row.
// @todo Test also the reverse, '2nd page' over '1st page', when
// https://www.drupal.org/node/2769825 is fixed.
// @see https://www.drupal.org/node/2769825
$dragged = $this->xpath("//tr[@data-drupal-selector='edit-table-book-admin-{$page1->id()}']//a[@class='tabledrag-handle']")[0];
$target = $this->xpath("//tr[@data-drupal-selector='edit-table-book-admin-{$page2->id()}']//a[@class='tabledrag-handle']")[0];
$dragged->dragTo($target);
// Give javascript some time to manipulate the DOM.
$this->assertJsCondition('jQuery(".tabledrag-changed-warning").is(":visible")');
// Check that the 'unsaved changes' text appeared in the message area.
$this->assertSession()->pageTextContains('You have unsaved changes.');
// Check that '2nd page' page precedes the '1st page'.
$this->assertOrderInPage(['2nd page', '1st page']);
$this->submitForm([], 'Save book pages');
$this->assertSession()->pageTextContains(new FormattableMarkup('Updated book @book.', ['@book' => $book->getTitle()]));
// Check that page reordering was done in the backend for drag-n-drop.
$page1 = Node::load($page1->id());
$page2 = Node::load($page2->id());
$this->assertGreaterThan($page2->book['weight'], $page1->book['weight']);
// Check again that '2nd page' is on top after form submit in the UI.
$this->assertOrderInPage(['2nd page', '1st page']);
// Toggle row weight selects as visible.
$page->findButton('Show row weights')->click();
// Check that rows weight selects are visible.
$this->assertTrue($weight_select1->isVisible());
$this->assertTrue($weight_select2->isVisible());
// Check that '1st page' row became heavier than '2nd page' row.
$this->assertGreaterThan($weight_select2->getValue(), $weight_select1->getValue());
// Reverse again using the weight fields. Use the current values so the test
// doesn't rely on knowing the values in the select boxes.
$value1 = $weight_select1->getValue();
$value2 = $weight_select2->getValue();
$weight_select1->setValue($value2);
$weight_select2->setValue($value1);
// Toggle row weight selects back to hidden.
$page->findButton('Hide row weights')->click();
// Check that rows weight selects are hidden again.
$this->assertFalse($weight_select1->isVisible());
$this->assertFalse($weight_select2->isVisible());
$this->submitForm([], 'Save book pages');
$this->assertSession()->pageTextContains(new FormattableMarkup('Updated book @book.', ['@book' => $book->getTitle()]));
// Check that the '1st page' is first again.
$this->assertOrderInPage(['1st page', '2nd page']);
// Check that page reordering was done in the backend for manual weight
// field usage.
$page1 = Node::load($page1->id());
$page2 = Node::load($page2->id());
$this->assertGreaterThan($page2->book['weight'], $page1->book['weight']);
}
/**
* Asserts that several pieces of markup are in a given order in the page.
*
* @param string[] $items
* An ordered list of strings.
*
* @throws \Behat\Mink\Exception\ExpectationException
* When any of the given string is not found.
*
* @internal
*
* @todo Remove this once https://www.drupal.org/node/2817657 is committed.
*/
protected function assertOrderInPage(array $items): void {
$session = $this->getSession();
$text = $session->getPage()->getHtml();
$strings = [];
foreach ($items as $item) {
if (($pos = strpos($text, $item)) === FALSE) {
throw new ExpectationException("Cannot find '$item' in the page", $session->getDriver());
}
$strings[$pos] = $item;
}
ksort($strings);
$ordered = implode(', ', array_map(function ($item) {
return "'$item'";
}, $items));
$this->assertSame($items, array_values($strings), "Found strings, ordered as: $ordered.");
}
/**
* Tests book outline AJAX request.
*/
public function testBookAddOutline(): void {
$this->drupalLogin($this->drupalCreateUser(['create book content', 'create new books', 'add content to books']));
$this->drupalGet('node/add/book');
$assert_session = $this->assertSession();
$session = $this->getSession();
$page = $session->getPage();
$page->find('css', '#edit-book')->click();
$book_select = $page->findField("book[bid]");
$book_select->setValue('new');
$assert_session->waitForText('This will be the top-level page in this book.');
$assert_session->pageTextContains('This will be the top-level page in this book.');
$assert_session->pageTextNotContains('No book selected.');
$book_select->setValue(0);
$assert_session->waitForText('No book selected.');
$assert_session->pageTextContains('No book selected.');
$assert_session->pageTextNotContains('This will be the top-level page in this book.');
}
}

View File

@@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Kernel\Block;
use Drupal\block\Entity\Block;
use Drupal\Tests\SchemaCheckTestTrait;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests the block config schema.
*
* @group book
*/
class BlockConfigSchemaTest extends KernelTestBase {
use SchemaCheckTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'block',
'book',
'node',
// \Drupal\block\Entity\Block->preSave() calls system_region_list().
'system',
'user',
];
/**
* The typed config manager.
*
* @var \Drupal\Core\Config\TypedConfigManagerInterface
*/
protected $typedConfig;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->typedConfig = \Drupal::service('config.typed');
$this->installEntitySchema('node');
$this->installSchema('book', ['book']);
$this->container->get('theme_installer')->install(['stark']);
}
/**
* Tests the block config schema for block plugins.
*/
public function testBlockConfigSchema(): void {
$id = strtolower($this->randomMachineName());
$block = Block::create([
'id' => $id,
'theme' => 'stark',
'weight' => 00,
'status' => TRUE,
'region' => 'content',
'plugin' => 'book_navigation',
'settings' => [
'label' => $this->randomMachineName(),
'provider' => 'system',
'label_display' => FALSE,
],
'visibility' => [],
]);
$block->save();
$config = $this->config("block.block.$id");
$this->assertEquals($id, $config->get('id'));
$this->assertConfigSchema($this->typedConfig, $config->getName(), $config->get());
}
}

View File

@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
/**
* Test installation of Book module.
*
* @group book
* @group legacy
*/
class BookInstallTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'node',
'system',
];
/**
* Tests Book install with pre-existing content type.
*
* Tests that Book module can be installed if content type with machine name
* 'book' already exists.
*/
public function testBookInstallWithPreexistingContentType(): void {
// Create a 'book' content type.
NodeType::create([
'type' => 'book',
'name' => 'Book',
])->save();
// Install the Book module. Using the module installer service ensures that
// all the install rituals, including default and optional configuration
// import, are performed.
$status = $this->container->get('module_installer')->install(['book']);
$this->assertTrue($status, 'Book module installed successfully');
}
}

View File

@@ -0,0 +1,346 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Kernel;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Url;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUser;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\Routing\Route;
/**
* Tests multilingual books.
*
* @group book
* @group legacy
*/
class BookMultilingualTest extends KernelTestBase {
use UserCreationTrait;
/**
* The translation langcode.
*/
const LANGCODE = 'de';
/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'user',
'node',
'field',
'text',
'book',
'language',
'content_translation',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Create the translation language.
$this->installConfig(['language']);
ConfigurableLanguage::createFromLangcode(self::LANGCODE)->save();
// Set up language negotiation.
$config = $this->config('language.types');
$config->set('configurable', [
LanguageInterface::TYPE_INTERFACE,
LanguageInterface::TYPE_CONTENT,
]);
// The language being tested should only be available as the content
// language so subsequent tests catch errors where the interface language
// is used instead of the content language. For this, the interface
// language is set to the user language and ::setCurrentLanguage() will
// set the user language to the language not being tested.
$config->set('negotiation', [
LanguageInterface::TYPE_INTERFACE => [
'enabled' => [LanguageNegotiationUser::METHOD_ID => 0],
],
LanguageInterface::TYPE_CONTENT => [
'enabled' => [LanguageNegotiationUrl::METHOD_ID => 0],
],
]);
$config->save();
$config = $this->config('language.negotiation');
$config->set('url.source', LanguageNegotiationUrl::CONFIG_DOMAIN);
$config->set('url.domains', [
'en' => 'en.book.test.domain',
self::LANGCODE => self::LANGCODE . '.book.test.domain',
]);
$config->save();
$this->container->get('kernel')->rebuildContainer();
$this->installEntitySchema('node');
$this->installEntitySchema('user');
$this->installSchema('book', ['book']);
$this->installSchema('node', ['node_access']);
$this->installConfig(['node', 'book', 'field']);
$node_type = NodeType::create([
'type' => $this->randomMachineName(),
'name' => $this->randomString(),
]);
$node_type->save();
$this->container->get('content_translation.manager')->setEnabled('node', $node_type->id(), TRUE);
$book_config = $this->config('book.settings');
$allowed_types = $book_config->get('allowed_types');
$allowed_types[] = $node_type->id();
$book_config->set('allowed_types', $allowed_types)->save();
// To test every possible combination of root-child / child-child, two
// trees are needed. The first level below the root needs to have two
// leaves and similarly a second level is needed with two-two leaves each:
//
// 1
// / \
// / \
// 2 3
// / \ / \
// / \ / \
// 4 5 6 7
//
// These are the actual node IDs, these are enforced as auto increment is
// not reliable.
//
// Similarly, the second tree root is node 8, the first two leaves are
// 9 and 10, the third level is 11, 12, 13, 14.
for ($root = 1; $root <= 8; $root += 7) {
for ($i = 0; $i <= 6; $i++) {
/** @var \Drupal\node\NodeInterface $node */
$node = Node::create([
'title' => $this->randomString(),
'type' => $node_type->id(),
]);
$node->addTranslation(self::LANGCODE, [
'title' => $this->randomString(),
]);
switch ($i) {
case 0:
$node->book['bid'] = 'new';
$node->book['pid'] = 0;
$node->book['depth'] = 1;
break;
case 1:
case 2:
$node->book['bid'] = $root;
$node->book['pid'] = $root;
$node->book['depth'] = 2;
break;
case 3:
case 4:
$node->book['bid'] = $root;
$node->book['pid'] = $root + 1;
$node->book['depth'] = 3;
break;
case 5:
case 6:
$node->book['bid'] = $root;
$node->book['pid'] = $root + 2;
$node->book['depth'] = 3;
break;
}
// This is necessary to make the table of contents consistent across
// test runs.
$node->book['weight'] = $i;
$node->nid->value = $root + $i;
$node->enforceIsNew();
$node->save();
}
}
\Drupal::currentUser()->setAccount($this->createUser(['access content']));
}
/**
* Tests various book manager methods return correct translations.
*
* @dataProvider langcodesProvider
*/
public function testMultilingualBookManager(string $langcode): void {
$this->setCurrentLanguage($langcode);
/** @var \Drupal\book\BookManagerInterface $bm */
$bm = $this->container->get('book.manager');
$books = $bm->getAllBooks();
$this->assertNotEmpty($books);
foreach ($books as $book) {
$bid = (int) $book['bid'];
$build = $bm->bookTreeOutput($bm->bookTreeAllData($bid));
$items = $build['#items'];
$this->assertBookItemIsCorrectlyTranslated($items[$bid], $langcode);
$this->assertBookItemIsCorrectlyTranslated($items[$bid]['below'][$bid + 1], $langcode);
$this->assertBookItemIsCorrectlyTranslated($items[$bid]['below'][$bid + 1]['below'][$bid + 3], $langcode);
$this->assertBookItemIsCorrectlyTranslated($items[$bid]['below'][$bid + 1]['below'][$bid + 4], $langcode);
$this->assertBookItemIsCorrectlyTranslated($items[$bid]['below'][$bid + 2], $langcode);
$this->assertBookItemIsCorrectlyTranslated($items[$bid]['below'][$bid + 2]['below'][$bid + 5], $langcode);
$this->assertBookItemIsCorrectlyTranslated($items[$bid]['below'][$bid + 2]['below'][$bid + 6], $langcode);
$toc = $bm->getTableOfContents($bid, 4);
// Root entry does not have an indent.
$this->assertToCEntryIsCorrectlyTranslated($toc, $langcode, $bid, '');
// The direct children of the root have one indent.
$this->assertToCEntryIsCorrectlyTranslated($toc, $langcode, $bid + 1, '--');
$this->assertToCEntryIsCorrectlyTranslated($toc, $langcode, $bid + 2, '--');
// Their children have two indents.
$this->assertToCEntryIsCorrectlyTranslated($toc, $langcode, $bid + 3, '----');
$this->assertToCEntryIsCorrectlyTranslated($toc, $langcode, $bid + 4, '----');
$this->assertToCEntryIsCorrectlyTranslated($toc, $langcode, $bid + 5, '----');
$this->assertToCEntryIsCorrectlyTranslated($toc, $langcode, $bid + 6, '----');
// $bid might be a string.
$this->assertSame([$bid + 0, $bid + 1, $bid + 3, $bid + 4, $bid + 2, $bid + 5, $bid + 6], array_keys($toc));
}
}
/**
* Tests various book breadcrumb builder methods return correct translations.
*
* @dataProvider langcodesProvider
*/
public function testMultilingualBookBreadcrumbBuilder(string $langcode): void {
$this->setCurrentLanguage($langcode);
// Test a level 3 node.
$nid = 7;
/** @var \Drupal\node\NodeInterface $node */
$node = Node::load($nid);
$route = new Route('/node/{node}');
$route_match = new RouteMatch('entity.node.canonical', $route, ['node' => $node], ['node' => $nid]);
/** @var \Drupal\book\BookBreadcrumbBuilder $bbb */
$bbb = $this->container->get('book.breadcrumb');
$links = $bbb->build($route_match)->getLinks();
$link = array_shift($links);
$rendered_link = (string) Link::fromTextAndUrl($link->getText(), $link->getUrl())->toString();
$this->assertStringContainsString("http://$langcode.book.test.domain/", $rendered_link);
$link = array_shift($links);
$this->assertNodeLinkIsCorrectlyTranslated(1, $link->getText(), $link->getUrl(), $langcode);
$link = array_shift($links);
$this->assertNodeLinkIsCorrectlyTranslated(3, $link->getText(), $link->getUrl(), $langcode);
$this->assertEmpty($links);
}
/**
* Tests the book export returns correct translations.
*
* @dataProvider langcodesProvider
*/
public function testMultilingualBookExport(string $langcode): void {
$this->setCurrentLanguage($langcode);
/** @var \Drupal\book\BookExport $be */
$be = $this->container->get('book.export');
/** @var \Drupal\book\BookManagerInterface $bm */
$bm = $this->container->get('book.manager');
$books = $bm->getAllBooks();
$this->assertNotEmpty($books);
foreach ($books as $book) {
$contents = $be->bookExportHtml(Node::load($book['bid']))['#contents'][0];
$this->assertSame($contents["#node"]->language()->getId(), $langcode);
$this->assertSame($contents["#children"][0]["#node"]->language()->getId(), $langcode);
$this->assertSame($contents["#children"][1]["#node"]->language()->getId(), $langcode);
$this->assertSame($contents["#children"][0]["#children"][0]["#node"]->language()->getId(), $langcode);
$this->assertSame($contents["#children"][0]["#children"][1]["#node"]->language()->getId(), $langcode);
$this->assertSame($contents["#children"][1]["#children"][0]["#node"]->language()->getId(), $langcode);
$this->assertSame($contents["#children"][1]["#children"][1]["#node"]->language()->getId(), $langcode);
}
}
/**
* Data provider for ::testMultilingualBooks().
*/
public static function langcodesProvider() {
return [
[self::LANGCODE],
['en'],
];
}
/**
* Sets the current language.
*
* @param string $langcode
* The langcode. The content language will be set to this using the
* appropriate domain while the user language will be set to something
* else so subsequent tests catch errors where the interface language
* is used instead of the content language.
*/
protected function setCurrentLanguage(string $langcode): void {
$request = Request::create("http://$langcode.book.test.domain/");
$request->setSession(new Session(new MockArraySessionStorage()));
\Drupal::requestStack()->push($request);
$language_manager = $this->container->get('language_manager');
$language_manager->reset();
$current_user = \Drupal::currentUser();
$languages = $language_manager->getLanguages();
unset($languages[$langcode]);
$current_user->getAccount()->set('preferred_langcode', reset($languages)->getId());
$this->assertNotSame($current_user->getPreferredLangcode(), $langcode);
}
/**
* Asserts a book item is correctly translated.
*
* @param array $item
* A book tree item.
* @param string $langcode
* The language code for the requested translation.
*
* @internal
*/
protected function assertBookItemIsCorrectlyTranslated(array $item, string $langcode): void {
$this->assertNodeLinkIsCorrectlyTranslated((int) $item['original_link']['nid'], $item['title'], $item['url'], $langcode);
}
/**
* Asserts a node link is correctly translated.
*
* @param int $nid
* The node id.
* @param string $title
* The expected title.
* @param \Drupal\Core\Url $url
* The URL being tested.
* @param string $langcode
* The language code.
*
* @internal
*/
protected function assertNodeLinkIsCorrectlyTranslated(int $nid, string $title, Url $url, string $langcode): void {
$node = Node::load($nid);
$this->assertSame($node->getTranslation($langcode)->label(), $title);
$rendered_link = (string) Link::fromTextAndUrl($title, $url)->toString();
$this->assertStringContainsString("http://$langcode.book.test.domain/node/$nid", $rendered_link);
}
/**
* Asserts one entry in the table of contents is correct.
*
* @param array $toc
* The entire table of contents array.
* @param string $langcode
* The language code for the requested translation.
* @param int $nid
* The node ID.
* @param string $indent
* The indentation before the actual table of contents label.
*
* @internal
*/
protected function assertToCEntryIsCorrectlyTranslated(array $toc, string $langcode, int $nid, string $indent): void {
$node = Node::load($nid);
$node_label = $node->getTranslation($langcode)->label();
$this->assertSame($indent . ' ' . $node_label, $toc[$nid]);
}
}

View File

@@ -0,0 +1,111 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Kernel;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests that the Book module handles pending revisions correctly.
*
* @group book
* @group legacy
*/
class BookPendingRevisionTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'system',
'user',
'field',
'filter',
'text',
'node',
'book',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('user');
$this->installEntitySchema('node');
$this->installSchema('book', ['book']);
$this->installSchema('node', ['node_access']);
$this->installConfig(['node', 'book', 'field']);
}
/**
* Tests pending revision handling for books.
*/
public function testBookWithPendingRevisions(): void {
$content_type = NodeType::create([
'type' => $this->randomMachineName(),
'name' => $this->randomString(),
]);
$content_type->save();
$book_config = $this->config('book.settings');
$allowed_types = $book_config->get('allowed_types');
$allowed_types[] = $content_type->id();
$book_config->set('allowed_types', $allowed_types)->save();
// Create two top-level books a child.
$book_1 = Node::create(['title' => $this->randomString(), 'type' => $content_type->id()]);
$book_1->book['bid'] = 'new';
$book_1->save();
$book_1_child = Node::create(['title' => $this->randomString(), 'type' => $content_type->id()]);
$book_1_child->book['bid'] = $book_1->id();
$book_1_child->book['pid'] = $book_1->id();
$book_1_child->save();
$book_2 = Node::create(['title' => $this->randomString(), 'type' => $content_type->id()]);
$book_2->book['bid'] = 'new';
$book_2->save();
$child = Node::create(['title' => $this->randomString(), 'type' => $content_type->id()]);
$child->book['bid'] = $book_1->id();
$child->book['pid'] = $book_1->id();
$child->save();
// Try to move the child to a different book while saving it as a pending
// revision.
/** @var \Drupal\book\BookManagerInterface $book_manager */
$book_manager = $this->container->get('book.manager');
// Check that the API doesn't allow us to change the book outline for
// pending revisions.
$child->book['bid'] = $book_2->id();
$child->setNewRevision(TRUE);
$child->isDefaultRevision(FALSE);
$this->assertFalse($book_manager->updateOutline($child), 'A pending revision can not change the book outline.');
// Check that the API doesn't allow us to change the book parent for
// pending revisions.
$child = \Drupal::entityTypeManager()->getStorage('node')->loadUnchanged($child->id());
$child->book['pid'] = $book_1_child->id();
$child->setNewRevision(TRUE);
$child->isDefaultRevision(FALSE);
$this->assertFalse($book_manager->updateOutline($child), 'A pending revision can not change the book outline.');
// Check that the API doesn't allow us to change the book weight for
// pending revisions.
$child = \Drupal::entityTypeManager()->getStorage('node')->loadUnchanged($child->id());
$child->book['weight'] = 2;
$child->setNewRevision(TRUE);
$child->isDefaultRevision(FALSE);
$this->assertFalse($book_manager->updateOutline($child), 'A pending revision can not change the book outline.');
}
}

View File

@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Kernel;
use Drupal\book\Form\BookSettingsForm;
use Drupal\Core\Form\FormState;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
/**
* @covers \Drupal\book\Form\BookSettingsForm
* @group book
* @group legacy
*/
class BookSettingsFormTest extends KernelTestBase {
use ContentTypeCreationTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'book',
'field',
'node',
'system',
'text',
'user',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig(['book', 'node']);
$this->createContentType(['type' => 'chapter']);
$this->createContentType(['type' => 'page']);
}
/**
* Tests that submitted values are processed and saved correctly.
*/
public function testConfigValuesSavedCorrectly(): void {
$form_state = new FormState();
$form_state->setValues([
'book_allowed_types' => ['page', 'chapter', ''],
'book_child_type' => 'page',
]);
$this->container->get('form_builder')->submitForm(BookSettingsForm::class, $form_state);
$config = $this->config('book.settings');
$this->assertSame(['chapter', 'page'], $config->get('allowed_types'));
$this->assertSame('page', $config->get('child_type'));
}
}

View File

@@ -0,0 +1,105 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Kernel;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests that the Book module cannot be uninstalled if books exist.
*
* @group book
* @group legacy
*/
class BookUninstallTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'system',
'user',
'field',
'filter',
'text',
'node',
'book',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('user');
$this->installEntitySchema('node');
$this->installSchema('book', ['book']);
$this->installSchema('node', ['node_access']);
$this->installConfig(['node', 'book', 'field']);
// For uninstall to work.
$this->installSchema('user', ['users_data']);
}
/**
* Tests the book_system_info_alter() method.
*/
public function testBookUninstall(): void {
// No nodes exist.
$validation_reasons = \Drupal::service('module_installer')->validateUninstall(['book']);
$this->assertEquals([], $validation_reasons, 'The book module is not required.');
$content_type = NodeType::create([
'type' => $this->randomMachineName(),
'name' => $this->randomString(),
]);
$content_type->save();
$book_config = $this->config('book.settings');
$allowed_types = $book_config->get('allowed_types');
$allowed_types[] = $content_type->id();
$book_config->set('allowed_types', $allowed_types)->save();
$node = Node::create(['title' => $this->randomString(), 'type' => $content_type->id()]);
$node->book['bid'] = 'new';
$node->save();
// One node in a book but not of type book.
$validation_reasons = \Drupal::service('module_installer')->validateUninstall(['book']);
$this->assertEquals(['To uninstall Book, delete all content that is part of a book'], $validation_reasons['book']);
$book_node = Node::create(['title' => $this->randomString(), 'type' => 'book']);
$book_node->book['bid'] = FALSE;
$book_node->save();
// Two nodes, one in a book but not of type book and one book node (which is
// not in a book).
$validation_reasons = \Drupal::service('module_installer')->validateUninstall(['book']);
$this->assertEquals(['To uninstall Book, delete all content that is part of a book'], $validation_reasons['book']);
$node->delete();
// One node of type book but not actually part of a book.
$validation_reasons = \Drupal::service('module_installer')->validateUninstall(['book']);
$this->assertEquals(['To uninstall Book, delete all content that has the Book content type'], $validation_reasons['book']);
$book_node->delete();
// No nodes exist therefore the book module is not required.
$module_data = \Drupal::service('extension.list.module')->getList();
$this->assertFalse(isset($module_data['book']->info['required']), 'The book module is not required.');
$node = Node::create(['title' => $this->randomString(), 'type' => $content_type->id()]);
$node->save();
// One node exists but is not part of a book therefore the book module is
// not required.
$validation_reasons = \Drupal::service('module_installer')->validateUninstall(['book']);
$this->assertEquals([], $validation_reasons, 'The book module is not required.');
// Uninstall the Book module and check the node type is deleted.
\Drupal::service('module_installer')->uninstall(['book']);
$this->assertNull(NodeType::load('book'), "The book node type does not exist.");
}
}

View File

@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Kernel\Migrate\d6;
use Drupal\Tests\SchemaCheckTestTrait;
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
/**
* Upgrade variables to book.settings.yml.
*
* @group book
* @group legacy
*/
class MigrateBookConfigsTest extends MigrateDrupal6TestBase {
use SchemaCheckTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = ['book'];
/**
* Gets the path to the fixture file.
*/
protected function getFixtureFilePath() {
return __DIR__ . '/../../../../fixtures/drupal6.php';
}
/**
* Data provider for testBookSettings().
*
* @return array
* The data for each test scenario.
*/
public static function providerBookSettings() {
return [
// d6_book_settings was renamed to book_settings, but use the old alias to
// prove that it works.
// @see book_migration_plugins_alter()
['d6_book_settings'],
['book_settings'],
];
}
/**
* Tests migration of book variables to book.settings.yml.
*
* @dataProvider providerBookSettings
*/
public function testBookSettings($migration_id): void {
$this->executeMigration($migration_id);
$config = $this->config('book.settings');
$this->assertSame('book', $config->get('child_type'));
$this->assertSame('book pages', $config->get('block.navigation.mode'));
$this->assertSame(['book'], $config->get('allowed_types'));
$this->assertConfigSchema(\Drupal::service('config.typed'), 'book.settings', $config->get());
}
}

View File

@@ -0,0 +1,108 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Kernel\Migrate\d6;
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
use Drupal\node\Entity\Node;
/**
* Upgrade book structure.
*
* @group book
* @group legacy
*/
class MigrateBookTest extends MigrateDrupal6TestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['book', 'node'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installSchema('book', ['book']);
$this->installSchema('node', ['node_access']);
$this->migrateUsers(FALSE);
$this->installConfig(['node']);
$this->executeMigrations([
'd6_node_settings',
'd6_node_type',
'd6_node',
'd6_book',
]);
}
/**
* Gets the path to the fixture file.
*/
protected function getFixtureFilePath() {
return __DIR__ . '/../../../../fixtures/drupal6.php';
}
/**
* Tests the Drupal 6 book structure to Drupal 8 migration.
*/
public function testBook(): void {
$nodes = Node::loadMultiple([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
$this->assertSame('1', $nodes[1]->book['bid']);
$this->assertSame('0', $nodes[1]->book['pid']);
$this->assertSame('1', $nodes[2]->book['bid']);
$this->assertSame('1', $nodes[2]->book['pid']);
$this->assertSame('1', $nodes[3]->book['bid']);
$this->assertSame('1', $nodes[3]->book['pid']);
$this->assertSame('1', $nodes[4]->book['bid']);
$this->assertSame('3', $nodes[4]->book['pid']);
$this->assertSame('1', $nodes[5]->book['bid']);
$this->assertSame('3', $nodes[5]->book['pid']);
$this->assertSame('6', $nodes[6]->book['bid']);
$this->assertSame('0', $nodes[6]->book['pid']);
$this->assertSame('6', $nodes[7]->book['bid']);
$this->assertSame('6', $nodes[7]->book['pid']);
$this->assertSame('6', $nodes[8]->book['bid']);
$this->assertSame('6', $nodes[8]->book['pid']);
$this->assertSame('6', $nodes[9]->book['bid']);
$this->assertSame('8', $nodes[9]->book['pid']);
$this->assertSame('6', $nodes[10]->book['bid']);
$this->assertSame('8', $nodes[10]->book['pid']);
$tree = \Drupal::service('book.manager')->bookTreeAllData(1);
$this->assertSame('1', $tree['50000 Birds 1']['link']['nid']);
$this->assertSame('2', $tree['50000 Birds 1']['below']['50000 Emu 2']['link']['nid']);
$this->assertSame([], $tree['50000 Birds 1']['below']['50000 Emu 2']['below']);
$this->assertSame('3', $tree['50000 Birds 1']['below']['50000 Parrots 3']['link']['nid']);
$this->assertSame('4', $tree['50000 Birds 1']['below']['50000 Parrots 3']['below']['50000 Kea 4']['link']['nid']);
$this->assertSame([], $tree['50000 Birds 1']['below']['50000 Parrots 3']['below']['50000 Kea 4']['below']);
$this->assertSame('5', $tree['50000 Birds 1']['below']['50000 Parrots 3']['below']['50000 Kakapo 5']['link']['nid']);
$this->assertSame([], $tree['50000 Birds 1']['below']['50000 Parrots 3']['below']['50000 Kakapo 5']['below']);
$tree = \Drupal::service('book.manager')->bookTreeAllData(6);
$this->assertSame('6', $tree['50000 Tree 6']['link']['nid']);
$this->assertSame('7', $tree['50000 Tree 6']['below']['50000 Rimu 7']['link']['nid']);
$this->assertSame([], $tree['50000 Tree 6']['below']['50000 Rimu 7']['below']);
$this->assertSame('8', $tree['50000 Tree 6']['below']['50000 Oaks 8']['link']['nid']);
$this->assertSame('9', $tree['50000 Tree 6']['below']['50000 Oaks 8']['below']['50000 Cork oak 9']['link']['nid']);
$this->assertSame([], $tree['50000 Tree 6']['below']['50000 Oaks 8']['below']['50000 Cork oak 9']['below']);
$this->assertSame('10', $tree['50000 Tree 6']['below']['50000 Oaks 8']['below']['50000 White oak 10']['link']['nid']);
$this->assertSame([], $tree['50000 Tree 6']['below']['50000 Oaks 8']['below']['50000 White oak 10']['below']);
// Set the d6_book migration to update and re run the migration.
$id_map = $this->migration->getIdMap();
$id_map->prepareUpdate();
$this->executeMigration('d6_book');
}
}

View File

@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Kernel\Migrate\d7;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
use Drupal\Tests\SchemaCheckTestTrait;
/**
* Tests the migration of Book settings.
*
* @group book
* @group legacy
*/
class MigrateBookConfigsTest extends MigrateDrupal7TestBase {
use SchemaCheckTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = ['book', 'node'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->executeMigration('book_settings');
}
/**
* Gets the path to the fixture file.
*/
protected function getFixtureFilePath() {
return __DIR__ . '/../../../../fixtures/drupal7.php';
}
/**
* Tests migration of book variables to book.settings.yml.
*/
public function testBookSettings(): void {
$config = $this->config('book.settings');
$this->assertSame('book', $config->get('child_type'));
$this->assertSame('all pages', $config->get('block.navigation.mode'));
$this->assertSame(['book'], $config->get('allowed_types'));
$this->assertConfigSchema(\Drupal::service('config.typed'), 'book.settings', $config->get());
}
}

View File

@@ -0,0 +1,111 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Kernel\Migrate\d7;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
use Drupal\node\Entity\Node;
/**
* Tests migration of book structures from Drupal 7.
*
* @group book
* @group legacy
*/
class MigrateBookTest extends MigrateDrupal7TestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'book',
'menu_ui',
'node',
'text',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installSchema('book', ['book']);
$this->installSchema('node', ['node_access']);
$this->migrateUsers(FALSE);
$this->migrateContentTypes();
$this->executeMigrations([
'd7_node',
'd7_book',
]);
}
/**
* Gets the path to the fixture file.
*/
protected function getFixtureFilePath() {
return __DIR__ . '/../../../../fixtures/drupal7.php';
}
/**
* Tests the Drupal 7 book structure to Drupal 8 migration.
*/
public function testBook(): void {
$nodes = Node::loadMultiple([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
$this->assertSame('1', $nodes[1]->book['bid']);
$this->assertSame('0', $nodes[1]->book['pid']);
$this->assertSame('1', $nodes[2]->book['bid']);
$this->assertSame('1', $nodes[2]->book['pid']);
$this->assertSame('1', $nodes[3]->book['bid']);
$this->assertSame('1', $nodes[3]->book['pid']);
$this->assertSame('1', $nodes[4]->book['bid']);
$this->assertSame('3', $nodes[4]->book['pid']);
$this->assertSame('1', $nodes[5]->book['bid']);
$this->assertSame('3', $nodes[5]->book['pid']);
$this->assertSame('6', $nodes[6]->book['bid']);
$this->assertSame('0', $nodes[6]->book['pid']);
$this->assertSame('6', $nodes[7]->book['bid']);
$this->assertSame('6', $nodes[7]->book['pid']);
$this->assertSame('6', $nodes[8]->book['bid']);
$this->assertSame('6', $nodes[8]->book['pid']);
$this->assertSame('6', $nodes[9]->book['bid']);
$this->assertSame('8', $nodes[9]->book['pid']);
$this->assertSame('6', $nodes[10]->book['bid']);
$this->assertSame('8', $nodes[10]->book['pid']);
$tree = \Drupal::service('book.manager')->bookTreeAllData(1);
$this->assertSame('1', $tree['50000 Birds 1']['link']['nid']);
$this->assertSame('2', $tree['50000 Birds 1']['below']['50000 Emu 2']['link']['nid']);
$this->assertSame([], $tree['50000 Birds 1']['below']['50000 Emu 2']['below']);
$this->assertSame('3', $tree['50000 Birds 1']['below']['50000 Parrots 3']['link']['nid']);
$this->assertSame('4', $tree['50000 Birds 1']['below']['50000 Parrots 3']['below']['50000 Kea 4']['link']['nid']);
$this->assertSame([], $tree['50000 Birds 1']['below']['50000 Parrots 3']['below']['50000 Kea 4']['below']);
$this->assertSame('5', $tree['50000 Birds 1']['below']['50000 Parrots 3']['below']['50000 Kakapo 5']['link']['nid']);
$this->assertSame([], $tree['50000 Birds 1']['below']['50000 Parrots 3']['below']['50000 Kakapo 5']['below']);
$tree = \Drupal::service('book.manager')->bookTreeAllData(6);
$this->assertSame('6', $tree['50000 Tree 6']['link']['nid']);
$this->assertSame('7', $tree['50000 Tree 6']['below']['50000 Rimu 7']['link']['nid']);
$this->assertSame([], $tree['50000 Tree 6']['below']['50000 Rimu 7']['below']);
$this->assertSame('8', $tree['50000 Tree 6']['below']['50000 Oaks 8']['link']['nid']);
$this->assertSame('9', $tree['50000 Tree 6']['below']['50000 Oaks 8']['below']['50000 Cork oak 9']['link']['nid']);
$this->assertSame([], $tree['50000 Tree 6']['below']['50000 Oaks 8']['below']['50000 Cork oak 9']['below']);
$this->assertSame('10', $tree['50000 Tree 6']['below']['50000 Oaks 8']['below']['50000 White oak 10']['link']['nid']);
$this->assertSame([], $tree['50000 Tree 6']['below']['50000 Oaks 8']['below']['50000 White oak 10']['below']);
// Set the d7_book migration to update and re run the migration.
$id_map = $this->migration->getIdMap();
$id_map->prepareUpdate();
$this->executeMigration('d7_book');
}
}

View File

@@ -0,0 +1,89 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Kernel\Plugin\migrate\source;
use Drupal\Tests\migrate\Kernel\MigrateSqlSourceTestBase;
// cspell:ignore mlid plid
/**
* @covers \Drupal\book\Plugin\migrate\source\Book
* @group book
* @group legacy
*/
class BookTest extends MigrateSqlSourceTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['book', 'migrate_drupal', 'node'];
/**
* {@inheritdoc}
*/
public static function providerSource() {
$tests = [];
// The source data.
$tests[0]['source_data']['book'] = [
[
'mlid' => '1',
'nid' => '4',
'bid' => '4',
],
];
$tests[0]['source_data']['menu_links'] = [
[
'menu_name' => 'book-toc-1',
'mlid' => '1',
'plid' => '0',
'link_path' => 'node/4',
'router_path' => 'node/%',
'link_title' => 'Test top book title',
'options' => 'a:0:{}',
'module' => 'book',
'hidden' => '0',
'external' => '0',
'has_children' => '1',
'expanded' => '0',
'weight' => '-10',
'depth' => '1',
'customized' => '0',
'p1' => '1',
'p2' => '0',
'p3' => '0',
'p4' => '0',
'p5' => '0',
'p6' => '0',
'p7' => '0',
'p8' => '0',
'p9' => '0',
'updated' => '0',
],
];
// The expected results.
$tests[0]['expected_data'] = [
[
'nid' => '4',
'bid' => '4',
'mlid' => '1',
'plid' => '0',
'weight' => '-10',
'p1' => '1',
'p2' => '0',
'p3' => '0',
'p4' => '0',
'p5' => '0',
'p6' => '0',
'p7' => '0',
'p8' => '0',
'p9' => '0',
],
];
return $tests;
}
}

View File

@@ -0,0 +1,142 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Unit;
use Drupal\book\BookManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\book\BookManager
* @group book
* @group legacy
*/
class BookManagerTest extends UnitTestCase {
/**
* The mocked entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManager|\PHPUnit\Framework\MockObject\MockObject
*/
protected $entityTypeManager;
/**
* The mocked language manager.
*
* @var \Drupal\Core\Language\LanguageManager|\PHPUnit\Framework\MockObject\MockObject
*/
protected $languageManager;
/**
* The mocked entity repository.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $entityRepository;
/**
* The mocked config factory.
*
* @var \Drupal\Core\Config\ConfigFactory|\PHPUnit\Framework\MockObject\MockObject
*/
protected $configFactory;
/**
* The mocked translation manager.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $translation;
/**
* The mocked renderer.
*
* @var \Drupal\Core\Render\RendererInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $renderer;
/**
* The tested book manager.
*
* @var \Drupal\book\BookManager
*/
protected $bookManager;
/**
* Book outline storage.
*
* @var \Drupal\book\BookOutlineStorageInterface
*/
protected $bookOutlineStorage;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
$this->translation = $this->getStringTranslationStub();
$this->configFactory = $this->getConfigFactoryStub([]);
$this->bookOutlineStorage = $this->createMock('Drupal\book\BookOutlineStorageInterface');
$this->renderer = $this->createMock('\Drupal\Core\Render\RendererInterface');
$this->languageManager = $this->createMock('Drupal\Core\Language\LanguageManagerInterface');
$this->entityRepository = $this->createMock('Drupal\Core\Entity\EntityRepositoryInterface');
// Used for both book manager cache services: backend chain and memory.
$cache = $this->createMock(CacheBackendInterface::class);
$this->bookManager = new BookManager($this->entityTypeManager, $this->translation, $this->configFactory, $this->bookOutlineStorage, $this->renderer, $this->languageManager, $this->entityRepository, $cache, $cache);
}
/**
* Tests the getBookParents() method.
*
* @dataProvider providerTestGetBookParents
*/
public function testGetBookParents($book, $parent, $expected): void {
$this->assertEquals($expected, $this->bookManager->getBookParents($book, $parent));
}
/**
* Provides test data for testGetBookParents.
*
* @return array
* The test data.
*/
public static function providerTestGetBookParents() {
$empty = [
'p1' => 0,
'p2' => 0,
'p3' => 0,
'p4' => 0,
'p5' => 0,
'p6' => 0,
'p7' => 0,
'p8' => 0,
'p9' => 0,
];
return [
// Provides a book without an existing parent.
[
['pid' => 0, 'nid' => 12],
[],
['depth' => 1, 'p1' => 12] + $empty,
],
// Provides a book with an existing parent.
[
['pid' => 11, 'nid' => 12],
['nid' => 11, 'depth' => 1, 'p1' => 11],
['depth' => 2, 'p1' => 11, 'p2' => 12] + $empty,
],
// Provides a book with two existing parents.
[
['pid' => 11, 'nid' => 12],
['nid' => 11, 'depth' => 2, 'p1' => 10, 'p2' => 11],
['depth' => 3, 'p1' => 10, 'p2' => 11, 'p3' => 12] + $empty,
],
];
}
}

View File

@@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Unit;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\book\BookUninstallValidator
* @group book
* @group legacy
*/
class BookUninstallValidatorTest extends UnitTestCase {
/**
* @var \Drupal\book\BookUninstallValidator|\PHPUnit\Framework\MockObject\MockObject
*/
protected $bookUninstallValidator;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->bookUninstallValidator = $this->getMockBuilder('Drupal\book\BookUninstallValidator')
->disableOriginalConstructor()
->onlyMethods(['hasBookOutlines', 'hasBookNodes'])
->getMock();
$this->bookUninstallValidator->setStringTranslation($this->getStringTranslationStub());
}
/**
* @covers ::validate
*/
public function testValidateNotBook(): void {
$this->bookUninstallValidator->expects($this->never())
->method('hasBookOutlines');
$this->bookUninstallValidator->expects($this->never())
->method('hasBookNodes');
$module = 'not_book';
$expected = [];
$reasons = $this->bookUninstallValidator->validate($module);
$this->assertEquals($expected, $reasons);
}
/**
* @covers ::validate
*/
public function testValidateEntityQueryWithoutResults(): void {
$this->bookUninstallValidator->expects($this->once())
->method('hasBookOutlines')
->willReturn(FALSE);
$this->bookUninstallValidator->expects($this->once())
->method('hasBookNodes')
->willReturn(FALSE);
$module = 'book';
$expected = [];
$reasons = $this->bookUninstallValidator->validate($module);
$this->assertEquals($expected, $reasons);
}
/**
* @covers ::validate
*/
public function testValidateEntityQueryWithResults(): void {
$this->bookUninstallValidator->expects($this->once())
->method('hasBookOutlines')
->willReturn(FALSE);
$this->bookUninstallValidator->expects($this->once())
->method('hasBookNodes')
->willReturn(TRUE);
$module = 'book';
$expected = ['To uninstall Book, delete all content that has the Book content type'];
$reasons = $this->bookUninstallValidator->validate($module);
$this->assertEquals($expected, $reasons);
}
/**
* @covers ::validate
*/
public function testValidateOutlineStorage(): void {
$this->bookUninstallValidator->expects($this->once())
->method('hasBookOutlines')
->willReturn(TRUE);
$this->bookUninstallValidator->expects($this->never())
->method('hasBookNodes');
$module = 'book';
$expected = ['To uninstall Book, delete all content that is part of a book'];
$reasons = $this->bookUninstallValidator->validate($module);
$this->assertEquals($expected, $reasons);
}
}

View File

@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\book\Unit\Menu;
use Drupal\Tests\Core\Menu\LocalTaskIntegrationTestBase;
/**
* Tests existence of book local tasks.
*
* @group book
* @group legacy
*/
class BookLocalTasksTest extends LocalTaskIntegrationTestBase {
/**
* {@inheritdoc}
*/
protected function setUp(): void {
$this->directoryList = [
'book' => 'core/modules/book',
'node' => 'core/modules/node',
];
parent::setUp();
}
/**
* Tests local task existence.
*
* @dataProvider getBookAdminRoutes
*/
public function testBookAdminLocalTasks($route): void {
$this->assertLocalTasks($route, [
0 => ['book.admin', 'book.settings'],
]);
}
/**
* Provides a list of routes to test.
*/
public static function getBookAdminRoutes() {
return [
['book.admin'],
['book.settings'],
];
}
/**
* Tests local task existence.
*
* @dataProvider getBookNodeRoutes
*/
public function testBookNodeLocalTasks($route): void {
$this->assertLocalTasks($route, [
0 => ['entity.node.book_outline_form', 'entity.node.canonical', 'entity.node.edit_form', 'entity.node.delete_form', 'entity.node.version_history'],
]);
}
/**
* Provides a list of routes to test.
*/
public static function getBookNodeRoutes() {
return [
['entity.node.canonical'],
['entity.node.book_outline_form'],
];
}
}