Issue #1938296 by disasm, YesCT: Convert book_admin_overview() and book_render() to a new-style Controller.

8.0.x
catch 2013-04-15 22:19:00 +01:00
parent 26697707dc
commit 80931c9a5b
7 changed files with 193 additions and 58 deletions

View File

@ -7,41 +7,6 @@
use Drupal\Core\Entity\EntityInterface; 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. * Form constructor for administering a single book's hierarchy.
* *

View File

@ -134,10 +134,8 @@ function book_menu() {
$items['admin/content/book'] = array( $items['admin/content/book'] = array(
'title' => 'Books', 'title' => 'Books',
'description' => "Manage your site's book outlines.", 'description' => "Manage your site's book outlines.",
'page callback' => 'book_admin_overview', 'route_name' => 'book_admin',
'access arguments' => array('administer book outlines'),
'type' => MENU_LOCAL_TASK, 'type' => MENU_LOCAL_TASK,
'file' => 'book.admin.inc',
); );
$items['admin/content/book/list'] = array( $items['admin/content/book/list'] = array(
'title' => 'List', 'title' => 'List',
@ -161,10 +159,8 @@ function book_menu() {
); );
$items['book'] = array( $items['book'] = array(
'title' => 'Books', 'title' => 'Books',
'page callback' => 'book_render', 'route_name' => 'book_render',
'access arguments' => array('access content'),
'type' => MENU_SUGGESTED_ITEM, 'type' => MENU_SUGGESTED_ITEM,
'file' => 'book.pages.inc',
); );
$items['book/export/%/%node'] = array( $items['book/export/%/%node'] = array(
'page callback' => 'book_export', 'page callback' => 'book_export',
@ -274,6 +270,8 @@ function book_entity_view_mode_info() {
/** /**
* Returns an array of all books. * 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 * This list may be used for generating a list of all the books, or for building
* the options for a form select. * the options for a form select.
* *

View File

@ -9,23 +9,6 @@ use Drupal\Core\Entity\EntityInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 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. * Page callback: Generates representations of a book page and its children.
* *

View File

@ -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: book_settings:
pattern: '/admin/content/book/settings' pattern: '/admin/content/book/settings'
defaults: defaults:

View File

@ -0,0 +1,4 @@
services:
book.manager:
class: Drupal\book\BookManager
arguments: ['@database']

View File

@ -0,0 +1,82 @@
<?php
/**
* @file
* Contains \Drupal\book\BookManager.
*/
namespace Drupal\book;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Database\Connection;
/**
* Book Manager Service.
*/
class BookManager {
/**
* Database Service Object.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* Books Array.
*
* @var array
*/
protected $books;
/**
* Constructs a BookManager object.
*/
public function __construct(Connection $database) {
$this->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;
}
}
}
}

View File

@ -0,0 +1,89 @@
<?php
/**
* @file
* Contains \Drupal\book\Controller\BookController.
*/
namespace Drupal\book\Controller;
use Drupal\Core\ControllerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\book\BookManager;
/**
* Controller routines for book routes.
*/
class BookController implements ControllerInterface {
/**
* Book Manager Service.
*
* @var \Drupal\book\BookManager
*/
protected $bookManager;
/**
* Injects BookManager Service.
*/
public static function create(ContainerInterface $container) {
return new static($container->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));
}
}