diff -u b/core/modules/book/book.services.yml b/core/modules/book/book.services.yml --- b/core/modules/book/book.services.yml +++ b/core/modules/book/book.services.yml @@ -6,3 +6,5 @@ - class: Drupal\book\Access\BookExport + class: Drupal\book\Access\BookAccessCheck tags: - { name: access_check } + book.export: + class: Drupal\book\BookExport reverted: --- b/core/modules/book/lib/Drupal/book/Access/BookExport.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 -u b/core/modules/book/lib/Drupal/book/Controller/BookController.php b/core/modules/book/lib/Drupal/book/Controller/BookController.php --- b/core/modules/book/lib/Drupal/book/Controller/BookController.php +++ b/core/modules/book/lib/Drupal/book/Controller/BookController.php @@ -9,13 +9,11 @@ use Drupal\Core\Controller\ControllerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Drupal\Component\Utility\Unicode; use Drupal\node\NodeInterface; use Drupal\book\BookManager; -use Drupal\node\Plugin\Core\Entity\Node; /** * Controller routines for book routes. @@ -118,92 +116,15 @@ $type = Unicode::strtolower($type); - // @todo Conver the custom export functionality to serializer. + // @todo Convert the custom export functionality to serializer. + $book_export_service = \Drupal::service('book.export'); $method = 'bookExport' . $type; - if (method_exists($this, $method)) { - return new Response($this->{$method}($node)); - } - else { + if (!method_exists($book_export_service, $method)) { drupal_set_message(t('Unknown export format.')); throw new NotFoundHttpException(); } - } - /** - * 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)
elements corresponding to depth 0 and depth 1. - * This is intended to support WYSIWYG output - e.g., level 3 sections always - * look like level 3 sections, no matter their depth relative to the node - * selected to be exported as printer-friendly HTML. - * - * @param \Drupal\node\NodeInterface $node - * The node to export. - * - * @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')) { - if (isset($node->book)) { - $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'])); - } - else { - throw new NotFoundHttpException(); - } - } - else { - throw new AccessDeniedHttpException(); - } + return new Response($book_export_service->{$method}($node)); } - /** - * 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). - * - * @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. - * - * @return string - * The output generated in visiting each node. - */ - protected function exportTraverse($tree, $visit_func) { - $output = ''; - - foreach ($tree as $data) { - // Note- access checking is already performed when building the tree. - if ($node = node_load($data['link']['nid'])) { - $children = ''; - - 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); - } - } - } - - return $output; - } } only in patch2: unchanged: --- /dev/null +++ b/core/modules/book/lib/Drupal/book/Access/BookAccessCheck.php @@ -0,0 +1,34 @@ +getRequirements()); + } + + /** + * {@inheritdoc} + */ + public function access(Route $route, Request $request) { + return user_access('access printer-friendly version') && node_access('view', $request->attributes->get('node')); + } + +} only in patch2: unchanged: --- /dev/null +++ b/core/modules/book/lib/Drupal/book/BookExport.php @@ -0,0 +1,95 @@ + elements corresponding to depth 0 and depth 1. + * This is intended to support WYSIWYG output - e.g., level 3 sections always + * look like level 3 sections, no matter their depth relative to the node + * selected to be exported as printer-friendly HTML. + * + * @param \Drupal\node\NodeInterface $node + * The node to export. + * + * @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'])); + } + + /** + * 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). + * + * @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. + * + * @return string + * The output generated in visiting each node. + */ + protected function exportTraverse($tree, $visit_func) { + $output = ''; + + foreach ($tree as $data) { + // Note- access checking is already performed when building the tree. + if ($node = node_load($data['link']['nid'])) { + $children = ''; + + 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); + } + } + } + + return $output; + } + +}