Commit 377a9187 authored by webchick's avatar webchick

Issue #1938316 by disasm, jaskho, stella, scor, tim.plunkett, jibran | Crell:...

Issue #1938316 by disasm, jaskho, stella, scor, tim.plunkett, jibran | Crell: Convert book_outline() to a new-style Controller.
parent bf37effc
This diff is collapsed.
......@@ -9,67 +9,6 @@
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Page callback: Shows the outline form for a single node.
*
* @param \Drupal\Core\Entity\EntityInterface $node
* The book node for which to show the outline.
*
* @return string
* A HTML-formatted string with the outline form for a single node.
*
* @see book_menu()
*/
function book_outline(EntityInterface $node) {
drupal_set_title($node->label());
return drupal_get_form('book_outline_form', $node);
}
/**
* Form constructor for the book outline form.
*
* Allows handling of all book outline operations via the outline tab.
*
* @param \Drupal\Core\Entity\EntityInterface $node
* The book node for which to show the outline.
*
* @see book_outline_form_submit()
* @see book_remove_button_submit()
* @ingroup forms
*/
function book_outline_form($form, &$form_state, EntityInterface $node) {
if (!isset($node->book)) {
// The node is not part of any book yet - set default options.
$node->book = _book_link_defaults($node->id());
}
else {
$node->book['original_bid'] = $node->book['bid'];
}
// Find the depth limit for the parent select.
if (!isset($node->book['parent_depth_limit'])) {
$node->book['parent_depth_limit'] = _book_parent_depth_limit($node->book);
}
$form['#node'] = $node;
$form['#id'] = 'book-outline';
_book_add_form_elements($form, $form_state, $node);
$form['update'] = array(
'#type' => 'submit',
'#value' => $node->book['original_bid'] ? t('Update book outline') : t('Add to book outline'),
'#weight' => 15,
);
$form['remove'] = array(
'#type' => 'submit',
'#value' => t('Remove from book outline'),
'#access' => _book_node_is_removable($node),
'#weight' => 20,
'#submit' => array('book_remove_button_submit'),
);
return $form;
}
/**
* Form submission handler for book_outline_form().
......@@ -82,38 +21,6 @@ function book_remove_button_submit($form, &$form_state) {
$form_state['redirect'] = 'node/' . $form['#node']->id() . '/outline/remove';
}
/**
* Form submission handler for book_outline_form().
*
* @see book_remove_button_submit()
*/
function book_outline_form_submit($form, &$form_state) {
$node = $form['#node'];
$form_state['redirect'] = "node/" . $node->id();
$book_link = $form_state['values']['book'];
if (!$book_link['bid']) {
drupal_set_message(t('No changes were made'));
return;
}
$book_link['menu_name'] = book_menu_name($book_link['bid']);
$node->book = $book_link;
if (_book_update_outline($node)) {
if ($node->book['parent_mismatch']) {
// This will usually only happen when JS is disabled.
drupal_set_message(t('The post has been added to the selected book. You may now position it relative to other pages.'));
$form_state['redirect'] = "node/" . $node->id() . "/outline";
}
else {
drupal_set_message(t('The book outline has been updated.'));
}
}
else {
drupal_set_message(t('There was an error adding the post to the book.'), 'error');
}
}
/**
* Form constructor to confirm removal of a node from a book.
*
......@@ -143,7 +50,7 @@ function book_remove_form($form, &$form_state, EntityInterface $node) {
*/
function book_remove_form_submit($form, &$form_state) {
$node = $form['#node'];
if (_book_node_is_removable($node)) {
if (Drupal::service('book.manager')->checkNodeIsRemovable($node)) {
menu_link_delete($node->book['mlid']);
db_delete('book')
->condition('nid', $node->id())
......
......@@ -15,7 +15,7 @@ book_admin:
book_settings:
pattern: '/admin/structure/book/settings'
defaults:
_form: 'Drupal\book\Form\BookSettingsForm'
_form: '\Drupal\book\Form\BookSettingsForm'
requirements:
_permission: 'administer site configuration'
......@@ -28,3 +28,13 @@ book_export:
requirements:
_permission: 'access printer-friendly version'
_entity_access: 'node.view'
book_outline:
pattern: '/node/{node}/outline'
defaults:
_entity_form: 'node.book_outline'
options:
_access_mode: 'ALL'
requirements:
_permission: 'administer book outlines'
_entity_access: 'node.view'
services:
book.manager:
class: Drupal\book\BookManager
arguments: ['@database', '@entity.manager']
arguments: ['@database', '@entity.manager', '@string_translation', '@config.factory']
book.export:
class: Drupal\book\BookExport
arguments: ['@entity.manager']
<?php
/**
* @file
* Contains \Drupal\book\Form\BookOutlineForm.
*/
namespace Drupal\book\Form;
use Drupal\Core\Entity\EntityFormControllerNG;
use Drupal\book\BookManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Displays the book outline form.
*/
class BookOutlineForm extends EntityFormControllerNG {
/**
* The book being displayed.
*
* @var \Drupal\node\NodeInterface
*/
protected $entity;
/**
* BookManager service.
*
* @var \Drupal\book\BookManager
*/
protected $bookManager;
/**
* Constructs a BookOutlineForm object.
*/
public function __construct(BookManager $bookManager) {
$this->bookManager = $bookManager;
}
/**
* This method lets us inject the services this class needs.
*
* Only inject services that are actually needed. Which services
* are needed will vary by the controller.
*/
public static function create(ContainerInterface $container) {
return new static($container->get('book.manager'));
}
/**
* {@inheritdoc}
*/
public function getBaseFormID() {
return FALSE;
}
/**
* {@inheritdoc}
*/
public function form(array $form, array &$form_state) {
$form['#title'] = $this->entity->label();
if (!isset($this->entity->book)) {
// The node is not part of any book yet - set default options.
$this->entity->book = $this->bookManager->getLinkDefaults($this->entity->id());
}
else {
$this->entity->book['original_bid'] = $this->entity->book['bid'];
}
// Find the depth limit for the parent select.
if (!isset($this->entity->book['parent_depth_limit'])) {
$this->entity->book['parent_depth_limit'] = $this->bookManager->getParentDepthLimit($this->entity->book);
}
$form = $this->bookManager->addFormElements($form, $form_state, $this->entity, $this->currentUser());
return $form;
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, array &$form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this->entity->book['original_bid'] ? $this->t('Update book outline') : $this->t('Add to book outline');
$actions['delete']['#value'] = $this->t('Remove from book outline');
$actions['delete']['#access'] = $this->bookManager->checkNodeIsRemovable($this->entity);
return $actions;
}
/**
* {@inheritdoc}
*
* @see book_remove_button_submit()
*/
public function submit(array $form, array &$form_state) {
$form_state['redirect'] = 'node/' . $this->entity->id();
$book_link = $form_state['values']['book'];
if (!$book_link['bid']) {
drupal_set_message($this->t('No changes were made'));
return;
}
$book_link['menu_name'] = $this->bookManager->createMenuName($book_link['bid']);
$this->entity->book = $book_link;
if ($this->bookManager->updateOutline($this->entity)) {
if ($this->entity->book['parent_mismatch']) {
// This will usually only happen when JS is disabled.
drupal_set_message($this->t('The post has been added to the selected book. You may now position it relative to other pages.'));
$form_state['redirect'] = 'node/' . $this->entity->id() . '/outline';
}
else {
drupal_set_message($this->t('The book outline has been updated.'));
}
}
else {
drupal_set_message($this->t('There was an error adding the post to the book.'), 'error');
}
}
/**
* {@inheritdoc}
*/
public function delete(array $form, array &$form_state) {
$form_state['redirect'] = 'node/' . $this->entity->id() . '/outline/remove';
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment