PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/book/src/Controller/BookController.php

http://github.com/drupal/drupal
PHP | 166 lines | 83 code | 15 blank | 68 comment | 2 complexity | a0b154f2724dd037486875906e620099 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\book\Controller;
  3. use Drupal\book\BookExport;
  4. use Drupal\book\BookManagerInterface;
  5. use Drupal\Core\Controller\ControllerBase;
  6. use Drupal\Core\Link;
  7. use Drupal\Core\Render\RendererInterface;
  8. use Drupal\Core\Url;
  9. use Drupal\node\NodeInterface;
  10. use Symfony\Component\DependencyInjection\Container;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. /**
  15. * Controller routines for book routes.
  16. */
  17. class BookController extends ControllerBase {
  18. /**
  19. * The book manager.
  20. *
  21. * @var \Drupal\book\BookManagerInterface
  22. */
  23. protected $bookManager;
  24. /**
  25. * The book export service.
  26. *
  27. * @var \Drupal\book\BookExport
  28. */
  29. protected $bookExport;
  30. /**
  31. * The renderer.
  32. *
  33. * @var \Drupal\Core\Render\RendererInterface
  34. */
  35. protected $renderer;
  36. /**
  37. * Constructs a BookController object.
  38. *
  39. * @param \Drupal\book\BookManagerInterface $bookManager
  40. * The book manager.
  41. * @param \Drupal\book\BookExport $bookExport
  42. * The book export service.
  43. * @param \Drupal\Core\Render\RendererInterface $renderer
  44. * The renderer.
  45. */
  46. public function __construct(BookManagerInterface $bookManager, BookExport $bookExport, RendererInterface $renderer) {
  47. $this->bookManager = $bookManager;
  48. $this->bookExport = $bookExport;
  49. $this->renderer = $renderer;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public static function create(ContainerInterface $container) {
  55. return new static(
  56. $container->get('book.manager'),
  57. $container->get('book.export'),
  58. $container->get('renderer')
  59. );
  60. }
  61. /**
  62. * Returns an administrative overview of all books.
  63. *
  64. * @return array
  65. * A render array representing the administrative page content.
  66. */
  67. public function adminOverview() {
  68. $rows = [];
  69. $headers = [t('Book'), t('Operations')];
  70. // Add any recognized books to the table list.
  71. foreach ($this->bookManager->getAllBooks() as $book) {
  72. /** @var \Drupal\Core\Url $url */
  73. $url = $book['url'];
  74. if (isset($book['options'])) {
  75. $url->setOptions($book['options']);
  76. }
  77. $row = [
  78. Link::fromTextAndUrl($book['title'], $url),
  79. ];
  80. $links = [];
  81. $links['edit'] = [
  82. 'title' => t('Edit order and titles'),
  83. 'url' => Url::fromRoute('book.admin_edit', ['node' => $book['nid']]),
  84. ];
  85. $row[] = [
  86. 'data' => [
  87. '#type' => 'operations',
  88. '#links' => $links,
  89. ],
  90. ];
  91. $rows[] = $row;
  92. }
  93. return [
  94. '#type' => 'table',
  95. '#header' => $headers,
  96. '#rows' => $rows,
  97. '#empty' => t('No books available.'),
  98. ];
  99. }
  100. /**
  101. * Prints a listing of all books.
  102. *
  103. * @return array
  104. * A render array representing the listing of all books content.
  105. */
  106. public function bookRender() {
  107. $book_list = [];
  108. foreach ($this->bookManager->getAllBooks() as $book) {
  109. $book_list[] = Link::fromTextAndUrl($book['title'], $book['url']);
  110. }
  111. return [
  112. '#theme' => 'item_list',
  113. '#items' => $book_list,
  114. '#cache' => [
  115. 'tags' => $this->entityTypeManager()->getDefinition('node')->getListCacheTags(),
  116. ],
  117. ];
  118. }
  119. /**
  120. * Generates representations of a book page and its children.
  121. *
  122. * The method delegates the generation of output to helper methods. The method
  123. * name is derived by prepending 'bookExport' to the camelized form of given
  124. * output type. For example, a type of 'html' results in a call to the method
  125. * bookExportHtml().
  126. *
  127. * @param string $type
  128. * A string encoding the type of output requested. The following types are
  129. * currently supported in book module:
  130. * - html: Printer-friendly HTML.
  131. * Other types may be supported in contributed modules.
  132. * @param \Drupal\node\NodeInterface $node
  133. * The node to export.
  134. *
  135. * @return array
  136. * A render array representing the node and its children in the book
  137. * hierarchy in a format determined by the $type parameter.
  138. *
  139. * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  140. */
  141. public function bookExport($type, NodeInterface $node) {
  142. $method = 'bookExport' . Container::camelize($type);
  143. // @todo Convert the custom export functionality to serializer.
  144. if (!method_exists($this->bookExport, $method)) {
  145. $this->messenger()->addStatus(t('Unknown export format.'));
  146. throw new NotFoundHttpException();
  147. }
  148. $exported_book = $this->bookExport->{$method}($node);
  149. return new Response($this->renderer->renderRoot($exported_book));
  150. }
  151. }