diff --git a/core/modules/book/book.module b/core/modules/book/book.module index 8bdfcfc..f6764ff 100644 --- a/core/modules/book/book.module +++ b/core/modules/book/book.module @@ -1117,29 +1117,6 @@ function template_preprocess_book_export_html(&$variables) { } /** - * Generates printer-friendly HTML for a node. - * - * @param \Drupal\Core\Entity\EntityInterface $node - * The node that will be output. - * @param $children - * (optional) All the rendered child nodes within the current node. Defaults - * to an empty string. - * - * @return - * The HTML generated for the given node. - * - * @see book_export_traverse() - */ -function book_node_export(EntityInterface $node, $children = '') { - $build = node_view($node, 'print'); - unset($build['#theme']); - // @todo Rendering should happen in the template using render(). - $node->rendered = drupal_render($build); - $book_node_export_html = array('#theme' => 'book_node_export_html', '#node' => $node, '#children' => $children ); - return drupal_render($book_node_export_html); -} - -/** * Prepares variables for single node export templates. * * Default template: book-node-export-html.html.twig. diff --git a/core/modules/book/book.routing.yml b/core/modules/book/book.routing.yml index ac3cfd3..0b0f2a6 100644 --- a/core/modules/book/book.routing.yml +++ b/core/modules/book/book.routing.yml @@ -24,4 +24,5 @@ book_export: defaults: _content: '\Drupal\book\Controller\BookController::bookExport' requirements: - _book_export_access: 'TRUE' + _permission: 'access printer-friendly version' + _entity_access: 'node.view' diff --git a/core/modules/book/book.services.yml b/core/modules/book/book.services.yml index 7030606..042df96 100644 --- a/core/modules/book/book.services.yml +++ b/core/modules/book/book.services.yml @@ -2,9 +2,6 @@ services: book.manager: class: Drupal\book\BookManager arguments: ['@database', '@plugin.manager.entity'] - access_check.book_export: - class: Drupal\book\Access\BookAccessCheck - tags: - - { name: access_check } book.export: class: Drupal\book\BookExport + arguments: ['@plugin.manager.entity'] diff --git a/core/modules/book/lib/Drupal/book/Access/BookAccessCheck.php b/core/modules/book/lib/Drupal/book/Access/BookAccessCheck.php deleted file mode 100644 index 8ada281..0000000 --- a/core/modules/book/lib/Drupal/book/Access/BookAccessCheck.php +++ /dev/null @@ -1,34 +0,0 @@ -getRequirements()); - } - - /** - * {@inheritdoc} - */ - public function access(Route $route, Request $request) { - return user_access('access printer-friendly version') && node_access('view', $request->attributes->get('node')); - } - -} diff --git a/core/modules/book/lib/Drupal/book/BookExport.php b/core/modules/book/lib/Drupal/book/BookExport.php index 6f47b46..39117bd 100644 --- a/core/modules/book/lib/Drupal/book/BookExport.php +++ b/core/modules/book/lib/Drupal/book/BookExport.php @@ -1,22 +1,49 @@ nodeStorage = $entityManager->getStorageController('node'); + $this->nodeRender = $entityManager->getRenderController('node'); + } + + /** * Generates HTML for export when invoked by book_export(). * * The given node is embedded to its absolute depth in a top level section. For @@ -31,65 +58,81 @@ class BookExport { * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException * Thrown when the node was not attached to a book. - * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException - * Thrown when the user does not have access to export a book. - * - * @internal param $ \Drupal\node\Plugin\Core\Entity\Node - * The node to export. * * @return string A string containing HTML representing the node and its children in */ public function bookExportHtml(NodeInterface $node) { - if (!user_access('access printer-friendly version')) { - throw new AccessDeniedHttpException(); - } - if (!isset($node->book)) { throw new NotFoundHttpException(); } $tree = book_menu_subtree_data($node->book); - $contents = $this->exportTraverse($tree, 'book_node_export'); - return theme('book_export_html', array('title' => $node->label(), 'contents' => $contents, 'depth' => $node->book['depth'])); + $contents = $this->exportTraverse($tree, array($this, 'bookNodeExport')); + return array( + '#theme' => 'book_export_html', + '#title' => $node->label(), + '#contents' => $contents, + '#depth' => $node->book['depth'], + ); } /** * Traverses the book tree to build printable or exportable output. * - * During the traversal, the $visit_func() callback is applied to each node and - * is called recursively for each child of the node (in weight, title order). + * 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 string $visit_func - * A function callback to be called upon visiting a node in the tree. + * @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($tree, $visit_func) { - $output = ''; + protected function exportTraverse(array $tree, $callable) { + // If there is no valid callable, use the default callback. + $callable = !empty($callable) ? $callable : array($this, 'bookNodeExport'); + $output = ''; foreach ($tree as $data) { // Note- access checking is already performed when building the tree. - if ($node = node_load($data['link']['nid'])) { - $children = ''; + if ($node = $this->nodeStorage->load($data['link']['nid'])) { + $children = $data['below'] ? $this->exportTraverse($data['below'], $callable) : ''; - if ($data['below']) { - $children = $this->exportTraverse($data['below'], $visit_func); - } - - if (!empty($visit_func)) { - $output .= call_user_func($visit_func, $node, $children); - } - else { - // Use the default function. - $output .= book_node_export($node, $children); - } + $callable_output = call_user_func($callable, $node, $children); + $output .= drupal_render($callable_output); } } return $output; } + /** + * 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 string + * The HTML generated for the given node. + * + * @see book_export_traverse() + */ + protected function bookNodeExport(NodeInterface $node, $children = '') { + $build = $this->nodeRender->view($node, 'print', NULL); + unset($build['#theme']); + + // @todo Rendering should happen in the template using render(). + $node->rendered = drupal_render($build); + return array( + '#theme' => 'book_node_export_html', + '#node' => $node, + '#children' => $children, + ); + } + }