Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176

This commit is contained in:
Pantheon Automation 2015-08-17 17:00:26 -07:00 committed by Greg Anderson
commit 9921556621
13277 changed files with 1459781 additions and 0 deletions

View file

@ -0,0 +1,300 @@
<?php
/**
* @file
* Contains \Drupal\book\Form\BookAdminEditForm.
*/
namespace Drupal\book\Form;
use Drupal\book\BookManager;
use Drupal\book\BookManagerInterface;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for administering a single book's hierarchy.
*/
class BookAdminEditForm extends FormBase {
/**
* The node storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $nodeStorage;
/**
* The book manager.
*
* @var \Drupal\book\BookManagerInterface
*/
protected $bookManager;
/**
* Constructs a new BookAdminEditForm.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $node_storage
* The custom block storage.
* @param \Drupal\book\BookManagerInterface $book_manager
* The book manager.
*/
public function __construct(EntityStorageInterface $node_storage, BookManagerInterface $book_manager) {
$this->nodeStorage = $node_storage;
$this->bookManager = $book_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$entity_manager = $container->get('entity.manager');
return new static(
$entity_manager->getStorage('node'),
$container->get('book.manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'book_admin_edit';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node = NULL) {
$form['#title'] = $node->label();
$form['#node'] = $node;
$this->bookAdminTable($node, $form);
$form['save'] = array(
'#type' => 'submit',
'#value' => $this->t('Save book pages'),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($form_state->getValue('tree_hash') != $form_state->getValue('tree_current_hash')) {
$form_state->setErrorByName('', $this->t('This book has been modified by another user, the changes could not be saved.'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Save elements in the same order as defined in post rather than the form.
// This ensures parents are updated before their children, preventing orphans.
$user_input = $form_state->getUserInput();
if (isset($user_input['table'])) {
$order = array_flip(array_keys($user_input['table']));
$form['table'] = array_merge($order, $form['table']);
foreach (Element::children($form['table']) as $key) {
if ($form['table'][$key]['#item']) {
$row = $form['table'][$key];
$values = $form_state->getValue(array('table', $key));
// Update menu item if moved.
if ($row['parent']['pid']['#default_value'] != $values['pid'] || $row['weight']['#default_value'] != $values['weight']) {
$link = $this->bookManager->loadBookLink($values['nid'], FALSE);
$link['weight'] = $values['weight'];
$link['pid'] = $values['pid'];
$this->bookManager->saveBookLink($link, FALSE);
}
// Update the title if changed.
if ($row['title']['#default_value'] != $values['title']) {
$node = $this->nodeStorage->load($values['nid']);
$node->revision_log = $this->t('Title changed from %original to %current.', array('%original' => $node->label(), '%current' => $values['title']));
$node->title = $values['title'];
$node->book['link_title'] = $values['title'];
$node->setNewRevision();
$node->save();
$this->logger('content')->notice('book: updated %title.', array('%title' => $node->label(), 'link' => $node->link($this->t('View'))));
}
}
}
}
drupal_set_message($this->t('Updated book %title.', array('%title' => $form['#node']->label())));
}
/**
* Builds the table portion of the form for the book administration page.
*
* @param \Drupal\node\NodeInterface $node
* The node of the top-level page in the book.
* @param array $form
* The form that is being modified, passed by reference.
*
* @see self::buildForm()
*/
protected function bookAdminTable(NodeInterface $node, array &$form) {
$form['table'] = array(
'#type' => 'table',
'#header' => [
$this->t('Title'),
$this->t('Weight'),
$this->t('Parent'),
$this->t('Operations'),
],
'#empty' => $this->t('No book content available.'),
'#tabledrag' => [
[
'action' => 'match',
'relationship' => 'parent',
'group' => 'book-pid',
'subgroup' => 'book-pid',
'source' => 'book-nid',
'hidden' => TRUE,
'limit' => BookManager::BOOK_MAX_DEPTH - 2,
],
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'book-weight',
],
],
);
$tree = $this->bookManager->bookSubtreeData($node->book);
// Do not include the book item itself.
$tree = array_shift($tree);
if ($tree['below']) {
$hash = Crypt::hashBase64(serialize($tree['below']));
// Store the hash value as a hidden form element so that we can detect
// if another user changed the book hierarchy.
$form['tree_hash'] = array(
'#type' => 'hidden',
'#default_value' => $hash,
);
$form['tree_current_hash'] = array(
'#type' => 'value',
'#value' => $hash,
);
$this->bookAdminTableTree($tree['below'], $form['table']);
}
}
/**
* Helps build the main table in the book administration page form.
*
* @param array $tree
* A subtree of the book menu hierarchy.
* @param array $form
* The form that is being modified, passed by reference.
*
* @see self::buildForm()
*/
protected function bookAdminTableTree(array $tree, array &$form) {
// The delta must be big enough to give each node a distinct value.
$count = count($tree);
$delta = ($count < 30) ? 15 : intval($count / 2) + 1;
$access = \Drupal::currentUser()->hasPermission('administer nodes');
$destination = $this->getDestinationArray();
foreach ($tree as $data) {
$nid = $data['link']['nid'];
$id = 'book-admin-' . $nid;
$form[$id]['#item'] = $data['link'];
$form[$id]['#nid'] = $nid;
$form[$id]['#attributes']['class'][] = 'draggable';
$form[$id]['#weight'] = $data['link']['weight'];
if (isset($data['link']['depth']) && $data['link']['depth'] > 2) {
$indentation = [
'#theme' => 'indentation',
'#size' => $data['link']['depth'] - 2,
];
}
$form[$id]['title'] = [
'#prefix' => !empty($indentation) ? drupal_render($indentation) : '',
'#type' => 'textfield',
'#default_value' => $data['link']['title'],
'#maxlength' => 255,
'#size' => 40,
];
$form[$id]['weight'] = [
'#type' => 'weight',
'#default_value' => $data['link']['weight'],
'#delta' => max($delta, abs($data['link']['weight'])),
'#title' => $this->t('Weight for @title', ['@title' => $data['link']['title']]),
'#title_display' => 'invisible',
'#attributes' => [
'class' => ['book-weight'],
],
];
$form[$id]['parent']['nid'] = [
'#parents' => ['table', $id, 'nid'],
'#type' => 'hidden',
'#value' => $nid,
'#attributes' => [
'class' => ['book-nid'],
],
];
$form[$id]['parent']['pid'] = [
'#parents' => ['table', $id, 'pid'],
'#type' => 'hidden',
'#default_value' => $data['link']['pid'],
'#attributes' => [
'class' => ['book-pid'],
],
];
$form[$id]['parent']['bid'] = [
'#parents' => ['table', $id, 'bid'],
'#type' => 'hidden',
'#default_value' => $data['link']['bid'],
'#attributes' => [
'class' => ['book-bid'],
],
];
$form[$id]['operations'] = [
'#type' => 'operations',
];
$form[$id]['operations']['#links']['view'] = [
'title' => $this->t('View'),
'url' => new Url('entity.node.canonical', ['node' => $nid]),
];
if ($access) {
$form[$id]['operations']['#links']['edit'] = [
'title' => $this->t('Edit'),
'url' => new Url('entity.node.edit_form', ['node' => $nid]),
'query' => $destination,
];
$form[$id]['operations']['#links']['delete'] = [
'title' => $this->t('Delete'),
'url' => new Url('entity.node.delete_form', ['node' => $nid]),
'query' => $destination,
];
}
if ($data['below']) {
$this->bookAdminTableTree($data['below'], $form);
}
}
}
}

View file

@ -0,0 +1,136 @@
<?php
/**
* @file
* Contains \Drupal\book\Form\BookOutlineForm.
*/
namespace Drupal\book\Form;
use Drupal\book\BookManagerInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Displays the book outline form.
*/
class BookOutlineForm extends ContentEntityForm {
/**
* The book being displayed.
*
* @var \Drupal\node\NodeInterface
*/
protected $entity;
/**
* BookManager service.
*
* @var \Drupal\book\BookManagerInterface
*/
protected $bookManager;
/**
* Constructs a BookOutlineForm object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\book\BookManagerInterface $book_manager
* The BookManager service.
*/
public function __construct(EntityManagerInterface $entity_manager, BookManagerInterface $book_manager) {
parent::__construct($entity_manager);
$this->bookManager = $book_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager'),
$container->get('book.manager')
);
}
/**
* {@inheritdoc}
*/
public function getBaseFormId() {
return NULL;
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $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(), FALSE);
return $form;
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $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}
*/
public function save(array $form, FormStateInterface $form_state) {
$form_state->setRedirect(
'entity.node.canonical',
array('node' => $this->entity->id())
);
$book_link = $form_state->getValue('book');
if (!$book_link['bid']) {
drupal_set_message($this->t('No changes were made'));
return;
}
$this->entity->book = $book_link;
if ($this->bookManager->updateOutline($this->entity)) {
if (isset($this->entity->book['parent_mismatch']) && $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->setRedirectUrl($this->entity->urlInfo('book-outline-form'));
}
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, FormStateInterface $form_state) {
$form_state->setRedirectUrl($this->entity->urlInfo('book-remove-form'));
}
}

View file

@ -0,0 +1,115 @@
<?php
/**
* @file
* Contains \Drupal\book\Form\BookRemoveForm.
*/
namespace Drupal\book\Form;
use Drupal\book\BookManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Remove form for book module.
*/
class BookRemoveForm extends ConfirmFormBase {
/**
* The book manager.
*
* @var \Drupal\book\BookManagerInterface
*/
protected $bookManager;
/**
* The node representing the book.
*
* @var \Drupal\node\NodeInterface
*/
protected $node;
/**
* Constructs a BookRemoveForm object.
*
* @param \Drupal\book\BookManagerInterface $book_manager
* The book manager.
*/
public function __construct(BookManagerInterface $book_manager) {
$this->bookManager = $book_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('book.manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'book_remove_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node = NULL) {
$this->node = $node;
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function getDescription() {
$title = array('%title' => $this->node->label());
if ($this->node->book['has_children']) {
return $this->t('%title has associated child pages, which will be relocated automatically to maintain their connection to the book. To recreate the hierarchy (as it was before removing this page), %title may be added again using the Outline tab, and each of its former child pages will need to be relocated manually.', $title);
}
else {
return $this->t('%title may be added to hierarchy again using the Outline tab.', $title);
}
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Remove');
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to remove %title from the book hierarchy?', array('%title' => $this->node->label()));
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return $this->node->urlInfo();
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($this->bookManager->checkNodeIsRemovable($this->node)) {
$this->bookManager->deleteFromBook($this->node->id());
drupal_set_message($this->t('The post has been removed from the book.'));
}
$form_state->setRedirectUrl($this->getCancelUrl());
}
}

View file

@ -0,0 +1,89 @@
<?php
/**
* @file
* Contains \Drupal\book\Form\BookSettingsForm.
*/
namespace Drupal\book\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Configure book settings for this site.
*/
class BookSettingsForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'book_admin_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['book.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$types = node_type_get_names();
$config = $this->config('book.settings');
$form['book_allowed_types'] = array(
'#type' => 'checkboxes',
'#title' => $this->t('Content types allowed in book outlines'),
'#default_value' => $config->get('allowed_types'),
'#options' => $types,
'#description' => $this->t('Users with the %outline-perm permission can add all content types.', array('%outline-perm' => $this->t('Administer book outlines'))),
'#required' => TRUE,
);
$form['book_child_type'] = array(
'#type' => 'radios',
'#title' => $this->t('Content type for the <em>Add child page</em> link'),
'#default_value' => $config->get('child_type'),
'#options' => $types,
'#required' => TRUE,
);
$form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$child_type = $form_state->getValue('book_child_type');
if ($form_state->isValueEmpty(array('book_allowed_types', $child_type))) {
$form_state->setErrorByName('book_child_type', $this->t('The content type for the %add-child link must be one of those selected as an allowed book outline type.', array('%add-child' => $this->t('Add child page'))));
}
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$allowed_types = array_filter($form_state->getValue('book_allowed_types'));
// We need to save the allowed types in an array ordered by machine_name so
// that we can save them in the correct order if node type changes.
// @see book_node_type_update().
sort($allowed_types);
$this->config('book.settings')
// Remove unchecked types.
->set('allowed_types', $allowed_types)
->set('child_type', $form_state->getValue('book_child_type'))
->save();
parent::submitForm($form, $form_state);
}
}