From 80931c9a5b123f6c8de719c1403bea9e73809da5 Mon Sep 17 00:00:00 2001 From: catch Date: Mon, 15 Apr 2013 22:19:00 +0100 Subject: [PATCH] Issue #1938296 by disasm, YesCT: Convert book_admin_overview() and book_render() to a new-style Controller. --- core/modules/book/book.admin.inc | 35 -------- core/modules/book/book.module | 10 +-- core/modules/book/book.pages.inc | 17 ---- core/modules/book/book.routing.yml | 14 +++ core/modules/book/book.services.yml | 4 + .../book/lib/Drupal/book/BookManager.php | 82 +++++++++++++++++ .../Drupal/book/Controller/BookController.php | 89 +++++++++++++++++++ 7 files changed, 193 insertions(+), 58 deletions(-) create mode 100644 core/modules/book/book.services.yml create mode 100644 core/modules/book/lib/Drupal/book/BookManager.php create mode 100644 core/modules/book/lib/Drupal/book/Controller/BookController.php diff --git a/core/modules/book/book.admin.inc b/core/modules/book/book.admin.inc index ff5dac8762c..6002151195e 100644 --- a/core/modules/book/book.admin.inc +++ b/core/modules/book/book.admin.inc @@ -7,41 +7,6 @@ use Drupal\Core\Entity\EntityInterface; -/** - * Page callback: Returns an administrative overview of all books. - * - * @return string - * A HTML-formatted string with the administrative page content. - * - * @see book_menu() - */ -function book_admin_overview() { - $rows = array(); - - $headers = array(t('Book'), t('Operations')); - - // Add any recognized books to the table list. - foreach (book_get_books() as $book) { - $row = array( - l($book['title'], $book['href'], $book['options']), - ); - $links = array(); - $links['edit'] = array( - 'title' => t('Edit order and titles'), - 'href' => 'admin/content/book/' . $book['nid'], - ); - $row[] = array( - 'data' => array( - '#type' => 'operations', - '#links' => $links, - ), - ); - $rows[] = $row; - } - - return theme('table', array('header' => $headers, 'rows' => $rows, 'empty' => t('No books available.'))); -} - /** * Form constructor for administering a single book's hierarchy. * diff --git a/core/modules/book/book.module b/core/modules/book/book.module index 26f324ecac3..d97dcf1a640 100644 --- a/core/modules/book/book.module +++ b/core/modules/book/book.module @@ -134,10 +134,8 @@ function book_menu() { $items['admin/content/book'] = array( 'title' => 'Books', 'description' => "Manage your site's book outlines.", - 'page callback' => 'book_admin_overview', - 'access arguments' => array('administer book outlines'), + 'route_name' => 'book_admin', 'type' => MENU_LOCAL_TASK, - 'file' => 'book.admin.inc', ); $items['admin/content/book/list'] = array( 'title' => 'List', @@ -161,10 +159,8 @@ function book_menu() { ); $items['book'] = array( 'title' => 'Books', - 'page callback' => 'book_render', - 'access arguments' => array('access content'), + 'route_name' => 'book_render', 'type' => MENU_SUGGESTED_ITEM, - 'file' => 'book.pages.inc', ); $items['book/export/%/%node'] = array( 'page callback' => 'book_export', @@ -274,6 +270,8 @@ function book_entity_view_mode_info() { /** * Returns an array of all books. * + * @todo Remove in favor of BookManager Service. http://drupal.org/node/1963894 + * * This list may be used for generating a list of all the books, or for building * the options for a form select. * diff --git a/core/modules/book/book.pages.inc b/core/modules/book/book.pages.inc index 2418591b6b4..67c4c994c25 100644 --- a/core/modules/book/book.pages.inc +++ b/core/modules/book/book.pages.inc @@ -9,23 +9,6 @@ use Drupal\Core\Entity\EntityInterface; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; -/** - * Page callback: Prints a listing of all books. - * - * @return string - * A HTML-formatted string with the listing of all books content. - * - * @see book_menu() - */ -function book_render() { - $book_list = array(); - foreach (book_get_books() as $book) { - $book_list[] = l($book['title'], $book['href'], $book['options']); - } - - return theme('item_list', array('items' => $book_list)); -} - /** * Page callback: Generates representations of a book page and its children. * diff --git a/core/modules/book/book.routing.yml b/core/modules/book/book.routing.yml index 3af7c08721d..77b492bf55b 100644 --- a/core/modules/book/book.routing.yml +++ b/core/modules/book/book.routing.yml @@ -1,3 +1,17 @@ +book_render: + pattern: '/book' + defaults: + _content: '\Drupal\book\Controller\BookController::bookRender' + requirements: + _permission: 'access content' + +book_admin: + pattern: '/admin/content/book' + defaults: + _content: '\Drupal\book\Controller\BookController::adminOverview' + requirements: + _permission: 'administer book outlines' + book_settings: pattern: '/admin/content/book/settings' defaults: diff --git a/core/modules/book/book.services.yml b/core/modules/book/book.services.yml new file mode 100644 index 00000000000..254c6ecc029 --- /dev/null +++ b/core/modules/book/book.services.yml @@ -0,0 +1,4 @@ +services: + book.manager: + class: Drupal\book\BookManager + arguments: ['@database'] diff --git a/core/modules/book/lib/Drupal/book/BookManager.php b/core/modules/book/lib/Drupal/book/BookManager.php new file mode 100644 index 00000000000..cf2070bc862 --- /dev/null +++ b/core/modules/book/lib/Drupal/book/BookManager.php @@ -0,0 +1,82 @@ +database = $database; + } + + /** + * Returns an array of all books. + * + * This list may be used for generating a list of all the books, or for building + * the options for a form select. + * + * @return + * An array of all books. + */ + public function getAllBooks() { + if (!isset($this->books)) { + $this->loadBooks(); + } + return $this->books; + } + + /** + * Loads Books Array. + */ + protected function loadBooks() { + $this->books = array(); + $nids = $this->database->query("SELECT DISTINCT(bid) FROM {book}")->fetchCol(); + if ($nids) { + $query = $this->database->select('book', 'b', array('fetch' => \PDO::FETCH_ASSOC)); + $query->join('node', 'n', 'b.nid = n.nid'); + $query->join('menu_links', 'ml', 'b.mlid = ml.mlid'); + $query->addField('n', 'type', 'type'); + $query->addField('n', 'title', 'title'); + $query->fields('b'); + $query->fields('ml'); + $query->condition('n.nid', $nids, 'IN'); + $query->condition('n.status', 1); + $query->orderBy('ml.weight'); + $query->orderBy('ml.link_title'); + $query->addTag('node_access'); + $book_links = $query->execute(); + foreach ($book_links as $link) { + $link['href'] = $link['link_path']; + $link['options'] = unserialize($link['options']); + $this->books[$link['bid']] = $link; + } + } + } + +} diff --git a/core/modules/book/lib/Drupal/book/Controller/BookController.php b/core/modules/book/lib/Drupal/book/Controller/BookController.php new file mode 100644 index 00000000000..8c2fceb1db8 --- /dev/null +++ b/core/modules/book/lib/Drupal/book/Controller/BookController.php @@ -0,0 +1,89 @@ +get('book.manager')); + } + + /** + * Constructs a BookController object. + */ + public function __construct(BookManager $bookManager) { + $this->bookManager = $bookManager; + } + + /** + * Returns an administrative overview of all books. + * + * @return string + * A HTML-formatted string with the administrative page content. + * + */ + public function adminOverview() { + $rows = array(); + + $headers = array(t('Book'), t('Operations')); + + // Add any recognized books to the table list. + foreach ($this->bookManager->getAllBooks() as $book) { + $row = array( + l($book['title'], $book['href'], $book['options']), + ); + $links = array(); + $links['edit'] = array( + 'title' => t('Edit order and titles'), + 'href' => 'admin/content/book/' . $book['nid'], + ); + $row[] = array( + 'data' => array( + '#type' => 'operations', + '#links' => $links, + ), + ); + $rows[] = $row; + } + + return theme('table', array('header' => $headers, 'rows' => $rows, 'empty' => t('No books available.'))); + } + + /** + * Prints a listing of all books. + * + * @return string + * A HTML-formatted string with the listing of all books content. + */ + public function bookRender() { + $book_list = array(); + foreach ($this->bookManager->getAllBooks() as $book) { + $book_list[] = l($book['title'], $book['href'], $book['options']); + } + + return theme('item_list', array('items' => $book_list)); + } + +}