updated core and modules
This commit is contained in:
@@ -8,8 +8,8 @@ dependencies:
|
||||
- node
|
||||
configure: book.settings
|
||||
|
||||
# Information added by Drupal.org packaging script on 2016-08-03
|
||||
version: '8.1.8'
|
||||
# Information added by Drupal.org packaging script on 2017-08-16
|
||||
version: '8.3.7'
|
||||
core: '8.x'
|
||||
project: 'drupal'
|
||||
datestamp: 1470233790
|
||||
datestamp: 1502903957
|
||||
|
||||
@@ -17,119 +17,119 @@ function book_uninstall() {
|
||||
* Implements hook_schema().
|
||||
*/
|
||||
function book_schema() {
|
||||
$schema['book'] = array(
|
||||
$schema['book'] = [
|
||||
'description' => 'Stores book outline information. Uniquely defines the location of each node in the book outline',
|
||||
'fields' => array(
|
||||
'nid' => array(
|
||||
'fields' => [
|
||||
'nid' => [
|
||||
'type' => 'int',
|
||||
'unsigned' => TRUE,
|
||||
'not null' => TRUE,
|
||||
'default' => 0,
|
||||
'description' => "The book page's {node}.nid.",
|
||||
),
|
||||
'bid' => array(
|
||||
],
|
||||
'bid' => [
|
||||
'type' => 'int',
|
||||
'unsigned' => TRUE,
|
||||
'not null' => TRUE,
|
||||
'default' => 0,
|
||||
'description' => "The book ID is the {book}.nid of the top-level page.",
|
||||
),
|
||||
'pid' => array(
|
||||
],
|
||||
'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' => array(
|
||||
],
|
||||
'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' => array(
|
||||
],
|
||||
'weight' => [
|
||||
'description' => 'Weight among book entries in the same book at the same depth.',
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
'default' => 0,
|
||||
),
|
||||
'depth' => array(
|
||||
],
|
||||
'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' => array(
|
||||
],
|
||||
'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' => array(
|
||||
],
|
||||
'p2' => [
|
||||
'description' => 'The second nid in the materialized path. See p1.',
|
||||
'type' => 'int',
|
||||
'unsigned' => TRUE,
|
||||
'not null' => TRUE,
|
||||
'default' => 0,
|
||||
),
|
||||
'p3' => array(
|
||||
],
|
||||
'p3' => [
|
||||
'description' => 'The third nid in the materialized path. See p1.',
|
||||
'type' => 'int',
|
||||
'unsigned' => TRUE,
|
||||
'not null' => TRUE,
|
||||
'default' => 0,
|
||||
),
|
||||
'p4' => array(
|
||||
],
|
||||
'p4' => [
|
||||
'description' => 'The fourth nid in the materialized path. See p1.',
|
||||
'type' => 'int',
|
||||
'unsigned' => TRUE,
|
||||
'not null' => TRUE,
|
||||
'default' => 0,
|
||||
),
|
||||
'p5' => array(
|
||||
],
|
||||
'p5' => [
|
||||
'description' => 'The fifth nid in the materialized path. See p1.',
|
||||
'type' => 'int',
|
||||
'unsigned' => TRUE,
|
||||
'not null' => TRUE,
|
||||
'default' => 0,
|
||||
),
|
||||
'p6' => array(
|
||||
],
|
||||
'p6' => [
|
||||
'description' => 'The sixth nid in the materialized path. See p1.',
|
||||
'type' => 'int',
|
||||
'unsigned' => TRUE,
|
||||
'not null' => TRUE,
|
||||
'default' => 0,
|
||||
),
|
||||
'p7' => array(
|
||||
],
|
||||
'p7' => [
|
||||
'description' => 'The seventh nid in the materialized path. See p1.',
|
||||
'type' => 'int',
|
||||
'unsigned' => TRUE,
|
||||
'not null' => TRUE,
|
||||
'default' => 0,
|
||||
),
|
||||
'p8' => array(
|
||||
],
|
||||
'p8' => [
|
||||
'description' => 'The eighth nid in the materialized path. See p1.',
|
||||
'type' => 'int',
|
||||
'unsigned' => TRUE,
|
||||
'not null' => TRUE,
|
||||
'default' => 0,
|
||||
),
|
||||
'p9' => array(
|
||||
],
|
||||
'p9' => [
|
||||
'description' => 'The ninth nid in the materialized path. See p1.',
|
||||
'type' => 'int',
|
||||
'unsigned' => TRUE,
|
||||
'not null' => TRUE,
|
||||
'default' => 0,
|
||||
),
|
||||
),
|
||||
'primary key' => array('nid'),
|
||||
'indexes' => array(
|
||||
'book_parents' => array('bid', 'p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8', 'p9'),
|
||||
),
|
||||
);
|
||||
],
|
||||
],
|
||||
'primary key' => ['nid'],
|
||||
'indexes' => [
|
||||
'book_parents' => ['bid', 'p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8', 'p9'],
|
||||
],
|
||||
];
|
||||
|
||||
return $schema;
|
||||
}
|
||||
|
||||
@@ -25,17 +25,17 @@ function book_help($route_name, RouteMatchInterface $route_match) {
|
||||
switch ($route_name) {
|
||||
case 'help.page.book':
|
||||
$output = '<h3>' . t('About') . '</h3>';
|
||||
$output .= '<p>' . t('The Book module is used for creating structured, multi-page content, such as site resource guides, manuals, and wikis. It allows you to create content that has chapters, sections, subsections, or any similarly-tiered structure. Enabling the module creates a new content type <em>Book page</em>. For more information, see the <a href=":book">online documentation for the Book module</a>.', array(':book' => 'https://www.drupal.org/documentation/modules/book')) . '</p>';
|
||||
$output .= '<p>' . t('The Book module is used for creating structured, multi-page content, such as site resource guides, manuals, and wikis. It allows you to create content that has chapters, sections, subsections, or any similarly-tiered structure. Enabling the module creates a new content type <em>Book page</em>. For more information, see the <a href=":book">online documentation for the Book module</a>.', [':book' => 'https://www.drupal.org/documentation/modules/book']) . '</p>';
|
||||
$output .= '<h3>' . t('Uses') . '</h3>';
|
||||
$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>.', array(':admin-book' => \Drupal::url('book.admin'))) . '</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' => \Drupal::url('book.admin')]) . '</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 enabled for book outlines. On the <a href=":admin-settings">Book settings page</a> you can configure content types that can used in book outlines.', array(':admin-settings' => \Drupal::url('book.settings'))) . '</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.', array(':admin-settings' => \Drupal::url('book.settings'))) . '</dd>';
|
||||
$output .= '<dd>' . t('The <em>Book page</em> content type is the initial content type enabled for book outlines. On the <a href=":admin-settings">Book settings page</a> you can configure content types that can used in book outlines.', [':admin-settings' => \Drupal::url('book.settings')]) . '</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' => \Drupal::url('book.settings')]) . '</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.", array(':admin-block' => (\Drupal::moduleHandler()->moduleExists('block')) ? \Drupal::url('block.admin_display') : '#')) . '</dd>';
|
||||
$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')) ? \Drupal::url('block.admin_display') : '#']) . '</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>';
|
||||
@@ -47,7 +47,7 @@ function book_help($route_name, RouteMatchInterface $route_match) {
|
||||
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>.', array(':book' => \Drupal::url('book.render'), ':book-admin' => \Drupal::url('book.admin'))) . '</p>';
|
||||
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' => \Drupal::url('book.render'), ':book-admin' => \Drupal::url('book.admin')]) . '</p>';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,23 +55,23 @@ function book_help($route_name, RouteMatchInterface $route_match) {
|
||||
* Implements hook_theme().
|
||||
*/
|
||||
function book_theme() {
|
||||
return array(
|
||||
'book_navigation' => array(
|
||||
'variables' => array('book_link' => NULL),
|
||||
),
|
||||
'book_tree' => array(
|
||||
'variables' => array('items' => array(), 'attributes' => array()),
|
||||
),
|
||||
'book_export_html' => array(
|
||||
'variables' => array('title' => NULL, 'contents' => NULL, 'depth' => NULL),
|
||||
),
|
||||
'book_all_books_block' => array(
|
||||
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' => array(
|
||||
'variables' => array('node' => NULL, 'content' => NULL, 'children' => NULL),
|
||||
),
|
||||
);
|
||||
],
|
||||
'book_node_export_html' => [
|
||||
'variables' => ['node' => NULL, 'content' => NULL, 'children' => NULL],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,31 +97,31 @@ function book_node_links_alter(array &$links, NodeInterface $node, array &$conte
|
||||
$child_type = \Drupal::config('book.settings')->get('child_type');
|
||||
$access_control_handler = \Drupal::entityManager()->getAccessControlHandler('node');
|
||||
if (($account->hasPermission('add content to books') || $account->hasPermission('administer book outlines')) && $access_control_handler->createAccess($child_type) && $node->isPublished() && $node->book['depth'] < BookManager::BOOK_MAX_DEPTH) {
|
||||
$book_links['book_add_child'] = array(
|
||||
$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'] = array(
|
||||
$book_links['book_printer'] = [
|
||||
'title' => t('Printer-friendly version'),
|
||||
'url' => Url::fromRoute('book.export', [
|
||||
'type' => 'html',
|
||||
'node' => $node->id(),
|
||||
]),
|
||||
'attributes' => array('title' => t('Show a printer-friendly version of this book page and its sub-pages.'))
|
||||
);
|
||||
'attributes' => ['title' => t('Show a printer-friendly version of this book page and its sub-pages.')]
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($book_links)) {
|
||||
$links['book'] = array(
|
||||
$links['book'] = [
|
||||
'#theme' => 'links__node__book',
|
||||
'#links' => $book_links,
|
||||
'#attributes' => array('class' => array('links', 'inline')),
|
||||
);
|
||||
'#attributes' => ['class' => ['links', 'inline']],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -148,17 +148,17 @@ function book_form_node_form_alter(&$form, FormStateInterface $form_state, $form
|
||||
$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'] = array(
|
||||
$form['book']['pick-book'] = [
|
||||
'#type' => 'submit',
|
||||
'#value' => t('Change book (update list of parents)'),
|
||||
'#submit' => array('book_pick_book_nojs_submit'),
|
||||
'#submit' => ['book_pick_book_nojs_submit'],
|
||||
'#weight' => 20,
|
||||
'#attributes' => array(
|
||||
'class' => array(
|
||||
'#attributes' => [
|
||||
'class' => [
|
||||
'js-hide',
|
||||
),
|
||||
),
|
||||
);
|
||||
],
|
||||
],
|
||||
];
|
||||
$form['#entity_builders'][] = 'book_node_builder';
|
||||
}
|
||||
}
|
||||
@@ -232,7 +232,7 @@ function book_node_view(array &$build, EntityInterface $node, EntityViewDisplayI
|
||||
if (!$book_node->access()) {
|
||||
return;
|
||||
}
|
||||
$build['book_navigation'] = array(
|
||||
$build['book_navigation'] = [
|
||||
'#theme' => 'book_navigation',
|
||||
'#book_link' => $node->book,
|
||||
'#weight' => 100,
|
||||
@@ -241,7 +241,7 @@ function book_node_view(array &$build, EntityInterface $node, EntityViewDisplayI
|
||||
'#cache' => [
|
||||
'tags' => $node->getEntityType()->getListCacheTags(),
|
||||
],
|
||||
);
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -295,7 +295,7 @@ function book_node_prepare_form(NodeInterface $node, $operation, FormStateInterf
|
||||
// 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 = array();
|
||||
$node->book = [];
|
||||
|
||||
$query = \Drupal::request()->query;
|
||||
if ($node->isNew() && !is_null($query->get('parent')) && is_numeric($query->get('parent'))) {
|
||||
@@ -341,10 +341,10 @@ function book_form_node_confirm_form_alter(&$form, FormStateInterface $form_stat
|
||||
}
|
||||
|
||||
if (isset($node->book) && $node->book['has_children']) {
|
||||
$form['book_warning'] = array(
|
||||
'#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.', array('%title' => $node->label())) . '</p>',
|
||||
$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,
|
||||
);
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -365,13 +365,13 @@ function book_form_node_confirm_form_alter(&$form, FormStateInterface $form_stat
|
||||
function template_preprocess_book_all_books_block(&$variables) {
|
||||
// Remove all non-renderable elements.
|
||||
$elements = $variables['book_menus'];
|
||||
$variables['book_menus'] = array();
|
||||
$variables['book_menus'] = [];
|
||||
foreach (Element::children($elements) as $index) {
|
||||
$variables['book_menus'][] = array(
|
||||
$variables['book_menus'][] = [
|
||||
'id' => $index,
|
||||
'menu' => $elements[$index],
|
||||
'title' => $elements[$index]['#book_title'],
|
||||
);
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -391,7 +391,7 @@ function template_preprocess_book_navigation(&$variables) {
|
||||
// Provide extra variables for themers. Not needed by default.
|
||||
$variables['book_id'] = $book_link['bid'];
|
||||
$variables['book_title'] = $book_link['link_title'];
|
||||
$variables['book_url'] = \Drupal::url('entity.node.canonical', array('node' => $book_link['bid']));
|
||||
$variables['book_url'] = \Drupal::url('entity.node.canonical', ['node' => $book_link['bid']]);
|
||||
$variables['current_depth'] = $book_link['depth'];
|
||||
$variables['tree'] = '';
|
||||
|
||||
@@ -401,14 +401,14 @@ function template_preprocess_book_navigation(&$variables) {
|
||||
if ($book_link['nid']) {
|
||||
$variables['tree'] = $book_outline->childrenLinks($book_link);
|
||||
|
||||
$build = array();
|
||||
$build = [];
|
||||
|
||||
if ($prev = $book_outline->prevLink($book_link)) {
|
||||
$prev_href = \Drupal::url('entity.node.canonical', array('node' => $prev['nid']));
|
||||
$build['#attached']['html_head_link'][][] = array(
|
||||
$prev_href = \Drupal::url('entity.node.canonical', ['node' => $prev['nid']]);
|
||||
$build['#attached']['html_head_link'][][] = [
|
||||
'rel' => 'prev',
|
||||
'href' => $prev_href,
|
||||
);
|
||||
];
|
||||
$variables['prev_url'] = $prev_href;
|
||||
$variables['prev_title'] = $prev['title'];
|
||||
}
|
||||
@@ -416,21 +416,21 @@ function template_preprocess_book_navigation(&$variables) {
|
||||
/** @var \Drupal\book\BookManagerInterface $book_manager */
|
||||
$book_manager = \Drupal::service('book.manager');
|
||||
if ($book_link['pid'] && $parent = $book_manager->loadBookLink($book_link['pid'])) {
|
||||
$parent_href = \Drupal::url('entity.node.canonical', array('node' => $book_link['pid']));
|
||||
$build['#attached']['html_head_link'][][] = array(
|
||||
$parent_href = \Drupal::url('entity.node.canonical', ['node' => $book_link['pid']]);
|
||||
$build['#attached']['html_head_link'][][] = [
|
||||
'rel' => 'up',
|
||||
'href' => $parent_href,
|
||||
);
|
||||
];
|
||||
$variables['parent_url'] = $parent_href;
|
||||
$variables['parent_title'] = $parent['title'];
|
||||
}
|
||||
|
||||
if ($next = $book_outline->nextLink($book_link)) {
|
||||
$next_href = \Drupal::url('entity.node.canonical', array('node' => $next['nid']));
|
||||
$build['#attached']['html_head_link'][][] = array(
|
||||
$next_href = \Drupal::url('entity.node.canonical', ['node' => $next['nid']]);
|
||||
$build['#attached']['html_head_link'][][] = [
|
||||
'rel' => 'next',
|
||||
'href' => $next_href,
|
||||
);
|
||||
];
|
||||
$variables['next_url'] = $next_href;
|
||||
$variables['next_title'] = $next['title'];
|
||||
}
|
||||
@@ -442,7 +442,7 @@ function template_preprocess_book_navigation(&$variables) {
|
||||
|
||||
$variables['has_links'] = FALSE;
|
||||
// Link variables to filter for values and set state of the flag variable.
|
||||
$links = array('prev_url', 'prev_title', 'parent_url', 'parent_title', 'next_url', 'next_title');
|
||||
$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.
|
||||
@@ -475,7 +475,7 @@ function template_preprocess_book_export_html(&$variables) {
|
||||
$variables['language_rtl'] = ($language_interface->getDirection() == LanguageInterface::DIRECTION_RTL);
|
||||
|
||||
// HTML element attributes.
|
||||
$attributes = array();
|
||||
$attributes = [];
|
||||
$attributes['lang'] = $language_interface->getId();
|
||||
$attributes['dir'] = $language_interface->getDirection();
|
||||
$variables['html_attributes'] = new Attribute($attributes);
|
||||
|
||||
@@ -3,7 +3,7 @@ administer book outlines:
|
||||
create new books:
|
||||
title: 'Create new books'
|
||||
add content to books:
|
||||
title: 'Add content and child pages 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.'
|
||||
|
||||
@@ -35,5 +35,5 @@ services:
|
||||
class: Drupal\book\BookUninstallValidator
|
||||
tags:
|
||||
- { name: module_install.uninstall_validator }
|
||||
arguments: ['@book.outline_storage', '@entity.query', '@string_translation']
|
||||
arguments: ['@book.outline_storage', '@entity_type.manager', '@string_translation']
|
||||
lazy: true
|
||||
|
||||
@@ -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 books 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;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
langcode: en
|
||||
status: true
|
||||
dependencies:
|
||||
config:
|
||||
- node.type.book
|
||||
id: node.book.promote
|
||||
field_name: promote
|
||||
entity_type: node
|
||||
bundle: book
|
||||
label: 'Promoted to front page'
|
||||
description: ''
|
||||
required: false
|
||||
translatable: true
|
||||
default_value:
|
||||
-
|
||||
value: 0
|
||||
default_value_callback: ''
|
||||
settings:
|
||||
on_label: 'On'
|
||||
off_label: 'Off'
|
||||
field_type: boolean
|
||||
@@ -0,0 +1,60 @@
|
||||
langcode: en
|
||||
status: true
|
||||
dependencies:
|
||||
config:
|
||||
- field.field.node.book.body
|
||||
- node.type.book
|
||||
module:
|
||||
- text
|
||||
id: node.book.default
|
||||
targetEntityType: node
|
||||
bundle: book
|
||||
mode: default
|
||||
content:
|
||||
body:
|
||||
type: text_textarea_with_summary
|
||||
weight: 26
|
||||
region: content
|
||||
settings:
|
||||
rows: 9
|
||||
summary_rows: 3
|
||||
placeholder: ''
|
||||
third_party_settings: { }
|
||||
created:
|
||||
type: datetime_timestamp
|
||||
weight: 10
|
||||
region: content
|
||||
settings: { }
|
||||
third_party_settings: { }
|
||||
promote:
|
||||
type: boolean_checkbox
|
||||
settings:
|
||||
display_label: true
|
||||
weight: 15
|
||||
region: content
|
||||
third_party_settings: { }
|
||||
sticky:
|
||||
type: boolean_checkbox
|
||||
settings:
|
||||
display_label: true
|
||||
weight: 16
|
||||
region: content
|
||||
third_party_settings: { }
|
||||
title:
|
||||
type: string_textfield
|
||||
weight: -5
|
||||
region: content
|
||||
settings:
|
||||
size: 60
|
||||
placeholder: ''
|
||||
third_party_settings: { }
|
||||
uid:
|
||||
type: entity_reference_autocomplete
|
||||
weight: 5
|
||||
region: content
|
||||
settings:
|
||||
match_operator: CONTAINS
|
||||
size: 60
|
||||
placeholder: ''
|
||||
third_party_settings: { }
|
||||
hidden: { }
|
||||
@@ -0,0 +1,25 @@
|
||||
langcode: en
|
||||
status: true
|
||||
dependencies:
|
||||
config:
|
||||
- field.field.node.book.body
|
||||
- node.type.book
|
||||
module:
|
||||
- text
|
||||
- user
|
||||
id: node.book.default
|
||||
targetEntityType: node
|
||||
bundle: book
|
||||
mode: default
|
||||
content:
|
||||
body:
|
||||
label: hidden
|
||||
type: text_default
|
||||
weight: 100
|
||||
region: content
|
||||
settings: { }
|
||||
third_party_settings: { }
|
||||
links:
|
||||
weight: 101
|
||||
region: content
|
||||
hidden: { }
|
||||
@@ -0,0 +1,27 @@
|
||||
langcode: en
|
||||
status: true
|
||||
dependencies:
|
||||
config:
|
||||
- core.entity_view_mode.node.teaser
|
||||
- field.field.node.book.body
|
||||
- node.type.book
|
||||
module:
|
||||
- text
|
||||
- user
|
||||
id: node.book.teaser
|
||||
targetEntityType: node
|
||||
bundle: book
|
||||
mode: teaser
|
||||
content:
|
||||
body:
|
||||
label: hidden
|
||||
type: text_summary_or_trimmed
|
||||
weight: 100
|
||||
region: content
|
||||
settings:
|
||||
trim_length: 600
|
||||
third_party_settings: { }
|
||||
links:
|
||||
weight: 101
|
||||
region: content
|
||||
hidden: { }
|
||||
@@ -0,0 +1,12 @@
|
||||
langcode: en
|
||||
status: false
|
||||
dependencies:
|
||||
enforced:
|
||||
module:
|
||||
- book
|
||||
module:
|
||||
- node
|
||||
id: node.print
|
||||
label: Print
|
||||
targetEntityType: node
|
||||
cache: true
|
||||
@@ -0,0 +1,21 @@
|
||||
langcode: en
|
||||
status: true
|
||||
dependencies:
|
||||
config:
|
||||
- field.storage.node.body
|
||||
- node.type.book
|
||||
module:
|
||||
- text
|
||||
id: node.book.body
|
||||
field_name: body
|
||||
entity_type: node
|
||||
bundle: book
|
||||
label: Body
|
||||
description: ''
|
||||
required: false
|
||||
translatable: true
|
||||
default_value: { }
|
||||
default_value_callback: ''
|
||||
settings:
|
||||
display_summary: true
|
||||
field_type: text_with_summary
|
||||
@@ -0,0 +1,13 @@
|
||||
langcode: en
|
||||
status: true
|
||||
dependencies:
|
||||
enforced:
|
||||
module:
|
||||
- book
|
||||
name: 'Book page'
|
||||
type: book
|
||||
description: '<em>Books</em> have a built-in hierarchical navigation. Use for handbooks or tutorials.'
|
||||
help: ''
|
||||
new_revision: true
|
||||
preview_mode: 1
|
||||
display_submitted: true
|
||||
@@ -0,0 +1,8 @@
|
||||
# Schema for the views plugins of the Book module.
|
||||
|
||||
views.argument_default.top_level_book:
|
||||
type: sequence
|
||||
label: 'Top Level Book from current node'
|
||||
sequence:
|
||||
type: string
|
||||
label: 'Nid'
|
||||
@@ -14,7 +14,7 @@ process:
|
||||
method: process
|
||||
source: plid
|
||||
-
|
||||
plugin: migration
|
||||
plugin: migration_lookup
|
||||
migration: d6_book
|
||||
destination:
|
||||
plugin: book
|
||||
|
||||
@@ -56,10 +56,10 @@ class BookBreadcrumbBuilder implements BreadcrumbBuilderInterface {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function build(RouteMatchInterface $route_match) {
|
||||
$book_nids = array();
|
||||
$book_nids = [];
|
||||
$breadcrumb = new Breadcrumb();
|
||||
|
||||
$links = array(Link::createFromRoute($this->t('Home'), '<front>'));
|
||||
$links = [Link::createFromRoute($this->t('Home'), '<front>')];
|
||||
$book = $route_match->getParameter('node')->book;
|
||||
$depth = 1;
|
||||
// We skip the current node.
|
||||
@@ -76,7 +76,7 @@ class BookBreadcrumbBuilder implements BreadcrumbBuilderInterface {
|
||||
$breadcrumb->addCacheableDependency($access);
|
||||
if ($access->isAllowed()) {
|
||||
$breadcrumb->addCacheableDependency($parent_book);
|
||||
$links[] = Link::createFromRoute($parent_book->label(), 'entity.node.canonical', array('node' => $parent_book->id()));
|
||||
$links[] = Link::createFromRoute($parent_book->label(), 'entity.node.canonical', ['node' => $parent_book->id()]);
|
||||
}
|
||||
}
|
||||
$depth++;
|
||||
|
||||
@@ -73,8 +73,8 @@ class BookExport {
|
||||
}
|
||||
|
||||
$tree = $this->bookManager->bookSubtreeData($node->book);
|
||||
$contents = $this->exportTraverse($tree, array($this, 'bookNodeExport'));
|
||||
return array(
|
||||
$contents = $this->exportTraverse($tree, [$this, 'bookNodeExport']);
|
||||
return [
|
||||
'#theme' => 'book_export_html',
|
||||
'#title' => $node->label(),
|
||||
'#contents' => $contents,
|
||||
@@ -82,7 +82,7 @@ class BookExport {
|
||||
'#cache' => [
|
||||
'tags' => $node->getEntityType()->getListCacheTags(),
|
||||
],
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,9 +101,9 @@ class BookExport {
|
||||
*/
|
||||
protected function exportTraverse(array $tree, $callable) {
|
||||
// If there is no valid callable, use the default callback.
|
||||
$callable = !empty($callable) ? $callable : array($this, 'bookNodeExport');
|
||||
$callable = !empty($callable) ? $callable : [$this, 'bookNodeExport'];
|
||||
|
||||
$build = array();
|
||||
$build = [];
|
||||
foreach ($tree as $data) {
|
||||
// Note- access checking is already performed when building the tree.
|
||||
if ($node = $this->nodeStorage->load($data['link']['nid'])) {
|
||||
@@ -133,12 +133,12 @@ class BookExport {
|
||||
$build = $this->viewBuilder->view($node, 'print', NULL);
|
||||
unset($build['#theme']);
|
||||
|
||||
return array(
|
||||
return [
|
||||
'#theme' => 'book_node_export_html',
|
||||
'#content' => $build,
|
||||
'#node' => $node,
|
||||
'#children' => $children,
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -92,7 +92,7 @@ class BookManager implements BookManagerInterface {
|
||||
* Loads Books Array.
|
||||
*/
|
||||
protected function loadBooks() {
|
||||
$this->books = array();
|
||||
$this->books = [];
|
||||
$nids = $this->bookOutlineStorage->getBooks();
|
||||
|
||||
if ($nids) {
|
||||
@@ -117,15 +117,15 @@ class BookManager implements BookManagerInterface {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getLinkDefaults($nid) {
|
||||
return array(
|
||||
return [
|
||||
'original_bid' => 0,
|
||||
'nid' => $nid,
|
||||
'bid' => 0,
|
||||
'pid' => 0,
|
||||
'has_children' => 0,
|
||||
'weight' => 0,
|
||||
'options' => array(),
|
||||
);
|
||||
'options' => [],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,7 +138,7 @@ class BookManager implements BookManagerInterface {
|
||||
/**
|
||||
* Determine the relative depth of the children of a given book link.
|
||||
*
|
||||
* @param array
|
||||
* @param array $book_link
|
||||
* The book link.
|
||||
*
|
||||
* @return int
|
||||
@@ -159,40 +159,40 @@ class BookManager implements BookManagerInterface {
|
||||
if ($form_state->hasValue('book')) {
|
||||
$node->book = $form_state->getValue('book');
|
||||
}
|
||||
$form['book'] = array(
|
||||
$form['book'] = [
|
||||
'#type' => 'details',
|
||||
'#title' => $this->t('Book outline'),
|
||||
'#weight' => 10,
|
||||
'#open' => !$collapsed,
|
||||
'#group' => 'advanced',
|
||||
'#attributes' => array(
|
||||
'class' => array('book-outline-form'),
|
||||
),
|
||||
'#attached' => array(
|
||||
'library' => array('book/drupal.book'),
|
||||
),
|
||||
'#attributes' => [
|
||||
'class' => ['book-outline-form'],
|
||||
],
|
||||
'#attached' => [
|
||||
'library' => ['book/drupal.book'],
|
||||
],
|
||||
'#tree' => TRUE,
|
||||
);
|
||||
foreach (array('nid', 'has_children', 'original_bid', 'parent_depth_limit') as $key) {
|
||||
$form['book'][$key] = array(
|
||||
];
|
||||
foreach (['nid', 'has_children', 'original_bid', 'parent_depth_limit'] as $key) {
|
||||
$form['book'][$key] = [
|
||||
'#type' => 'value',
|
||||
'#value' => $node->book[$key],
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
$form['book']['pid'] = $this->addParentSelectFormElements($node->book);
|
||||
|
||||
// @see \Drupal\book\Form\BookAdminEditForm::bookAdminTableTree(). The
|
||||
// weight may be larger than 15.
|
||||
$form['book']['weight'] = array(
|
||||
$form['book']['weight'] = [
|
||||
'#type' => 'weight',
|
||||
'#title' => $this->t('Weight'),
|
||||
'#default_value' => $node->book['weight'],
|
||||
'#delta' => max(15, abs($node->book['weight'])),
|
||||
'#weight' => 5,
|
||||
'#description' => $this->t('Pages at a given level are ordered first by weight and then by title.'),
|
||||
);
|
||||
$options = array();
|
||||
];
|
||||
$options = [];
|
||||
$nid = !$node->isNew() ? $node->id() : 'new';
|
||||
if ($node->id() && ($nid == $node->book['original_bid']) && ($node->book['parent_depth_limit'] == 0)) {
|
||||
// This is the top level node in a maximum depth book and thus cannot be
|
||||
@@ -207,15 +207,15 @@ class BookManager implements BookManagerInterface {
|
||||
|
||||
if ($account->hasPermission('create new books') && ($nid == 'new' || ($nid != $node->book['original_bid']))) {
|
||||
// The node can become a new book, if it is not one already.
|
||||
$options = array($nid => $this->t('- Create a new book -')) + $options;
|
||||
$options = [$nid => $this->t('- Create a new book -')] + $options;
|
||||
}
|
||||
if (!$node->book['bid']) {
|
||||
// The node is not currently in the hierarchy.
|
||||
$options = array(0 => $this->t('- None -')) + $options;
|
||||
$options = [0 => $this->t('- None -')] + $options;
|
||||
}
|
||||
|
||||
// Add a drop-down to select the destination book.
|
||||
$form['book']['bid'] = array(
|
||||
$form['book']['bid'] = [
|
||||
'#type' => 'select',
|
||||
'#title' => $this->t('Book'),
|
||||
'#default_value' => $node->book['bid'],
|
||||
@@ -223,14 +223,14 @@ class BookManager implements BookManagerInterface {
|
||||
'#access' => (bool) $options,
|
||||
'#description' => $this->t('Your page will be a part of the selected book.'),
|
||||
'#weight' => -5,
|
||||
'#attributes' => array('class' => array('book-title-select')),
|
||||
'#ajax' => array(
|
||||
'#attributes' => ['class' => ['book-title-select']],
|
||||
'#ajax' => [
|
||||
'callback' => 'book_form_update',
|
||||
'wrapper' => 'edit-book-plid-wrapper',
|
||||
'effect' => 'fade',
|
||||
'speed' => 'fast',
|
||||
),
|
||||
);
|
||||
],
|
||||
];
|
||||
return $form;
|
||||
}
|
||||
|
||||
@@ -281,8 +281,8 @@ class BookManager implements BookManagerInterface {
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getBookParents(array $item, array $parent = array()) {
|
||||
$book = array();
|
||||
public function getBookParents(array $item, array $parent = []) {
|
||||
$book = [];
|
||||
if ($item['pid'] == 0) {
|
||||
$book['p1'] = $item['nid'];
|
||||
for ($i = 2; $i <= static::BOOK_MAX_DEPTH; $i++) {
|
||||
@@ -325,15 +325,15 @@ class BookManager implements BookManagerInterface {
|
||||
protected function addParentSelectFormElements(array $book_link) {
|
||||
$config = $this->configFactory->get('book.settings');
|
||||
if ($config->get('override_parent_selector')) {
|
||||
return array();
|
||||
return [];
|
||||
}
|
||||
// Offer a message or a drop-down to choose a different parent page.
|
||||
$form = array(
|
||||
$form = [
|
||||
'#type' => 'hidden',
|
||||
'#value' => -1,
|
||||
'#prefix' => '<div id="edit-book-plid-wrapper">',
|
||||
'#suffix' => '</div>',
|
||||
);
|
||||
];
|
||||
|
||||
if ($book_link['nid'] === $book_link['bid']) {
|
||||
// This is a book - at the top level.
|
||||
@@ -348,16 +348,16 @@ class BookManager implements BookManagerInterface {
|
||||
$form['#prefix'] .= '<em>' . $this->t('No book selected.') . '</em>';
|
||||
}
|
||||
else {
|
||||
$form = array(
|
||||
$form = [
|
||||
'#type' => 'select',
|
||||
'#title' => $this->t('Parent item'),
|
||||
'#default_value' => $book_link['pid'],
|
||||
'#description' => $this->t('The parent page in the book. The maximum depth for a book and all child pages is @maxdepth. Some pages in the selected book may not be available as parents if selecting them would exceed this limit.', array('@maxdepth' => static::BOOK_MAX_DEPTH)),
|
||||
'#options' => $this->getTableOfContents($book_link['bid'], $book_link['parent_depth_limit'], array($book_link['nid'])),
|
||||
'#attributes' => array('class' => array('book-title-select')),
|
||||
'#description' => $this->t('The parent page in the book. The maximum depth for a book and all child pages is @maxdepth. Some pages in the selected book may not be available as parents if selecting them would exceed this limit.', ['@maxdepth' => static::BOOK_MAX_DEPTH]),
|
||||
'#options' => $this->getTableOfContents($book_link['bid'], $book_link['parent_depth_limit'], [$book_link['nid']]),
|
||||
'#attributes' => ['class' => ['book-title-select']],
|
||||
'#prefix' => '<div id="edit-book-plid-wrapper">',
|
||||
'#suffix' => '</div>',
|
||||
);
|
||||
];
|
||||
}
|
||||
$this->renderer->addCacheableDependency($form, $config);
|
||||
|
||||
@@ -388,7 +388,7 @@ class BookManager implements BookManagerInterface {
|
||||
* children).
|
||||
*/
|
||||
protected function recurseTableOfContents(array $tree, $indent, array &$toc, array $exclude, $depth_limit) {
|
||||
$nids = array();
|
||||
$nids = [];
|
||||
foreach ($tree as $data) {
|
||||
if ($data['link']['depth'] > $depth_limit) {
|
||||
// Don't iterate through any links on this level.
|
||||
@@ -417,9 +417,9 @@ class BookManager implements BookManagerInterface {
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getTableOfContents($bid, $depth_limit, array $exclude = array()) {
|
||||
public function getTableOfContents($bid, $depth_limit, array $exclude = []) {
|
||||
$tree = $this->bookTreeAllData($bid);
|
||||
$toc = array();
|
||||
$toc = [];
|
||||
$this->recurseTableOfContents($tree, '', $toc, $exclude, $depth_limit);
|
||||
|
||||
return $toc;
|
||||
@@ -443,14 +443,14 @@ class BookManager implements BookManagerInterface {
|
||||
}
|
||||
$this->updateOriginalParent($original);
|
||||
$this->books = NULL;
|
||||
Cache::invalidateTags(array('bid:' . $original['bid']));
|
||||
Cache::invalidateTags(['bid:' . $original['bid']]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function bookTreeAllData($bid, $link = NULL, $max_depth = NULL) {
|
||||
$tree = &drupal_static(__METHOD__, array());
|
||||
$tree = &drupal_static(__METHOD__, []);
|
||||
$language_interface = \Drupal::languageManager()->getCurrentLanguage();
|
||||
|
||||
// Use $nid as a flag for whether the data being loaded is for the whole
|
||||
@@ -462,10 +462,10 @@ class BookManager implements BookManagerInterface {
|
||||
|
||||
if (!isset($tree[$cid])) {
|
||||
// If the tree data was not in the static cache, build $tree_parameters.
|
||||
$tree_parameters = array(
|
||||
$tree_parameters = [
|
||||
'min_depth' => 1,
|
||||
'max_depth' => $max_depth,
|
||||
);
|
||||
];
|
||||
if ($nid) {
|
||||
$active_trail = $this->getActiveTrailIds($bid, $link);
|
||||
$tree_parameters['expanded'] = $active_trail;
|
||||
@@ -486,7 +486,7 @@ class BookManager implements BookManagerInterface {
|
||||
public function getActiveTrailIds($bid, $link) {
|
||||
// The tree is for a single item, so we need to match the values in its
|
||||
// p columns and 0 (the top level) with the plid values of other links.
|
||||
$active_trail = array(0);
|
||||
$active_trail = [0];
|
||||
for ($i = 1; $i < static::BOOK_MAX_DEPTH; $i++) {
|
||||
if (!empty($link["p$i"])) {
|
||||
$active_trail[] = $link["p$i"];
|
||||
@@ -600,7 +600,7 @@ class BookManager implements BookManagerInterface {
|
||||
* @return array
|
||||
* A fully built book tree.
|
||||
*/
|
||||
protected function bookTreeBuild($bid, array $parameters = array()) {
|
||||
protected function bookTreeBuild($bid, array $parameters = []) {
|
||||
// Build the book tree.
|
||||
$data = $this->doBookTreeBuild($bid, $parameters);
|
||||
// Check access for the current user to each item in the tree.
|
||||
@@ -639,9 +639,9 @@ class BookManager implements BookManagerInterface {
|
||||
*
|
||||
* @see \Drupal\book\BookOutlineStorageInterface::getBookMenuTree()
|
||||
*/
|
||||
protected function doBookTreeBuild($bid, array $parameters = array()) {
|
||||
protected function doBookTreeBuild($bid, array $parameters = []) {
|
||||
// Static cache of already built menu trees.
|
||||
$trees = &drupal_static(__METHOD__, array());
|
||||
$trees = &drupal_static(__METHOD__, []);
|
||||
$language_interface = \Drupal::languageManager()->getCurrentLanguage();
|
||||
|
||||
// Build the cache id; sort parents to prevent duplicate storage and remove
|
||||
@@ -664,18 +664,18 @@ class BookManager implements BookManagerInterface {
|
||||
$result = $this->bookOutlineStorage->getBookMenuTree($bid, $parameters, $min_depth, static::BOOK_MAX_DEPTH);
|
||||
|
||||
// Build an ordered array of links using the query result object.
|
||||
$links = array();
|
||||
$links = [];
|
||||
foreach ($result as $link) {
|
||||
$link = (array) $link;
|
||||
$links[$link['nid']] = $link;
|
||||
}
|
||||
$active_trail = (isset($parameters['active_trail']) ? $parameters['active_trail'] : array());
|
||||
$active_trail = (isset($parameters['active_trail']) ? $parameters['active_trail'] : []);
|
||||
$data['tree'] = $this->buildBookOutlineData($links, $active_trail, $min_depth);
|
||||
$data['node_links'] = array();
|
||||
$data['node_links'] = [];
|
||||
$this->bookTreeCollectNodeLinks($data['tree'], $data['node_links']);
|
||||
|
||||
// Cache the data, if it is not already in the cache.
|
||||
\Drupal::cache('data')->set($tree_cid, $data, Cache::PERMANENT, array('bid:' . $bid));
|
||||
\Drupal::cache('data')->set($tree_cid, $data, Cache::PERMANENT, ['bid:' . $bid]);
|
||||
$trees[$tree_cid] = $data;
|
||||
}
|
||||
|
||||
@@ -705,7 +705,7 @@ class BookManager implements BookManagerInterface {
|
||||
if (!isset($this->bookTreeFlattened[$book_link['nid']])) {
|
||||
// Call $this->bookTreeAllData() to take advantage of caching.
|
||||
$tree = $this->bookTreeAllData($book_link['bid'], $book_link, $book_link['depth'] + 1);
|
||||
$this->bookTreeFlattened[$book_link['nid']] = array();
|
||||
$this->bookTreeFlattened[$book_link['nid']] = [];
|
||||
$this->flatBookTree($tree, $this->bookTreeFlattened[$book_link['nid']]);
|
||||
}
|
||||
|
||||
@@ -735,7 +735,7 @@ class BookManager implements BookManagerInterface {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function loadBookLink($nid, $translate = TRUE) {
|
||||
$links = $this->loadBookLinks(array($nid), $translate);
|
||||
$links = $this->loadBookLinks([$nid], $translate);
|
||||
return isset($links[$nid]) ? $links[$nid] : FALSE;
|
||||
}
|
||||
|
||||
@@ -744,7 +744,7 @@ class BookManager implements BookManagerInterface {
|
||||
*/
|
||||
public function loadBookLinks($nids, $translate = TRUE) {
|
||||
$result = $this->bookOutlineStorage->loadMultiple($nids, $translate);
|
||||
$links = array();
|
||||
$links = [];
|
||||
foreach ($result as $link) {
|
||||
if ($translate) {
|
||||
$this->bookLinkTranslate($link);
|
||||
@@ -779,7 +779,7 @@ class BookManager implements BookManagerInterface {
|
||||
// Update the bid for this page and all children.
|
||||
if ($link['pid'] == 0) {
|
||||
$link['depth'] = 1;
|
||||
$parent = array();
|
||||
$parent = [];
|
||||
}
|
||||
// In case the form did not specify a proper PID we use the BID as new
|
||||
// parent.
|
||||
@@ -801,11 +801,11 @@ class BookManager implements BookManagerInterface {
|
||||
$this->updateParent($link);
|
||||
}
|
||||
// Update the weight and pid.
|
||||
$this->bookOutlineStorage->update($link['nid'], array(
|
||||
$this->bookOutlineStorage->update($link['nid'], [
|
||||
'weight' => $link['weight'],
|
||||
'pid' => $link['pid'],
|
||||
'bid' => $link['bid'],
|
||||
));
|
||||
]);
|
||||
}
|
||||
$cache_tags = [];
|
||||
foreach ($affected_bids as $bid) {
|
||||
@@ -825,16 +825,16 @@ class BookManager implements BookManagerInterface {
|
||||
*/
|
||||
protected function moveChildren(array $link, array $original) {
|
||||
$p = 'p1';
|
||||
$expressions = array();
|
||||
$expressions = [];
|
||||
for ($i = 1; $i <= $link['depth']; $p = 'p' . ++$i) {
|
||||
$expressions[] = array($p, ":p_$i", array(":p_$i" => $link[$p]));
|
||||
$expressions[] = [$p, ":p_$i", [":p_$i" => $link[$p]]];
|
||||
}
|
||||
$j = $original['depth'] + 1;
|
||||
while ($i <= static::BOOK_MAX_DEPTH && $j <= static::BOOK_MAX_DEPTH) {
|
||||
$expressions[] = array('p' . $i++, 'p' . $j++, array());
|
||||
$expressions[] = ['p' . $i++, 'p' . $j++, []];
|
||||
}
|
||||
while ($i <= static::BOOK_MAX_DEPTH) {
|
||||
$expressions[] = array('p' . $i++, 0, array());
|
||||
$expressions[] = ['p' . $i++, 0, []];
|
||||
}
|
||||
|
||||
$shift = $link['depth'] - $original['depth'];
|
||||
@@ -868,7 +868,7 @@ class BookManager implements BookManagerInterface {
|
||||
// Nothing to update.
|
||||
return TRUE;
|
||||
}
|
||||
return $this->bookOutlineStorage->update($link['pid'], array('has_children' => 1));
|
||||
return $this->bookOutlineStorage->update($link['pid'], ['has_children' => 1]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -897,7 +897,7 @@ class BookManager implements BookManagerInterface {
|
||||
// Update the parent. If the original link did not have children, then the
|
||||
// parent now does not have children. If the original had children, then the
|
||||
// the parent has children now (still).
|
||||
return $this->bookOutlineStorage->update($original['pid'], array('has_children' => $parent_has_children));
|
||||
return $this->bookOutlineStorage->update($original['pid'], ['has_children' => $parent_has_children]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -926,7 +926,7 @@ class BookManager implements BookManagerInterface {
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function bookTreeCheckAccess(&$tree, $node_links = array()) {
|
||||
public function bookTreeCheckAccess(&$tree, $node_links = []) {
|
||||
if ($node_links) {
|
||||
// @todo Extract that into its own method.
|
||||
$nids = array_keys($node_links);
|
||||
@@ -954,7 +954,7 @@ class BookManager implements BookManagerInterface {
|
||||
* The book tree to operate on.
|
||||
*/
|
||||
protected function doBookTreeCheckAccess(&$tree) {
|
||||
$new_tree = array();
|
||||
$new_tree = [];
|
||||
foreach ($tree as $key => $v) {
|
||||
$item = &$tree[$key]['link'];
|
||||
$this->bookLinkTranslate($item);
|
||||
@@ -993,7 +993,7 @@ class BookManager implements BookManagerInterface {
|
||||
}
|
||||
// The node label will be the value for the current user's language.
|
||||
$link['title'] = $node->label();
|
||||
$link['options'] = array();
|
||||
$link['options'] = [];
|
||||
}
|
||||
return $link;
|
||||
}
|
||||
@@ -1021,7 +1021,7 @@ class BookManager implements BookManagerInterface {
|
||||
* array will be empty if the book link has no items in its sub-tree
|
||||
* having a depth greater than or equal to $depth.
|
||||
*/
|
||||
protected function buildBookOutlineData(array $links, array $parents = array(), $depth = 1) {
|
||||
protected function buildBookOutlineData(array $links, array $parents = [], $depth = 1) {
|
||||
// Reverse the array so we can use the more efficient array_pop() function.
|
||||
$links = array_reverse($links);
|
||||
return $this->buildBookOutlineRecursive($links, $parents, $depth);
|
||||
@@ -1047,16 +1047,16 @@ class BookManager implements BookManagerInterface {
|
||||
* Book tree.
|
||||
*/
|
||||
protected function buildBookOutlineRecursive(&$links, $parents, $depth) {
|
||||
$tree = array();
|
||||
$tree = [];
|
||||
while ($item = array_pop($links)) {
|
||||
// We need to determine if we're on the path to root so we can later build
|
||||
// the correct active trail.
|
||||
$item['in_active_trail'] = in_array($item['nid'], $parents);
|
||||
// Add the current link to the tree.
|
||||
$tree[$item['nid']] = array(
|
||||
$tree[$item['nid']] = [
|
||||
'link' => $item,
|
||||
'below' => array(),
|
||||
);
|
||||
'below' => [],
|
||||
];
|
||||
// Look ahead to the next link, but leave it on the array so it's
|
||||
// available to other recursive function calls if we return or build a
|
||||
// sub-tree.
|
||||
@@ -1080,7 +1080,7 @@ class BookManager implements BookManagerInterface {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function bookSubtreeData($link) {
|
||||
$tree = &drupal_static(__METHOD__, array());
|
||||
$tree = &drupal_static(__METHOD__, []);
|
||||
|
||||
// Generate a cache ID (cid) specific for this $link.
|
||||
$cid = 'book-links:subtree-cid:' . $link['nid'];
|
||||
@@ -1101,23 +1101,23 @@ class BookManager implements BookManagerInterface {
|
||||
// If the subtree data was not in the cache, $data will be NULL.
|
||||
if (!isset($data)) {
|
||||
$result = $this->bookOutlineStorage->getBookSubtree($link, static::BOOK_MAX_DEPTH);
|
||||
$links = array();
|
||||
$links = [];
|
||||
foreach ($result as $item) {
|
||||
$links[] = $item;
|
||||
}
|
||||
$data['tree'] = $this->buildBookOutlineData($links, array(), $link['depth']);
|
||||
$data['node_links'] = array();
|
||||
$data['tree'] = $this->buildBookOutlineData($links, [], $link['depth']);
|
||||
$data['node_links'] = [];
|
||||
$this->bookTreeCollectNodeLinks($data['tree'], $data['node_links']);
|
||||
// Compute the real cid for book subtree data.
|
||||
$tree_cid = 'book-links:subtree-data:' . hash('sha256', serialize($data));
|
||||
// Cache the data, if it is not already in the cache.
|
||||
|
||||
if (!\Drupal::cache('data')->get($tree_cid)) {
|
||||
\Drupal::cache('data')->set($tree_cid, $data, Cache::PERMANENT, array('bid:' . $link['bid']));
|
||||
\Drupal::cache('data')->set($tree_cid, $data, Cache::PERMANENT, ['bid:' . $link['bid']]);
|
||||
}
|
||||
// Cache the cid of the (shared) data using the book and item-specific
|
||||
// cid.
|
||||
\Drupal::cache('data')->set($cid, $tree_cid, Cache::PERMANENT, array('bid:' . $link['bid']));
|
||||
\Drupal::cache('data')->set($cid, $tree_cid, Cache::PERMANENT, ['bid:' . $link['bid']]);
|
||||
}
|
||||
// Check access for the current user to each item in the tree.
|
||||
$this->bookTreeCheckAccess($data['tree'], $data['node_links']);
|
||||
|
||||
@@ -108,7 +108,7 @@ interface BookManagerInterface {
|
||||
* 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 = array());
|
||||
public function getTableOfContents($bid, $depth_limit, array $exclude = []);
|
||||
|
||||
/**
|
||||
* Finds the depth limit for items in the parent select.
|
||||
@@ -207,7 +207,7 @@ interface BookManagerInterface {
|
||||
*/
|
||||
public function getLinkDefaults($nid);
|
||||
|
||||
public function getBookParents(array $item, array $parent = array());
|
||||
public function getBookParents(array $item, array $parent = []);
|
||||
|
||||
/**
|
||||
* Builds the common elements of the book form for the node and outline forms.
|
||||
@@ -262,7 +262,7 @@ interface BookManagerInterface {
|
||||
* A collection of node link references generated from $tree by
|
||||
* menu_tree_collect_node_links().
|
||||
*/
|
||||
public function bookTreeCheckAccess(&$tree, $node_links = array());
|
||||
public function bookTreeCheckAccess(&$tree, $node_links = []);
|
||||
|
||||
/**
|
||||
* Gets the data representing a subtree of the book hierarchy.
|
||||
|
||||
@@ -105,7 +105,7 @@ class BookOutline {
|
||||
public function childrenLinks(array $book_link) {
|
||||
$flat = $this->bookManager->bookTreeGetFlat($book_link);
|
||||
|
||||
$children = array();
|
||||
$children = [];
|
||||
|
||||
if ($book_link['has_children']) {
|
||||
// Walk through the array until we find the current page.
|
||||
|
||||
@@ -43,7 +43,7 @@ class BookOutlineStorage implements BookOutlineStorageInterface {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function loadMultiple($nids, $access = TRUE) {
|
||||
$query = $this->connection->select('book', 'b', array('fetch' => \PDO::FETCH_ASSOC));
|
||||
$query = $this->connection->select('book', 'b', ['fetch' => \PDO::FETCH_ASSOC]);
|
||||
$query->fields('b');
|
||||
$query->condition('b.nid', $nids, 'IN');
|
||||
|
||||
@@ -89,7 +89,7 @@ class BookOutlineStorage implements BookOutlineStorageInterface {
|
||||
*/
|
||||
public function loadBookChildren($pid) {
|
||||
return $this->connection
|
||||
->query("SELECT * FROM {book} WHERE pid = :pid", array(':pid' => $pid))
|
||||
->query("SELECT * FROM {book} WHERE pid = :pid", [':pid' => $pid])
|
||||
->fetchAllAssoc('nid', \PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
@@ -128,12 +128,12 @@ class BookOutlineStorage implements BookOutlineStorageInterface {
|
||||
public function insert($link, $parents) {
|
||||
return $this->connection
|
||||
->insert('book')
|
||||
->fields(array(
|
||||
->fields([
|
||||
'nid' => $link['nid'],
|
||||
'bid' => $link['bid'],
|
||||
'pid' => $link['pid'],
|
||||
'weight' => $link['weight'],
|
||||
) + $parents
|
||||
] + $parents
|
||||
)
|
||||
->execute();
|
||||
}
|
||||
@@ -154,13 +154,13 @@ class BookOutlineStorage implements BookOutlineStorageInterface {
|
||||
*/
|
||||
public function updateMovedChildren($bid, $original, $expressions, $shift) {
|
||||
$query = $this->connection->update('book');
|
||||
$query->fields(array('bid' => $bid));
|
||||
$query->fields(['bid' => $bid]);
|
||||
|
||||
foreach ($expressions as $expression) {
|
||||
$query->expression($expression[0], $expression[1], $expression[2]);
|
||||
}
|
||||
|
||||
$query->expression('depth', 'depth + :depth', array(':depth' => $shift));
|
||||
$query->expression('depth', 'depth + :depth', [':depth' => $shift]);
|
||||
$query->condition('bid', $original['bid']);
|
||||
$p = 'p1';
|
||||
for ($i = 1; !empty($original[$p]); $p = 'p' . ++$i) {
|
||||
@@ -186,7 +186,7 @@ class BookOutlineStorage implements BookOutlineStorageInterface {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getBookSubtree($link, $max_depth) {
|
||||
$query = db_select('book', 'b', array('fetch' => \PDO::FETCH_ASSOC));
|
||||
$query = db_select('book', 'b', ['fetch' => \PDO::FETCH_ASSOC]);
|
||||
$query->fields('b');
|
||||
$query->condition('b.bid', $link['bid']);
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Drupal\book;
|
||||
|
||||
use Drupal\Core\Entity\Query\QueryFactory;
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\Core\Extension\ModuleUninstallValidatorInterface;
|
||||
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
||||
use Drupal\Core\StringTranslation\TranslationInterface;
|
||||
@@ -23,25 +23,25 @@ class BookUninstallValidator implements ModuleUninstallValidatorInterface {
|
||||
protected $bookOutlineStorage;
|
||||
|
||||
/**
|
||||
* The entity query for node.
|
||||
* The entity type manager.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\Query\QueryInterface
|
||||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
||||
*/
|
||||
protected $entityQuery;
|
||||
protected $entityTypeManager;
|
||||
|
||||
/**
|
||||
* Constructs a new BookUninstallValidator.
|
||||
*
|
||||
* @param \Drupal\book\BookOutlineStorageInterface $book_outline_storage
|
||||
* The book outline storage.
|
||||
* @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
|
||||
* The entity query factory.
|
||||
* @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, QueryFactory $query_factory, TranslationInterface $string_translation) {
|
||||
public function __construct(BookOutlineStorageInterface $book_outline_storage, EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation) {
|
||||
$this->bookOutlineStorage = $book_outline_storage;
|
||||
$this->entityQuery = $query_factory->get('node');
|
||||
$this->entityTypeManager = $entity_type_manager;
|
||||
$this->stringTranslation = $string_translation;
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ class BookUninstallValidator implements ModuleUninstallValidatorInterface {
|
||||
* TRUE if there are book nodes, FALSE otherwise.
|
||||
*/
|
||||
protected function hasBookNodes() {
|
||||
$nodes = $this->entityQuery
|
||||
$nodes = $this->entityTypeManager->getStorage('node')->getQuery()
|
||||
->condition('type', 'book')
|
||||
->accessCheck(FALSE)
|
||||
->range(0, 1)
|
||||
|
||||
@@ -4,7 +4,8 @@ namespace Drupal\book\Cache;
|
||||
|
||||
use Drupal\Core\Cache\CacheableMetadata;
|
||||
use Drupal\Core\Cache\Context\CacheContextInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerAware;
|
||||
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
|
||||
/**
|
||||
@@ -19,7 +20,9 @@ use Symfony\Component\HttpFoundation\RequestStack;
|
||||
* This class is container-aware to avoid initializing the 'book.manager'
|
||||
* service when it is not necessary.
|
||||
*/
|
||||
class BookNavigationCacheContext extends ContainerAware implements CacheContextInterface {
|
||||
class BookNavigationCacheContext implements CacheContextInterface, ContainerAwareInterface {
|
||||
|
||||
use ContainerAwareTrait;
|
||||
|
||||
/**
|
||||
* The request stack.
|
||||
|
||||
@@ -73,9 +73,9 @@ class BookController extends ControllerBase {
|
||||
* A render array representing the administrative page content.
|
||||
*/
|
||||
public function adminOverview() {
|
||||
$rows = array();
|
||||
$rows = [];
|
||||
|
||||
$headers = array(t('Book'), t('Operations'));
|
||||
$headers = [t('Book'), t('Operations')];
|
||||
// Add any recognized books to the table list.
|
||||
foreach ($this->bookManager->getAllBooks() as $book) {
|
||||
/** @var \Drupal\Core\Url $url */
|
||||
@@ -83,28 +83,28 @@ class BookController extends ControllerBase {
|
||||
if (isset($book['options'])) {
|
||||
$url->setOptions($book['options']);
|
||||
}
|
||||
$row = array(
|
||||
$row = [
|
||||
$this->l($book['title'], $url),
|
||||
);
|
||||
$links = array();
|
||||
$links['edit'] = array(
|
||||
];
|
||||
$links = [];
|
||||
$links['edit'] = [
|
||||
'title' => t('Edit order and titles'),
|
||||
'url' => Url::fromRoute('book.admin_edit', ['node' => $book['nid']]),
|
||||
);
|
||||
$row[] = array(
|
||||
'data' => array(
|
||||
];
|
||||
$row[] = [
|
||||
'data' => [
|
||||
'#type' => 'operations',
|
||||
'#links' => $links,
|
||||
),
|
||||
);
|
||||
],
|
||||
];
|
||||
$rows[] = $row;
|
||||
}
|
||||
return array(
|
||||
return [
|
||||
'#type' => 'table',
|
||||
'#header' => $headers,
|
||||
'#rows' => $rows,
|
||||
'#empty' => t('No books available.'),
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,17 +114,17 @@ class BookController extends ControllerBase {
|
||||
* A render array representing the listing of all books content.
|
||||
*/
|
||||
public function bookRender() {
|
||||
$book_list = array();
|
||||
$book_list = [];
|
||||
foreach ($this->bookManager->getAllBooks() as $book) {
|
||||
$book_list[] = $this->l($book['title'], $book['url']);
|
||||
}
|
||||
return array(
|
||||
return [
|
||||
'#theme' => 'item_list',
|
||||
'#items' => $book_list,
|
||||
'#cache' => [
|
||||
'tags' => \Drupal::entityManager()->getDefinition('node')->getListCacheTags(),
|
||||
],
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -70,10 +70,10 @@ class BookAdminEditForm extends FormBase {
|
||||
$form['#title'] = $node->label();
|
||||
$form['#node'] = $node;
|
||||
$this->bookAdminTable($node, $form);
|
||||
$form['save'] = array(
|
||||
$form['save'] = [
|
||||
'#type' => 'submit',
|
||||
'#value' => $this->t('Save book pages'),
|
||||
);
|
||||
];
|
||||
|
||||
return $form;
|
||||
}
|
||||
@@ -101,7 +101,7 @@ class BookAdminEditForm extends FormBase {
|
||||
foreach (Element::children($form['table']) as $key) {
|
||||
if ($form['table'][$key]['#item']) {
|
||||
$row = $form['table'][$key];
|
||||
$values = $form_state->getValue(array('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']) {
|
||||
@@ -114,18 +114,18 @@ class BookAdminEditForm extends FormBase {
|
||||
// Update the title if changed.
|
||||
if ($row['title']['#default_value'] != $values['title']) {
|
||||
$node = $this->nodeStorage->load($values['nid']);
|
||||
$node->revision_log = $this->t('Title changed from %original to %current.', array('%original' => $node->label(), '%current' => $values['title']));
|
||||
$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')->notice('book: updated %title.', array('%title' => $node->label(), 'link' => $node->link($this->t('View'))));
|
||||
$this->logger('content')->notice('book: updated %title.', ['%title' => $node->label(), 'link' => $node->link($this->t('View'))]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
drupal_set_message($this->t('Updated book %title.', array('%title' => $form['#node']->label())));
|
||||
drupal_set_message($this->t('Updated book %title.', ['%title' => $form['#node']->label()]));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,7 +139,7 @@ class BookAdminEditForm extends FormBase {
|
||||
* @see self::buildForm()
|
||||
*/
|
||||
protected function bookAdminTable(NodeInterface $node, array &$form) {
|
||||
$form['table'] = array(
|
||||
$form['table'] = [
|
||||
'#type' => 'table',
|
||||
'#header' => [
|
||||
$this->t('Title'),
|
||||
@@ -164,7 +164,7 @@ class BookAdminEditForm extends FormBase {
|
||||
'group' => 'book-weight',
|
||||
],
|
||||
],
|
||||
);
|
||||
];
|
||||
|
||||
$tree = $this->bookManager->bookSubtreeData($node->book);
|
||||
// Do not include the book item itself.
|
||||
@@ -173,14 +173,14 @@ class BookAdminEditForm extends FormBase {
|
||||
$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'] = array(
|
||||
$form['tree_hash'] = [
|
||||
'#type' => 'hidden',
|
||||
'#default_value' => $hash,
|
||||
);
|
||||
$form['tree_current_hash'] = array(
|
||||
];
|
||||
$form['tree_current_hash'] = [
|
||||
'#type' => 'value',
|
||||
'#value' => $hash,
|
||||
);
|
||||
];
|
||||
$this->bookAdminTableTree($tree['below'], $form['table']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
namespace Drupal\book\Form;
|
||||
|
||||
use Drupal\book\BookManagerInterface;
|
||||
use Drupal\Component\Datetime\TimeInterface;
|
||||
use Drupal\Core\Entity\ContentEntityForm;
|
||||
use Drupal\Core\Entity\EntityManagerInterface;
|
||||
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Url;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
@@ -35,9 +37,13 @@ class BookOutlineForm extends ContentEntityForm {
|
||||
* The entity manager.
|
||||
* @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(EntityManagerInterface $entity_manager, BookManagerInterface $book_manager) {
|
||||
parent::__construct($entity_manager);
|
||||
public function __construct(EntityManagerInterface $entity_manager, BookManagerInterface $book_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) {
|
||||
parent::__construct($entity_manager, $entity_type_bundle_info, $time);
|
||||
$this->bookManager = $book_manager;
|
||||
}
|
||||
|
||||
@@ -47,7 +53,9 @@ class BookOutlineForm extends ContentEntityForm {
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('entity.manager'),
|
||||
$container->get('book.manager')
|
||||
$container->get('book.manager'),
|
||||
$container->get('entity_type.bundle.info'),
|
||||
$container->get('datetime.time')
|
||||
);
|
||||
}
|
||||
|
||||
@@ -99,7 +107,7 @@ class BookOutlineForm extends ContentEntityForm {
|
||||
public function save(array $form, FormStateInterface $form_state) {
|
||||
$form_state->setRedirect(
|
||||
'entity.node.canonical',
|
||||
array('node' => $this->entity->id())
|
||||
['node' => $this->entity->id()]
|
||||
);
|
||||
$book_link = $form_state->getValue('book');
|
||||
if (!$book_link['bid']) {
|
||||
|
||||
@@ -65,7 +65,7 @@ class BookRemoveForm extends ConfirmFormBase {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDescription() {
|
||||
$title = array('%title' => $this->node->label());
|
||||
$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);
|
||||
}
|
||||
@@ -85,7 +85,7 @@ class BookRemoveForm extends ConfirmFormBase {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getQuestion() {
|
||||
return $this->t('Are you sure you want to remove %title from the book hierarchy?', array('%title' => $this->node->label()));
|
||||
return $this->t('Are you sure you want to remove %title from the book hierarchy?', ['%title' => $this->node->label()]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -30,22 +30,22 @@ class BookSettingsForm extends ConfigFormBase {
|
||||
public function buildForm(array $form, FormStateInterface $form_state) {
|
||||
$types = node_type_get_names();
|
||||
$config = $this->config('book.settings');
|
||||
$form['book_allowed_types'] = array(
|
||||
$form['book_allowed_types'] = [
|
||||
'#type' => 'checkboxes',
|
||||
'#title' => $this->t('Content types allowed in book outlines'),
|
||||
'#default_value' => $config->get('allowed_types'),
|
||||
'#options' => $types,
|
||||
'#description' => $this->t('Users with the %outline-perm permission can add all content types.', array('%outline-perm' => $this->t('Administer book outlines'))),
|
||||
'#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'] = array(
|
||||
];
|
||||
$form['book_child_type'] = [
|
||||
'#type' => 'radios',
|
||||
'#title' => $this->t('Content type for the <em>Add child page</em> link'),
|
||||
'#default_value' => $config->get('child_type'),
|
||||
'#options' => $types,
|
||||
'#required' => TRUE,
|
||||
);
|
||||
$form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
|
||||
];
|
||||
$form['array_filter'] = ['#type' => 'value', '#value' => TRUE];
|
||||
|
||||
return parent::buildForm($form, $form_state);
|
||||
}
|
||||
@@ -55,8 +55,8 @@ class BookSettingsForm extends ConfigFormBase {
|
||||
*/
|
||||
public function validateForm(array &$form, FormStateInterface $form_state) {
|
||||
$child_type = $form_state->getValue('book_child_type');
|
||||
if ($form_state->isValueEmpty(array('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.', array('%add-child' => $this->t('Add child page'))));
|
||||
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);
|
||||
|
||||
@@ -7,6 +7,7 @@ use Drupal\book\BookManagerInterface;
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\node\NodeInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
@@ -85,26 +86,26 @@ class BookNavigationBlock extends BlockBase implements ContainerFactoryPluginInt
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function defaultConfiguration() {
|
||||
return array(
|
||||
return [
|
||||
'block_mode' => "all pages",
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
function blockForm($form, FormStateInterface $form_state) {
|
||||
$options = array(
|
||||
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'] = array(
|
||||
];
|
||||
$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;
|
||||
}
|
||||
@@ -126,8 +127,8 @@ class BookNavigationBlock extends BlockBase implements ContainerFactoryPluginInt
|
||||
$current_bid = empty($node->book['bid']) ? 0 : $node->book['bid'];
|
||||
}
|
||||
if ($this->configuration['block_mode'] == 'all pages') {
|
||||
$book_menus = array();
|
||||
$pseudo_tree = array(0 => array('below' => FALSE));
|
||||
$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
|
||||
@@ -145,20 +146,23 @@ class BookNavigationBlock extends BlockBase implements ContainerFactoryPluginInt
|
||||
$pseudo_tree[0]['link'] = $book;
|
||||
$book_menus[$book_id] = $this->bookManager->bookTreeOutput($pseudo_tree);
|
||||
}
|
||||
$book_menus[$book_id] += array(
|
||||
$book_menus[$book_id] += [
|
||||
'#book_title' => $book['title'],
|
||||
);
|
||||
];
|
||||
}
|
||||
if ($book_menus) {
|
||||
return array(
|
||||
return [
|
||||
'#theme' => 'book_all_books_block',
|
||||
) + $book_menus;
|
||||
] + $book_menus;
|
||||
}
|
||||
}
|
||||
elseif ($current_bid) {
|
||||
// Only display this block when the user is browsing a book.
|
||||
$query = \Drupal::entityQuery('node');
|
||||
$nid = $query->condition('nid', $node->book['bid'], '=')->execute();
|
||||
// Only display this block when the user is browsing a book and do
|
||||
// not show unpublished books.
|
||||
$nid = \Drupal::entityQuery('node')
|
||||
->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) {
|
||||
@@ -171,7 +175,7 @@ class BookNavigationBlock extends BlockBase implements ContainerFactoryPluginInt
|
||||
}
|
||||
}
|
||||
}
|
||||
return array();
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,9 +17,9 @@ class Book extends DrupalSqlBase {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function query() {
|
||||
$query = $this->select('book', 'b')->fields('b', array('nid', 'bid'));
|
||||
$query = $this->select('book', 'b')->fields('b', ['nid', 'bid']);
|
||||
$query->join('menu_links', 'ml', 'b.mlid = ml.mlid');
|
||||
$ml_fields = array('mlid', 'plid', 'weight', 'has_children', 'depth');
|
||||
$ml_fields = ['mlid', 'plid', 'weight', 'has_children', 'depth'];
|
||||
for ($i = 1; $i <= 9; $i++) {
|
||||
$field = "p$i";
|
||||
$ml_fields[] = $field;
|
||||
@@ -42,7 +42,7 @@ class Book extends DrupalSqlBase {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function fields() {
|
||||
return array(
|
||||
return [
|
||||
'nid' => $this->t('Node ID'),
|
||||
'bid' => $this->t('Book ID'),
|
||||
'mlid' => $this->t('Menu link ID'),
|
||||
@@ -57,7 +57,7 @@ class Book extends DrupalSqlBase {
|
||||
'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.'),
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\book\Plugin\views\argument_default;
|
||||
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Drupal\node\NodeStorageInterface;
|
||||
use Drupal\node\Plugin\views\argument_default\Node;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Default argument plugin to get the current node's top level book.
|
||||
*
|
||||
* @ViewsArgumentDefault(
|
||||
* id = "top_level_book",
|
||||
* title = @Translation("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.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'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,8 +5,8 @@ package: Testing
|
||||
# version: VERSION
|
||||
# core: 8.x
|
||||
|
||||
# Information added by Drupal.org packaging script on 2016-08-03
|
||||
version: '8.1.8'
|
||||
# Information added by Drupal.org packaging script on 2017-08-16
|
||||
version: '8.3.7'
|
||||
core: '8.x'
|
||||
project: 'drupal'
|
||||
datestamp: 1470233790
|
||||
datestamp: 1502903957
|
||||
|
||||
@@ -5,8 +5,8 @@ package: Testing
|
||||
# version: VERSION
|
||||
# core: 8.x
|
||||
|
||||
# Information added by Drupal.org packaging script on 2016-08-03
|
||||
version: '8.1.8'
|
||||
# Information added by Drupal.org packaging script on 2017-08-16
|
||||
version: '8.3.7'
|
||||
core: '8.x'
|
||||
project: 'drupal'
|
||||
datestamp: 1470233790
|
||||
datestamp: 1502903957
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
name: 'Book test views'
|
||||
type: module
|
||||
description: 'Provides default views for views book tests.'
|
||||
package: Testing
|
||||
# version: VERSION
|
||||
# core: 8.x
|
||||
dependencies:
|
||||
- book
|
||||
- views
|
||||
|
||||
# Information added by Drupal.org packaging script on 2017-08-16
|
||||
version: '8.3.7'
|
||||
core: '8.x'
|
||||
project: 'drupal'
|
||||
datestamp: 1502903957
|
||||
+1059
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,204 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\book\Functional;
|
||||
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
|
||||
/**
|
||||
* Create a book, add pages, and test book interface.
|
||||
*
|
||||
* @group book
|
||||
*/
|
||||
class BookBreadcrumbTest extends BrowserTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['book', 'block', 'book_breadcrumb_test'];
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
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']);
|
||||
$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', 'administer content types', 'administer site configuration']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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($number, 2, '0', STR_PAD_LEFT) . ' - SimpleTest test node ' . $this->randomMachineName(10);
|
||||
$edit['body[0][value]'] = 'SimpleTest test body ' . $this->randomMachineName(32) . ' ' . $this->randomMachineName(32);
|
||||
$edit['book[bid]'] = $book_nid;
|
||||
|
||||
if ($parent !== NULL) {
|
||||
$this->drupalPostForm('node/add/book', $edit, t('Change book (update list of parents)'));
|
||||
|
||||
$edit['book[pid]'] = $parent;
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
// Make sure the parent was flagged as having children.
|
||||
$parent_node = \Drupal::entityManager()->getStorage('node')->loadUnchanged($parent);
|
||||
$this->assertFalse(empty($parent_node->book['has_children']), 'Parent node is marked as having children');
|
||||
}
|
||||
else {
|
||||
$this->drupalPostForm('node/add/book', $edit, t('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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the breadcrumb is updated when book content changes.
|
||||
*/
|
||||
public function testBreadcrumbTitleUpdates() {
|
||||
// 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[@class="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->assertEqual(5, count($got_breadcrumb));
|
||||
$this->assertEqual($nodes[3]->getTitle(), end($got_breadcrumb));
|
||||
$edit = [
|
||||
'title[0][value]' => 'Updated node5 title',
|
||||
];
|
||||
$this->drupalPostForm($nodes[3]->toUrl('edit-form'), $edit, 'Save');
|
||||
$this->drupalGet($nodes[4]->toUrl());
|
||||
// Fetch each node title in the current breadcrumb.
|
||||
$links = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a');
|
||||
$got_breadcrumb = [];
|
||||
foreach ($links as $link) {
|
||||
$got_breadcrumb[] = $link->getText();
|
||||
}
|
||||
$this->assertEqual(5, count($got_breadcrumb));
|
||||
$this->assertEqual($edit['title[0][value]'], end($got_breadcrumb));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the breadcrumb is updated when book access changes.
|
||||
*/
|
||||
public function testBreadcrumbAccessUpdates() {
|
||||
// Create a new book.
|
||||
$nodes = $this->createBreadcrumbBook();
|
||||
$this->drupalLogin($this->bookAuthor);
|
||||
$edit = [
|
||||
'title[0][value]' => "you can't see me",
|
||||
];
|
||||
$this->drupalPostForm($nodes[3]->toUrl('edit-form'), $edit, 'Save');
|
||||
$this->drupalGet($nodes[4]->toUrl());
|
||||
$links = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a');
|
||||
$got_breadcrumb = [];
|
||||
foreach ($links as $link) {
|
||||
$got_breadcrumb[] = $link->getText();
|
||||
}
|
||||
$this->assertEqual(5, count($got_breadcrumb));
|
||||
$this->assertEqual($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[@class="breadcrumb"]/ol/li/a');
|
||||
$got_breadcrumb = [];
|
||||
foreach ($links as $link) {
|
||||
$got_breadcrumb[] = $link->getText();
|
||||
}
|
||||
$this->assertEqual(4, count($got_breadcrumb));
|
||||
$this->assertEqual($nodes[2]->getTitle(), end($got_breadcrumb));
|
||||
$this->drupalGet($nodes[3]->toUrl());
|
||||
$this->assertResponse(403);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\book\Functional;
|
||||
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
use Drupal\Core\Config\PreExistingConfigException;
|
||||
|
||||
/**
|
||||
* Test installation of Book module.
|
||||
*
|
||||
* @group book
|
||||
*/
|
||||
class BookInstallTest extends BrowserTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['node'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test 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() {
|
||||
// Create a 'book' content type.
|
||||
$this->drupalCreateContentType(['type' => 'book']);
|
||||
|
||||
// Install the Book module.
|
||||
try {
|
||||
$this->container->get('module_installer')->install(['book']);
|
||||
}
|
||||
catch (PreExistingConfigException $e) {
|
||||
$this->fail("Expected exception thrown trying to install Book module: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,752 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\book\Functional;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
use Drupal\user\RoleInterface;
|
||||
|
||||
/**
|
||||
* Create a book, add pages, and test book interface.
|
||||
*
|
||||
* @group book
|
||||
*/
|
||||
class BookTest extends BrowserTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['book', 'block', 'node_access_test', 'book_test'];
|
||||
|
||||
/**
|
||||
* A book node.
|
||||
*
|
||||
* @var \Drupal\node\NodeInterface
|
||||
*/
|
||||
protected $book;
|
||||
|
||||
/**
|
||||
* A user with permission to create and edit books.
|
||||
*
|
||||
* @var object
|
||||
*/
|
||||
protected $bookAuthor;
|
||||
|
||||
/**
|
||||
* A user with permission to view a book and access printer-friendly version.
|
||||
*
|
||||
* @var object
|
||||
*/
|
||||
protected $webUser;
|
||||
|
||||
/**
|
||||
* A user with permission to create and edit books and to administer blocks.
|
||||
*
|
||||
* @var object
|
||||
*/
|
||||
protected $adminUser;
|
||||
|
||||
/**
|
||||
* A user without the 'node test view' permission.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $webUserWithoutNodeAccess;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
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']);
|
||||
$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']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new book with a page hierarchy.
|
||||
*
|
||||
* @return \Drupal\node\NodeInterface[]
|
||||
*/
|
||||
public function createBook() {
|
||||
// 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
|
||||
*/
|
||||
$nodes = [];
|
||||
$nodes[] = $this->createBookNode($book->id()); // Node 0.
|
||||
$nodes[] = $this->createBookNode($book->id(), $nodes[0]->book['nid']); // Node 1.
|
||||
$nodes[] = $this->createBookNode($book->id(), $nodes[0]->book['nid']); // Node 2.
|
||||
$nodes[] = $this->createBookNode($book->id()); // Node 3.
|
||||
$nodes[] = $this->createBookNode($book->id()); // Node 4.
|
||||
|
||||
$this->drupalLogout();
|
||||
|
||||
return $nodes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the book navigation cache context.
|
||||
*
|
||||
* @see \Drupal\book\Cache\BookNavigationCacheContext
|
||||
*/
|
||||
public function testBookNavigationCacheContext() {
|
||||
// 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->urlInfo());
|
||||
$this->assertRaw('[route.book_navigation]=book.none');
|
||||
|
||||
// On non-book node route.
|
||||
$this->drupalGet($page->urlInfo());
|
||||
$this->assertRaw('[route.book_navigation]=book.none');
|
||||
|
||||
// On book node route.
|
||||
$this->drupalGet($book_nodes[0]->urlInfo());
|
||||
$this->assertRaw('[route.book_navigation]=0|2|3');
|
||||
$this->drupalGet($book_nodes[1]->urlInfo());
|
||||
$this->assertRaw('[route.book_navigation]=0|2|3|4');
|
||||
$this->drupalGet($book_nodes[2]->urlInfo());
|
||||
$this->assertRaw('[route.book_navigation]=0|2|3|5');
|
||||
$this->drupalGet($book_nodes[3]->urlInfo());
|
||||
$this->assertRaw('[route.book_navigation]=0|2|6');
|
||||
$this->drupalGet($book_nodes[4]->urlInfo());
|
||||
$this->assertRaw('[route.book_navigation]=0|2|7');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests saving the book outline on an empty book.
|
||||
*/
|
||||
public function testEmptyBook() {
|
||||
// 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->drupalPostForm('admin/structure/book/' . $book->id(), [], t('Save book pages'));
|
||||
$this->assertText(t('Updated book @book.', ['@book' => $book->label()]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests book functionality through node interfaces.
|
||||
*/
|
||||
public function testBook() {
|
||||
// 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->assertCacheTag('config:book.settings');
|
||||
|
||||
/*
|
||||
* Add Node 5 under Node 3.
|
||||
* Book
|
||||
* |- Node 0
|
||||
* |- Node 1
|
||||
* |- Node 2
|
||||
* |- Node 3
|
||||
* |- Node 5
|
||||
* |- Node 4
|
||||
*/
|
||||
$nodes[] = $this->createBookNode($book->id(), $nodes[3]->book['nid']); // Node 5.
|
||||
$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->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('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 programatically.
|
||||
$this->drupalLogin($this->bookAuthor);
|
||||
$book = $this->createBookNode('new');
|
||||
$book->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* (optional) Previous link node. Defaults to FALSE.
|
||||
* @param $up
|
||||
* (optional) Up link node. Defaults to FALSE.
|
||||
* @param $next
|
||||
* (optional) Next link node. Defaults to FALSE.
|
||||
* @param array $breadcrumb
|
||||
* The nodes that should be displayed in the breadcrumb.
|
||||
*/
|
||||
public function checkBookNode(EntityInterface $node, $nodes, $previous = FALSE, $up = FALSE, $next = FALSE, array $breadcrumb) {
|
||||
// $number does not use drupal_static as it should not be reset
|
||||
// since it uniquely identifies each call to checkBookNode().
|
||||
static $number = 0;
|
||||
$this->drupalGet('node/' . $node->id());
|
||||
|
||||
// Check outline structure.
|
||||
if ($nodes !== NULL) {
|
||||
$this->assertPattern($this->generateOutlinePattern($nodes), format_string('Node @number outline confirmed.', ['@number' => $number]));
|
||||
}
|
||||
else {
|
||||
$this->pass(format_string('Node %number does not have outline.', ['%number' => $number]));
|
||||
}
|
||||
|
||||
// Check previous, up, and next links.
|
||||
if ($previous) {
|
||||
/** @var \Drupal\Core\Url $url */
|
||||
$url = $previous->urlInfo();
|
||||
$url->setOptions(['attributes' => ['rel' => ['prev'], 'title' => t('Go to previous page')]]);
|
||||
$text = SafeMarkup::format('<b>‹</b> @label', ['@label' => $previous->label()]);
|
||||
$this->assertRaw(\Drupal::l($text, $url), 'Previous page link found.');
|
||||
}
|
||||
|
||||
if ($up) {
|
||||
/** @var \Drupal\Core\Url $url */
|
||||
$url = $up->urlInfo();
|
||||
$url->setOptions(['attributes' => ['title' => t('Go to parent page')]]);
|
||||
$this->assertRaw(\Drupal::l('Up', $url), 'Up page link found.');
|
||||
}
|
||||
|
||||
if ($next) {
|
||||
/** @var \Drupal\Core\Url $url */
|
||||
$url = $next->urlInfo();
|
||||
$url->setOptions(['attributes' => ['rel' => ['next'], 'title' => t('Go to next page')]]);
|
||||
$text = SafeMarkup::format('@label <b>›</b>', ['@label' => $next->label()]);
|
||||
$this->assertRaw(\Drupal::l($text, $url), 'Next page link found.');
|
||||
}
|
||||
|
||||
// Compute the expected breadcrumb.
|
||||
$expected_breadcrumb = [];
|
||||
$expected_breadcrumb[] = \Drupal::url('<front>');
|
||||
foreach ($breadcrumb as $a_node) {
|
||||
$expected_breadcrumb[] = $a_node->url();
|
||||
}
|
||||
|
||||
// Fetch links in the current breadcrumb.
|
||||
$links = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a');
|
||||
$got_breadcrumb = [];
|
||||
foreach ($links as $link) {
|
||||
$got_breadcrumb[] = $link->getAttribute('href');
|
||||
}
|
||||
|
||||
// Compare expected and got breadcrumbs.
|
||||
$this->assertIdentical($expected_breadcrumb, $got_breadcrumb, 'The breadcrumb is correctly displayed on the page.');
|
||||
|
||||
// Check printer friendly version.
|
||||
$this->drupalGet('book/export/html/' . $node->id());
|
||||
$this->assertText($node->label(), 'Printer friendly title found.');
|
||||
$this->assertRaw($node->body->processed, 'Printer friendly body found.');
|
||||
|
||||
$number++;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public function generateOutlinePattern($nodes) {
|
||||
$outline = '';
|
||||
foreach ($nodes as $node) {
|
||||
$outline .= '(node\/' . $node->id() . ')(.*?)(' . $node->label() . ')(.*?)';
|
||||
}
|
||||
|
||||
return '/<nav id="book-navigation-' . $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.
|
||||
*
|
||||
* @return \Drupal\node\NodeInterface
|
||||
* The created node.
|
||||
*/
|
||||
public 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().
|
||||
static $number = 0; // Used to ensure that when sorted nodes stay in same order.
|
||||
|
||||
$edit = [];
|
||||
$edit['title[0][value]'] = str_pad($number, 2, '0', STR_PAD_LEFT) . ' - SimpleTest test node ' . $this->randomMachineName(10);
|
||||
$edit['body[0][value]'] = 'SimpleTest test body ' . $this->randomMachineName(32) . ' ' . $this->randomMachineName(32);
|
||||
$edit['book[bid]'] = $book_nid;
|
||||
|
||||
if ($parent !== NULL) {
|
||||
$this->drupalPostForm('node/add/book', $edit, t('Change book (update list of parents)'));
|
||||
|
||||
$edit['book[pid]'] = $parent;
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
// Make sure the parent was flagged as having children.
|
||||
$parent_node = \Drupal::entityManager()->getStorage('node')->loadUnchanged($parent);
|
||||
$this->assertFalse(empty($parent_node->book['has_children']), 'Parent node is marked as having children');
|
||||
}
|
||||
else {
|
||||
$this->drupalPostForm('node/add/book', $edit, t('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 book export ("printer-friendly version") functionality.
|
||||
*/
|
||||
public function testBookExport() {
|
||||
// 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(t('Printer-friendly version'));
|
||||
|
||||
// Make sure each part of the book is there.
|
||||
foreach ($nodes as $node) {
|
||||
$this->assertText($node->label(), 'Node title found in printer friendly version.');
|
||||
$this->assertRaw($node->body->processed, 'Node body found in printer friendly version.');
|
||||
}
|
||||
|
||||
// Make sure we can't export an unsupported format.
|
||||
$this->drupalGet('book/export/foobar/' . $this->book->id());
|
||||
$this->assertResponse('404', 'Unsupported export format returned "not found".');
|
||||
|
||||
// Make sure we get a 404 on a not existing book node.
|
||||
$this->drupalGet('book/export/html/123');
|
||||
$this->assertResponse('404', 'Not existing book node returned "not found".');
|
||||
|
||||
// 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->assertNoLink(t('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->assertResponse('403', 'Anonymous user properly forbidden.');
|
||||
|
||||
// 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->assertResponse('403', 'Anonymous user properly forbidden from seeing the printer-friendly version when denied by node access.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the functionality of the book navigation block.
|
||||
*/
|
||||
public function testBookNavigationBlock() {
|
||||
$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->drupalPostForm('admin/people/permissions/' . RoleInterface::ANONYMOUS_ID, $edit, t('Save permissions'));
|
||||
$this->assertText(t('The changes have been saved.'), "Permission 'node test view' successfully assigned to anonymous users.");
|
||||
|
||||
// Test correct display of the block.
|
||||
$nodes = $this->createBook();
|
||||
$this->drupalGet('<front>');
|
||||
$this->assertText($block->label(), 'Book navigation block is displayed.');
|
||||
$this->assertText($this->book->label(), format_string('Link to book root (@title) is displayed.', ['@title' => $nodes[0]->label()]));
|
||||
$this->assertNoText($nodes[0]->label(), 'No links to individual book pages are displayed.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests BookManager::getTableOfContents().
|
||||
*/
|
||||
public function testGetTableOfContents() {
|
||||
// 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'));
|
||||
// Snice Node 0 has children 2 levels deep, nodes 10 and 11 should not
|
||||
// appear in the selector.
|
||||
$this->assertNoOption('edit-book-pid', $nodes[10]->id());
|
||||
$this->assertNoOption('edit-book-pid', $nodes[11]->id());
|
||||
// Node 9 should be available as an option.
|
||||
$this->assertOption('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);
|
||||
$expected_nids = [$book->id(), $nodes[0]->id(), $nodes[1]->id(), $nodes[2]->id(), $nodes[3]->id(), $nodes[6]->id(), $nodes[4]->id()];
|
||||
$this->assertEqual(count($options), count($expected_nids));
|
||||
$diff = array_diff($expected_nids, array_keys($options));
|
||||
$this->assertTrue(empty($diff), 'Found all expected option keys');
|
||||
// Exclude Node 3.
|
||||
$options = $manager->getTableOfContents($book->id(), 3, [$nodes[3]->id()]);
|
||||
$expected_nids = [$book->id(), $nodes[0]->id(), $nodes[1]->id(), $nodes[2]->id(), $nodes[4]->id()];
|
||||
$this->assertEqual(count($options), count($expected_nids));
|
||||
$diff = array_diff($expected_nids, array_keys($options));
|
||||
$this->assertTrue(empty($diff), 'Found all expected option keys after excluding Node 3');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the book navigation block when an access module is installed.
|
||||
*/
|
||||
public function testNavigationBlockOnAccessModuleInstalled() {
|
||||
$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->drupalPostForm('admin/people/permissions/' . RoleInterface::ANONYMOUS_ID, $edit, t('Save permissions'));
|
||||
$this->assertText(t('The changes have been saved.'), "Permission 'node test view' successfully assigned to anonymous users.");
|
||||
|
||||
// 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->assertText($block->label(), 'Book navigation block is displayed to registered users.');
|
||||
$this->drupalLogout();
|
||||
|
||||
// Test correct display of the block to anonymous users.
|
||||
$this->drupalGet('node/' . $this->book->id());
|
||||
$this->assertText($block->label(), 'Book navigation block is displayed to anonymous users.');
|
||||
|
||||
// Test the 'book pages' block_mode setting.
|
||||
$this->drupalGet('<front>');
|
||||
$this->assertNoText($block->label(), 'Book navigation block is not shown on non-book pages.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the access for deleting top-level book nodes.
|
||||
*/
|
||||
public function testBookDelete() {
|
||||
$node_storage = $this->container->get('entity.manager')->getStorage('node');
|
||||
$nodes = $this->createBook();
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$edit = [];
|
||||
|
||||
// Test access to delete top-level and child book nodes.
|
||||
$this->drupalGet('node/' . $this->book->id() . '/outline/remove');
|
||||
$this->assertResponse('403', 'Deleting top-level book node properly forbidden.');
|
||||
$this->drupalPostForm('node/' . $nodes[4]->id() . '/outline/remove', $edit, t('Remove'));
|
||||
$node_storage->resetCache([$nodes[4]->id()]);
|
||||
$node4 = $node_storage->load($nodes[4]->id());
|
||||
$this->assertTrue(empty($node4->book), 'Deleting child book node properly allowed.');
|
||||
|
||||
// Delete all child book nodes and retest top-level node deletion.
|
||||
foreach ($nodes as $node) {
|
||||
$nids[] = $node->id();
|
||||
}
|
||||
entity_delete_multiple('node', $nids);
|
||||
$this->drupalPostForm('node/' . $this->book->id() . '/outline/remove', $edit, t('Remove'));
|
||||
$node_storage->resetCache([$this->book->id()]);
|
||||
$node = $node_storage->load($this->book->id());
|
||||
$this->assertTrue(empty($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->urlInfo('delete-form'));
|
||||
$this->assertRaw(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' => $this->book->label()]));
|
||||
// Delete parent, and visit a child page.
|
||||
$this->drupalPostForm($this->book->urlInfo('delete-form'), [], t('Delete'));
|
||||
$this->drupalGet($nodes[0]->urlInfo());
|
||||
$this->assertResponse(200);
|
||||
$this->assertText($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->assertEqual($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->assertEqual($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() {
|
||||
$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->assertNoLink(t('Book outline'), 'Book Author is not allowed to outline');
|
||||
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->drupalGet('node/' . $empty_book->id() . '/outline');
|
||||
$this->assertRaw(t('Book outline'));
|
||||
$this->assertOptionSelected('edit-book-bid', 0, 'Node does not belong to a book');
|
||||
$this->assertNoLink(t('Remove from book outline'));
|
||||
|
||||
$edit = [];
|
||||
$edit['book[bid]'] = '1';
|
||||
$this->drupalPostForm('node/' . $empty_book->id() . '/outline', $edit, t('Add to book outline'));
|
||||
$node = \Drupal::entityManager()->getStorage('node')->load($empty_book->id());
|
||||
// Test the book array.
|
||||
$this->assertEqual($node->book['nid'], $empty_book->id());
|
||||
$this->assertEqual($node->book['bid'], $empty_book->id());
|
||||
$this->assertEqual($node->book['depth'], 1);
|
||||
$this->assertEqual($node->book['p1'], $empty_book->id());
|
||||
$this->assertEqual($node->book['pid'], '0');
|
||||
|
||||
// Create new book.
|
||||
$this->drupalLogin($this->bookAuthor);
|
||||
$book = $this->createBookNode('new');
|
||||
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->drupalGet('node/' . $book->id() . '/outline');
|
||||
$this->assertRaw(t('Book outline'));
|
||||
$this->clickLink(t('Remove from book outline'));
|
||||
$this->assertRaw(t('Are you sure you want to remove %title from the book hierarchy?', ['%title' => $book->label()]));
|
||||
|
||||
// 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->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
|
||||
$node = \Drupal::entityManager()->getStorage('node')->load($node->id());
|
||||
|
||||
// Test the book array.
|
||||
$this->assertEqual($node->book['nid'], $node->id());
|
||||
$this->assertEqual($node->book['bid'], $node->id());
|
||||
$this->assertEqual($node->book['depth'], 1);
|
||||
$this->assertEqual($node->book['p1'], $node->id());
|
||||
$this->assertEqual($node->book['pid'], '0');
|
||||
|
||||
// Test the form itself.
|
||||
$this->drupalGet('node/' . $node->id() . '/edit');
|
||||
$this->assertOptionSelected('edit-book-bid', $node->id());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that saveBookLink() returns something.
|
||||
*/
|
||||
public function testSaveBookLink() {
|
||||
$book_manager = \Drupal::service('book.manager');
|
||||
|
||||
// Mock a link for a new book.
|
||||
$link = ['nid' => 1, 'has_children' => 0, 'original_bid' => 0, 'parent_depth_limit' => 8, 'pid' => 0, 'weight' => 0, 'bid' => 1];
|
||||
$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->assertEqual($return, $link);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the listing of all books.
|
||||
*/
|
||||
public function testBookListing() {
|
||||
// Create a new book.
|
||||
$this->createBook();
|
||||
|
||||
// Must be a user with 'node test view' permission since node_access_test is installed.
|
||||
$this->drupalLogin($this->webUser);
|
||||
|
||||
// Load the book page and assert the created book title is displayed.
|
||||
$this->drupalGet('book');
|
||||
|
||||
$this->assertText($this->book->label(), 'The book title is displayed on the book listing page.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the administrative listing of all books.
|
||||
*/
|
||||
public function testAdminBookListing() {
|
||||
// Create a new book.
|
||||
$this->createBook();
|
||||
|
||||
// Load the book page and assert the created book title is displayed.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->drupalGet('admin/structure/book');
|
||||
$this->assertText($this->book->label(), 'The book title is displayed on the administrative book listing page.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the administrative listing of all book pages in a book.
|
||||
*/
|
||||
public function testAdminBookNodeListing() {
|
||||
// Create a new book.
|
||||
$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->assertText($this->book->label(), 'The book title is displayed on the administrative book listing page.');
|
||||
|
||||
$elements = $this->xpath('//table//ul[@class="dropbutton"]/li/a');
|
||||
$this->assertEqual($elements[0]->getText(), 'View', 'View link is found from the list.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the loaded book in hook_node_load() does not depend on the user.
|
||||
*/
|
||||
public function testHookNodeLoadAccess() {
|
||||
\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::entityManager()->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->assertTrue(!empty($book_node->book));
|
||||
$this->assertEqual($book_node->book['bid'], $this->book->id());
|
||||
|
||||
// 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->assertTrue(!empty($book_node->book));
|
||||
$this->assertEqual($book_node->book['bid'], $this->book->id());
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
// 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 = [];
|
||||
$this->drupalPostForm('node/' . $this->book->id() . '/edit', $edit, t('Save and unpublish'));
|
||||
|
||||
// Test node page.
|
||||
$this->drupalGet('node/' . $this->book->id());
|
||||
$this->assertText($this->book->label(), 'Unpublished book with "Show block only on book pages" book navigation settings.');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\book\Functional\Views;
|
||||
|
||||
use Drupal\Tests\views\Functional\ViewTestBase;
|
||||
use Drupal\views\Tests\ViewTestData;
|
||||
|
||||
/**
|
||||
* Tests entity reference relationship data.
|
||||
*
|
||||
* @group book
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
public static $modules = ['book_test_views', 'book', 'views'];
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
parent::setUp($import_test_views);
|
||||
|
||||
// Create users.
|
||||
$this->bookAuthor = $this->drupalCreateUser(
|
||||
[
|
||||
'create new books',
|
||||
'create book content',
|
||||
'edit own book content',
|
||||
'add content to books',
|
||||
]
|
||||
);
|
||||
ViewTestData::createTestViews(get_class($this), ['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 . ' - SimpleTest test node ' . $this->randomMachineName(10);
|
||||
$edit['body[0][value]'] = 'SimpleTest test body ' . $this->randomMachineName(32) . ' ' . $this->randomMachineName(32);
|
||||
$edit['book[bid]'] = $book_nid;
|
||||
|
||||
if ($parent !== NULL) {
|
||||
$this->drupalPostForm('node/add/book', $edit, t('Change book (update list of parents)'));
|
||||
|
||||
$edit['book[pid]'] = $parent;
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
// Make sure the parent was flagged as having children.
|
||||
$parent_node = \Drupal::entityManager()->getStorage('node')->loadUnchanged($parent);
|
||||
$this->assertFalse(empty($parent_node->book['has_children']), 'Parent node is marked as having children');
|
||||
}
|
||||
else {
|
||||
$this->drupalPostForm('node/add/book', $edit, t('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() {
|
||||
|
||||
// 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->assertLink($nodes[$j]->label());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\book\FunctionalJavascript;
|
||||
|
||||
use Behat\Mink\Exception\ExpectationException;
|
||||
use Drupal\Component\Render\FormattableMarkup;
|
||||
use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
|
||||
use Drupal\node\Entity\Node;
|
||||
|
||||
/**
|
||||
* Tests Book javascript functionality.
|
||||
*
|
||||
* @group book
|
||||
*/
|
||||
class BookJavascriptTest extends JavascriptTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['book'];
|
||||
|
||||
/**
|
||||
* Tests re-ordering of books.
|
||||
*/
|
||||
public function testBookOrdering() {
|
||||
$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.
|
||||
*
|
||||
* @todo Remove this once https://www.drupal.org/node/2817657 is committed.
|
||||
*/
|
||||
protected function assertOrderInPage(array $items) {
|
||||
$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.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -27,11 +27,11 @@ class BookUninstallTest extends KernelTestBase {
|
||||
parent::setUp();
|
||||
$this->installEntitySchema('user');
|
||||
$this->installEntitySchema('node');
|
||||
$this->installSchema('book', array('book'));
|
||||
$this->installSchema('node', array('node_access'));
|
||||
$this->installConfig(array('node', 'book', 'field'));
|
||||
$this->installSchema('book', ['book']);
|
||||
$this->installSchema('node', ['node_access']);
|
||||
$this->installConfig(['node', 'book', 'field']);
|
||||
// For uninstall to work.
|
||||
$this->installSchema('user', array('users_data'));
|
||||
$this->installSchema('user', ['users_data']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,17 +42,17 @@ class BookUninstallTest extends KernelTestBase {
|
||||
$validation_reasons = \Drupal::service('module_installer')->validateUninstall(['book']);
|
||||
$this->assertEqual([], $validation_reasons, 'The book module is not required.');
|
||||
|
||||
$content_type = NodeType::create(array(
|
||||
$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(array('title' => $this->randomString(), 'type' => $content_type->id()));
|
||||
$node = Node::create(['title' => $this->randomString(), 'type' => $content_type->id()]);
|
||||
$node->book['bid'] = 'new';
|
||||
$node->save();
|
||||
|
||||
@@ -60,7 +60,7 @@ class BookUninstallTest extends KernelTestBase {
|
||||
$validation_reasons = \Drupal::service('module_installer')->validateUninstall(['book']);
|
||||
$this->assertEqual(['To uninstall Book, delete all content that is part of a book'], $validation_reasons['book']);
|
||||
|
||||
$book_node = Node::create(array('title' => $this->randomString(), 'type' => 'book'));
|
||||
$book_node = Node::create(['title' => $this->randomString(), 'type' => 'book']);
|
||||
$book_node->book['bid'] = FALSE;
|
||||
$book_node->save();
|
||||
|
||||
@@ -79,7 +79,7 @@ class BookUninstallTest extends KernelTestBase {
|
||||
$module_data = _system_rebuild_module_data();
|
||||
$this->assertFalse(isset($module_data['book']->info['required']), 'The book module is not required.');
|
||||
|
||||
$node = Node::create(array('title' => $this->randomString(), 'type' => $content_type->id()));
|
||||
$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.
|
||||
@@ -87,7 +87,7 @@ class BookUninstallTest extends KernelTestBase {
|
||||
$this->assertEqual([], $validation_reasons, 'The book module is not required.');
|
||||
|
||||
// Uninstall the Book module and check the node type is deleted.
|
||||
\Drupal::service('module_installer')->uninstall(array('book'));
|
||||
\Drupal::service('module_installer')->uninstall(['book']);
|
||||
$this->assertNull(NodeType::load('book'), "The book node type does not exist.");
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Drupal\Tests\book\Kernel\Migrate\d6;
|
||||
|
||||
use Drupal\config\Tests\SchemaCheckTestTrait;
|
||||
use Drupal\Tests\SchemaCheckTestTrait;
|
||||
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
|
||||
|
||||
/**
|
||||
@@ -33,8 +33,8 @@ class MigrateBookConfigsTest extends MigrateDrupal6TestBase {
|
||||
public function testBookSettings() {
|
||||
$config = $this->config('book.settings');
|
||||
$this->assertIdentical('book', $config->get('child_type'));
|
||||
$this->assertIdentical('all pages', $config->get('block.navigation.mode'));
|
||||
$this->assertIdentical(array('book'), $config->get('allowed_types'));
|
||||
$this->assertSame('book pages', $config->get('block.navigation.mode'));
|
||||
$this->assertIdentical(['book'], $config->get('allowed_types'));
|
||||
$this->assertConfigSchema(\Drupal::service('config.typed'), 'book.settings', $config->get());
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ class MigrateBookTest extends MigrateDrupal6TestBase {
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['book'];
|
||||
public static $modules = ['book', 'menu_ui'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
@@ -32,7 +32,7 @@ class MigrateBookTest extends MigrateDrupal6TestBase {
|
||||
* Tests the Drupal 6 book structure to Drupal 8 migration.
|
||||
*/
|
||||
public function testBook() {
|
||||
$nodes = Node::loadMultiple(array(4, 5, 6, 7, 8));
|
||||
$nodes = Node::loadMultiple([4, 5, 6, 7, 8]);
|
||||
$this->assertIdentical('4', $nodes[4]->book['bid']);
|
||||
$this->assertIdentical('0', $nodes[4]->book['pid']);
|
||||
|
||||
@@ -53,8 +53,8 @@ class MigrateBookTest extends MigrateDrupal6TestBase {
|
||||
$this->assertIdentical('5', $tree['49990 Node 4 4']['below']['50000 Node 5 5']['link']['nid']);
|
||||
$this->assertIdentical('6', $tree['49990 Node 4 4']['below']['50000 Node 5 5']['below']['50000 Node 6 6']['link']['nid']);
|
||||
$this->assertIdentical('7', $tree['49990 Node 4 4']['below']['50000 Node 5 5']['below']['50000 Node 7 7']['link']['nid']);
|
||||
$this->assertIdentical(array(), $tree['49990 Node 4 4']['below']['50000 Node 5 5']['below']['50000 Node 6 6']['below']);
|
||||
$this->assertIdentical(array(), $tree['49990 Node 4 4']['below']['50000 Node 5 5']['below']['50000 Node 7 7']['below']);
|
||||
$this->assertIdentical([], $tree['49990 Node 4 4']['below']['50000 Node 5 5']['below']['50000 Node 6 6']['below']);
|
||||
$this->assertIdentical([], $tree['49990 Node 4 4']['below']['50000 Node 5 5']['below']['50000 Node 7 7']['below']);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\book\Kernel\Plugin\migrate\source\d6;
|
||||
|
||||
use Drupal\Tests\migrate\Kernel\MigrateSqlSourceTestBase;
|
||||
|
||||
/**
|
||||
* @covers \Drupal\book\Plugin\migrate\source\d6\Book
|
||||
* @group book
|
||||
*/
|
||||
class BookTest extends MigrateSqlSourceTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['book', 'migrate_drupal'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -59,7 +59,7 @@ class BookManagerTest extends UnitTestCase {
|
||||
protected function setUp() {
|
||||
$this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
|
||||
$this->translation = $this->getStringTranslationStub();
|
||||
$this->configFactory = $this->getConfigFactoryStub(array());
|
||||
$this->configFactory = $this->getConfigFactoryStub([]);
|
||||
$this->bookOutlineStorage = $this->getMock('Drupal\book\BookOutlineStorageInterface');
|
||||
$this->renderer = $this->getMock('\Drupal\Core\Render\RendererInterface');
|
||||
$this->bookManager = new BookManager($this->entityManager, $this->translation, $this->configFactory, $this->bookOutlineStorage, $this->renderer);
|
||||
@@ -81,7 +81,7 @@ class BookManagerTest extends UnitTestCase {
|
||||
* The test data.
|
||||
*/
|
||||
public function providerTestGetBookParents() {
|
||||
$empty = array(
|
||||
$empty = [
|
||||
'p1' => 0,
|
||||
'p2' => 0,
|
||||
'p3' => 0,
|
||||
@@ -91,27 +91,27 @@ class BookManagerTest extends UnitTestCase {
|
||||
'p7' => 0,
|
||||
'p8' => 0,
|
||||
'p9' => 0,
|
||||
);
|
||||
return array(
|
||||
];
|
||||
return [
|
||||
// Provides a book without an existing parent.
|
||||
array(
|
||||
array('pid' => 0, 'nid' => 12),
|
||||
array(),
|
||||
array('depth' => 1, 'p1' => 12) + $empty,
|
||||
),
|
||||
[
|
||||
['pid' => 0, 'nid' => 12],
|
||||
[],
|
||||
['depth' => 1, 'p1' => 12] + $empty,
|
||||
],
|
||||
// Provides a book with an existing parent.
|
||||
array(
|
||||
array('pid' => 11, 'nid' => 12),
|
||||
array('nid' => 11, 'depth' => 1, 'p1' => 11,),
|
||||
array('depth' => 2, 'p1' => 11, 'p2' => 12) + $empty,
|
||||
),
|
||||
[
|
||||
['pid' => 11, 'nid' => 12],
|
||||
['nid' => 11, 'depth' => 1, 'p1' => 11],
|
||||
['depth' => 2, 'p1' => 11, 'p2' => 12] + $empty,
|
||||
],
|
||||
// Provides a book with two existing parents.
|
||||
array(
|
||||
array('pid' => 11, 'nid' => 12),
|
||||
array('nid' => 11, 'depth' => 2, 'p1' => 10, 'p2' => 11),
|
||||
array('depth' => 3, 'p1' => 10, 'p2' => 11, 'p3' => 12) + $empty,
|
||||
),
|
||||
);
|
||||
[
|
||||
['pid' => 11, 'nid' => 12],
|
||||
['nid' => 11, 'depth' => 2, 'p1' => 10, 'p2' => 11],
|
||||
['depth' => 3, 'p1' => 10, 'p2' => 11, 'p3' => 12] + $empty,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,10 +12,10 @@ use Drupal\Tests\Core\Menu\LocalTaskIntegrationTestBase;
|
||||
class BookLocalTasksTest extends LocalTaskIntegrationTestBase {
|
||||
|
||||
protected function setUp() {
|
||||
$this->directoryList = array(
|
||||
$this->directoryList = [
|
||||
'book' => 'core/modules/book',
|
||||
'node' => 'core/modules/node',
|
||||
);
|
||||
];
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
@@ -26,19 +26,19 @@ class BookLocalTasksTest extends LocalTaskIntegrationTestBase {
|
||||
*/
|
||||
public function testBookAdminLocalTasks($route) {
|
||||
|
||||
$this->assertLocalTasks($route, array(
|
||||
0 => array('book.admin', 'book.settings'),
|
||||
));
|
||||
$this->assertLocalTasks($route, [
|
||||
0 => ['book.admin', 'book.settings'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a list of routes to test.
|
||||
*/
|
||||
public function getBookAdminRoutes() {
|
||||
return array(
|
||||
array('book.admin'),
|
||||
array('book.settings'),
|
||||
);
|
||||
return [
|
||||
['book.admin'],
|
||||
['book.settings'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,19 +47,19 @@ class BookLocalTasksTest extends LocalTaskIntegrationTestBase {
|
||||
* @dataProvider getBookNodeRoutes
|
||||
*/
|
||||
public function testBookNodeLocalTasks($route) {
|
||||
$this->assertLocalTasks($route, array(
|
||||
0 => array('entity.node.book_outline_form', 'entity.node.canonical', 'entity.node.edit_form', 'entity.node.delete_form', 'entity.node.version_history',),
|
||||
));
|
||||
$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 function getBookNodeRoutes() {
|
||||
return array(
|
||||
array('entity.node.canonical'),
|
||||
array('entity.node.book_outline_form'),
|
||||
);
|
||||
return [
|
||||
['entity.node.canonical'],
|
||||
['entity.node.book_outline_form'],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user