Chris@0: bookManager = $bookManager; Chris@0: $this->bookExport = $bookExport; Chris@0: $this->renderer = $renderer; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function create(ContainerInterface $container) { Chris@0: return new static( Chris@0: $container->get('book.manager'), Chris@0: $container->get('book.export'), Chris@0: $container->get('renderer') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns an administrative overview of all books. Chris@0: * Chris@0: * @return array Chris@0: * A render array representing the administrative page content. Chris@0: */ Chris@0: public function adminOverview() { Chris@0: $rows = []; Chris@0: Chris@0: $headers = [t('Book'), t('Operations')]; Chris@0: // Add any recognized books to the table list. Chris@0: foreach ($this->bookManager->getAllBooks() as $book) { Chris@0: /** @var \Drupal\Core\Url $url */ Chris@0: $url = $book['url']; Chris@0: if (isset($book['options'])) { Chris@0: $url->setOptions($book['options']); Chris@0: } Chris@0: $row = [ Chris@0: $this->l($book['title'], $url), Chris@0: ]; Chris@0: $links = []; Chris@0: $links['edit'] = [ Chris@0: 'title' => t('Edit order and titles'), Chris@0: 'url' => Url::fromRoute('book.admin_edit', ['node' => $book['nid']]), Chris@0: ]; Chris@0: $row[] = [ Chris@0: 'data' => [ Chris@0: '#type' => 'operations', Chris@0: '#links' => $links, Chris@0: ], Chris@0: ]; Chris@0: $rows[] = $row; Chris@0: } Chris@0: return [ Chris@0: '#type' => 'table', Chris@0: '#header' => $headers, Chris@0: '#rows' => $rows, Chris@0: '#empty' => t('No books available.'), Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prints a listing of all books. Chris@0: * Chris@0: * @return array Chris@0: * A render array representing the listing of all books content. Chris@0: */ Chris@0: public function bookRender() { Chris@0: $book_list = []; Chris@0: foreach ($this->bookManager->getAllBooks() as $book) { Chris@0: $book_list[] = $this->l($book['title'], $book['url']); Chris@0: } Chris@0: return [ Chris@0: '#theme' => 'item_list', Chris@0: '#items' => $book_list, Chris@0: '#cache' => [ Chris@18: 'tags' => $this->entityTypeManager()->getDefinition('node')->getListCacheTags(), Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Generates representations of a book page and its children. Chris@0: * Chris@0: * The method delegates the generation of output to helper methods. The method Chris@0: * name is derived by prepending 'bookExport' to the camelized form of given Chris@0: * output type. For example, a type of 'html' results in a call to the method Chris@0: * bookExportHtml(). Chris@0: * Chris@0: * @param string $type Chris@0: * A string encoding the type of output requested. The following types are Chris@0: * currently supported in book module: Chris@0: * - html: Printer-friendly HTML. Chris@0: * Other types may be supported in contributed modules. Chris@0: * @param \Drupal\node\NodeInterface $node Chris@0: * The node to export. Chris@0: * Chris@0: * @return array Chris@0: * A render array representing the node and its children in the book Chris@0: * hierarchy in a format determined by the $type parameter. Chris@0: * Chris@0: * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException Chris@0: */ Chris@0: public function bookExport($type, NodeInterface $node) { Chris@0: $method = 'bookExport' . Container::camelize($type); Chris@0: Chris@0: // @todo Convert the custom export functionality to serializer. Chris@0: if (!method_exists($this->bookExport, $method)) { Chris@17: $this->messenger()->addStatus(t('Unknown export format.')); Chris@0: throw new NotFoundHttpException(); Chris@0: } Chris@0: Chris@0: $exported_book = $this->bookExport->{$method}($node); Chris@0: return new Response($this->renderer->renderRoot($exported_book)); Chris@0: } Chris@0: Chris@0: }