annotate core/modules/book/src/Controller/BookController.php @ 19:fa3358dc1485 tip

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