3 namespace Drupal\book\Controller;
5 use Drupal\book\BookExport;
6 use Drupal\book\BookManagerInterface;
7 use Drupal\Core\Controller\ControllerBase;
8 use Drupal\Core\Render\RendererInterface;
10 use Drupal\node\NodeInterface;
11 use Symfony\Component\DependencyInjection\Container;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13 use Symfony\Component\HttpFoundation\Response;
14 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
17 * Controller routines for book routes.
19 class BookController extends ControllerBase {
24 * @var \Drupal\book\BookManagerInterface
26 protected $bookManager;
29 * The book export service.
31 * @var \Drupal\book\BookExport
33 protected $bookExport;
38 * @var \Drupal\Core\Render\RendererInterface
43 * Constructs a BookController object.
45 * @param \Drupal\book\BookManagerInterface $bookManager
47 * @param \Drupal\book\BookExport $bookExport
48 * The book export service.
49 * @param \Drupal\Core\Render\RendererInterface $renderer
52 public function __construct(BookManagerInterface $bookManager, BookExport $bookExport, RendererInterface $renderer) {
53 $this->bookManager = $bookManager;
54 $this->bookExport = $bookExport;
55 $this->renderer = $renderer;
61 public static function create(ContainerInterface $container) {
63 $container->get('book.manager'),
64 $container->get('book.export'),
65 $container->get('renderer')
70 * Returns an administrative overview of all books.
73 * A render array representing the administrative page content.
75 public function adminOverview() {
78 $headers = [t('Book'), t('Operations')];
79 // Add any recognized books to the table list.
80 foreach ($this->bookManager->getAllBooks() as $book) {
81 /** @var \Drupal\Core\Url $url */
83 if (isset($book['options'])) {
84 $url->setOptions($book['options']);
87 $this->l($book['title'], $url),
91 'title' => t('Edit order and titles'),
92 'url' => Url::fromRoute('book.admin_edit', ['node' => $book['nid']]),
96 '#type' => 'operations',
104 '#header' => $headers,
106 '#empty' => t('No books available.'),
111 * Prints a listing of all books.
114 * A render array representing the listing of all books content.
116 public function bookRender() {
118 foreach ($this->bookManager->getAllBooks() as $book) {
119 $book_list[] = $this->l($book['title'], $book['url']);
122 '#theme' => 'item_list',
123 '#items' => $book_list,
125 'tags' => \Drupal::entityManager()->getDefinition('node')->getListCacheTags(),
131 * Generates representations of a book page and its children.
133 * The method delegates the generation of output to helper methods. The method
134 * name is derived by prepending 'bookExport' to the camelized form of given
135 * output type. For example, a type of 'html' results in a call to the method
138 * @param string $type
139 * A string encoding the type of output requested. The following types are
140 * currently supported in book module:
141 * - html: Printer-friendly HTML.
142 * Other types may be supported in contributed modules.
143 * @param \Drupal\node\NodeInterface $node
144 * The node to export.
147 * A render array representing the node and its children in the book
148 * hierarchy in a format determined by the $type parameter.
150 * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
152 public function bookExport($type, NodeInterface $node) {
153 $method = 'bookExport' . Container::camelize($type);
155 // @todo Convert the custom export functionality to serializer.
156 if (!method_exists($this->bookExport, $method)) {
157 drupal_set_message(t('Unknown export format.'));
158 throw new NotFoundHttpException();
161 $exported_book = $this->bookExport->{$method}($node);
162 return new Response($this->renderer->renderRoot($exported_book));