Move all files to 2017/

This commit is contained in:
Oliver Davies 2025-09-29 22:25:17 +01:00
parent ac7370f67f
commit 2875863330
15717 changed files with 0 additions and 0 deletions

View file

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

View file

@ -0,0 +1,90 @@
<?php
namespace Drupal\book;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\node\NodeInterface;
/**
* Provides a breadcrumb builder for nodes in a book.
*/
class BookBreadcrumbBuilder implements BreadcrumbBuilderInterface {
use StringTranslationTrait;
/**
* The node storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $nodeStorage;
/**
* The current user account.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;
/**
* Constructs the BookBreadcrumbBuilder.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager service.
* @param \Drupal\Core\Session\AccountInterface $account
* The current user account.
*/
public function __construct(EntityManagerInterface $entity_manager, AccountInterface $account) {
$this->nodeStorage = $entity_manager->getStorage('node');
$this->account = $account;
}
/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match) {
$node = $route_match->getParameter('node');
return $node instanceof NodeInterface && !empty($node->book);
}
/**
* {@inheritdoc}
*/
public function build(RouteMatchInterface $route_match) {
$book_nids = [];
$breadcrumb = new Breadcrumb();
$links = [Link::createFromRoute($this->t('Home'), '<front>')];
$book = $route_match->getParameter('node')->book;
$depth = 1;
// We skip the current node.
while (!empty($book['p' . ($depth + 1)])) {
$book_nids[] = $book['p' . $depth];
$depth++;
}
$parent_books = $this->nodeStorage->loadMultiple($book_nids);
if (count($parent_books) > 0) {
$depth = 1;
while (!empty($book['p' . ($depth + 1)])) {
if (!empty($parent_books[$book['p' . $depth]]) && ($parent_book = $parent_books[$book['p' . $depth]])) {
$access = $parent_book->access('view', $this->account, TRUE);
$breadcrumb->addCacheableDependency($access);
if ($access->isAllowed()) {
$breadcrumb->addCacheableDependency($parent_book);
$links[] = Link::createFromRoute($parent_book->label(), 'entity.node.canonical', ['node' => $parent_book->id()]);
}
}
$depth++;
}
}
$breadcrumb->setLinks($links);
$breadcrumb->addCacheContexts(['route.book_navigation']);
return $breadcrumb;
}
}

View file

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

File diff suppressed because it is too large Load diff

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -0,0 +1,92 @@
<?php
namespace Drupal\book\Cache;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\Context\CacheContextInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Defines the book navigation cache context service.
*
* Cache context ID: 'route.book_navigation'.
*
* This allows for book navigation location-aware caching. It depends on:
* - whether the current route represents a book node at all
* - and if so, where in the book hierarchy we are
*
* This class is container-aware to avoid initializing the 'book.manager'
* service when it is not necessary.
*/
class BookNavigationCacheContext implements CacheContextInterface, ContainerAwareInterface {
use ContainerAwareTrait;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* Constructs a new BookNavigationCacheContext service.
*
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
*/
public function __construct(RequestStack $request_stack) {
$this->requestStack = $request_stack;
}
/**
* {@inheritdoc}
*/
public static function getLabel() {
return t("Book navigation");
}
/**
* {@inheritdoc}
*/
public function getContext() {
// Find the current book's ID.
$current_bid = 0;
if ($node = $this->requestStack->getCurrentRequest()->get('node')) {
$current_bid = empty($node->book['bid']) ? 0 : $node->book['bid'];
}
// If we're not looking at a book node, then we're not navigating a book.
if ($current_bid === 0) {
return 'book.none';
}
// If we're looking at a book node, get the trail for that node.
$active_trail = $this->container->get('book.manager')
->getActiveTrailIds($node->book['bid'], $node->book);
return implode('|', $active_trail);
}
/**
* {@inheritdoc}
*/
public function getCacheableMetadata() {
// The book active trail depends on the node and data attached to it.
// That information is however not stored as part of the node.
$cacheable_metadata = new CacheableMetadata();
if ($node = $this->requestStack->getCurrentRequest()->get('node')) {
// If the node is part of a book then we can use the cache tag for that
// book. If not, then it can't be optimized away.
if (!empty($node->book['bid'])) {
$cacheable_metadata->addCacheTags(['bid:' . $node->book['bid']]);
}
else {
$cacheable_metadata->setCacheMaxAge(0);
}
}
return $cacheable_metadata;
}
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -0,0 +1,31 @@
<?php
namespace Drupal\book\Plugin\migrate\destination;
use Drupal\Core\Entity\EntityInterface;
use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
use Drupal\migrate\Row;
/**
* @MigrateDestination(
* id = "book",
* provider = "book"
* )
*/
class Book extends EntityContentBase {
/**
* {@inheritdoc}
*/
protected static function getEntityTypeId($plugin_id) {
return 'node';
}
/**
* {@inheritdoc}
*/
protected function updateEntity(EntityInterface $entity, Row $row) {
$entity->book = $row->getDestinationProperty('book');
}
}

View file

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

View file

@ -0,0 +1,21 @@
<?php
namespace Drupal\book\Plugin\migrate\source\d6;
use Drupal\book\Plugin\migrate\source\Book as BookGeneral;
@trigger_error('Book is deprecated in Drupal 8.6.x and will be removed before Drupal 9.0.x. Use \Drupal\book\Plugin\migrate\source\Book instead. See https://www.drupal.org/node/2947487 for more information.', E_USER_DEPRECATED);
/**
* Drupal 6 book source.
*
* @MigrateSource(
* id = "d6_book",
* source_module = "book"
* )
*
* @deprecated in Drupal 8.6.x, to be removed before Drupal 9.0.x. Use
* \Drupal\book\Plugin\migrate\source\Book instead. See
* https://www.drupal.org/node/2947487 for more information.
*/
class Book extends BookGeneral {}

View file

@ -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'];
}
}
}
}

View file

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