b92c3e3fcfeb7904f4ce9afcf7e3576055686a47
[yaffs-website] / web / core / modules / book / src / Controller / BookController.php
1 <?php
2
3 namespace Drupal\book\Controller;
4
5 use Drupal\book\BookExport;
6 use Drupal\book\BookManagerInterface;
7 use Drupal\Core\Controller\ControllerBase;
8 use Drupal\Core\Render\RendererInterface;
9 use Drupal\Core\Url;
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;
15
16 /**
17  * Controller routines for book routes.
18  */
19 class BookController extends ControllerBase {
20
21   /**
22    * The book manager.
23    *
24    * @var \Drupal\book\BookManagerInterface
25    */
26   protected $bookManager;
27
28   /**
29    * The book export service.
30    *
31    * @var \Drupal\book\BookExport
32    */
33   protected $bookExport;
34
35   /**
36    * The renderer.
37    *
38    * @var \Drupal\Core\Render\RendererInterface
39    */
40   protected $renderer;
41
42   /**
43    * Constructs a BookController object.
44    *
45    * @param \Drupal\book\BookManagerInterface $bookManager
46    *   The book manager.
47    * @param \Drupal\book\BookExport $bookExport
48    *   The book export service.
49    * @param \Drupal\Core\Render\RendererInterface $renderer
50    *   The renderer.
51    */
52   public function __construct(BookManagerInterface $bookManager, BookExport $bookExport, RendererInterface $renderer) {
53     $this->bookManager = $bookManager;
54     $this->bookExport = $bookExport;
55     $this->renderer = $renderer;
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public static function create(ContainerInterface $container) {
62     return new static(
63       $container->get('book.manager'),
64       $container->get('book.export'),
65       $container->get('renderer')
66     );
67   }
68
69   /**
70    * Returns an administrative overview of all books.
71    *
72    * @return array
73    *   A render array representing the administrative page content.
74    */
75   public function adminOverview() {
76     $rows = [];
77
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 */
82       $url = $book['url'];
83       if (isset($book['options'])) {
84         $url->setOptions($book['options']);
85       }
86       $row = [
87         $this->l($book['title'], $url),
88       ];
89       $links = [];
90       $links['edit'] = [
91         'title' => t('Edit order and titles'),
92         'url' => Url::fromRoute('book.admin_edit', ['node' => $book['nid']]),
93       ];
94       $row[] = [
95         'data' => [
96           '#type' => 'operations',
97           '#links' => $links,
98         ],
99       ];
100       $rows[] = $row;
101     }
102     return [
103       '#type' => 'table',
104       '#header' => $headers,
105       '#rows' => $rows,
106       '#empty' => t('No books available.'),
107     ];
108   }
109
110   /**
111    * Prints a listing of all books.
112    *
113    * @return array
114    *   A render array representing the listing of all books content.
115    */
116   public function bookRender() {
117     $book_list = [];
118     foreach ($this->bookManager->getAllBooks() as $book) {
119       $book_list[] = $this->l($book['title'], $book['url']);
120     }
121     return [
122       '#theme' => 'item_list',
123       '#items' => $book_list,
124       '#cache' => [
125         'tags' => \Drupal::entityManager()->getDefinition('node')->getListCacheTags(),
126       ],
127     ];
128   }
129
130   /**
131    * Generates representations of a book page and its children.
132    *
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
136    * bookExportHtml().
137    *
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.
145    *
146    * @return array
147    *   A render array representing the node and its children in the book
148    *   hierarchy in a format determined by the $type parameter.
149    *
150    * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
151    */
152   public function bookExport($type, NodeInterface $node) {
153     $method = 'bookExport' . Container::camelize($type);
154
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();
159     }
160
161     $exported_book = $this->bookExport->{$method}($node);
162     return new Response($this->renderer->renderRoot($exported_book));
163   }
164
165 }