Commit d04f659f authored by alexpott's avatar alexpott

Issue #2482857 by jhedstrom, lokapujya, pwolanin, lindzeng, swati_qa, Truptti,...

Issue #2482857 by jhedstrom, lokapujya, pwolanin, lindzeng, swati_qa, Truptti, alexpott: Cannot delete a book parent
parent 772127c9
......@@ -323,14 +323,22 @@ function book_node_prepare_form(NodeInterface $node, $operation, FormStateInterf
}
/**
* Implements hook_form_FORM_ID_alter() for node_delete_confirm().
* Implements hook_form_BASE_FORM_ID_alter().
*
* Alters the confirm form for a single node deletion.
*
* @see node_delete_confirm()
*/
function book_form_node_delete_confirm_alter(&$form, FormStateInterface $form_state) {
$node = Node::load($form['nid']['#value']);
function book_form_node_confirm_form_alter(&$form, FormStateInterface $form_state) {
// Only need to alter the delete operation form.
if ($form_state->getFormObject()->getOperation() !== 'delete') {
return;
}
/** @var \Drupal\node\NodeInterface $node */
$node = $form_state->getFormObject()->getEntity();
if (!book_type_is_allowed($node->getType())) {
// Not a book node.
return;
}
if (isset($node->book) && $node->book['has_children']) {
$form['book_warning'] = array(
......
......@@ -439,9 +439,9 @@ public function deleteFromBook($nid) {
if ($nid == $original['bid']) {
// Handle deletion of a top-level post.
$result = $this->bookOutlineStorage->loadBookChildren($nid);
foreach ($result as $child) {
$child['bid'] = $child['nid'];
$children = $this->entityManager->getStorage('node')->loadMultiple(array_keys($result));
foreach ($children as $child) {
$child->book['bid'] = $child->id();
$this->updateOutline($child);
}
}
......
......@@ -30,7 +30,7 @@ class BookTest extends WebTestBase {
/**
* A book node.
*
* @var object
* @var \Drupal\node\NodeInterface
*/
protected $book;
......@@ -77,11 +77,13 @@ protected function setUp() {
$this->bookAuthor = $this->drupalCreateUser(array('create new books', 'create book content', 'edit own book content', 'add content to books'));
$this->webUser = $this->drupalCreateUser(array('access printer-friendly version', 'node test view'));
$this->webUserWithoutNodeAccess = $this->drupalCreateUser(array('access printer-friendly version'));
$this->adminUser = $this->drupalCreateUser(array('create new books', 'create book content', 'edit own book content', 'add content to books', 'administer blocks', 'administer permissions', 'administer book outlines', 'node test view', 'administer content types', 'administer site configuration'));
$this->adminUser = $this->drupalCreateUser(array('create new books', 'create book content', 'edit any book content', 'delete any book content', 'add content to books', 'administer blocks', 'administer permissions', 'administer book outlines', 'node test view', 'administer content types', 'administer site configuration'));
}
/**
* Creates a new book with a page hierarchy.
*
* @return \Drupal\node\NodeInterface[]
*/
function createBook() {
// Create new book.
......@@ -498,6 +500,25 @@ function testBookDelete() {
$node_storage->resetCache(array($this->book->id()));
$node = $node_storage->load($this->book->id());
$this->assertTrue(empty($node->book), 'Deleting childless top-level book node properly allowed.');
// Tests directly deleting a book parent.
$nodes = $this->createBook();
$this->drupalLogin($this->adminUser);
$this->drupalGet($this->book->urlInfo('delete-form'));
$this->assertRaw(t('%title is part of a book outline, and has associated child pages. If you proceed with deletion, the child pages will be relocated automatically.', ['%title' => $this->book->label()]));
// Delete parent, and visit a child page.
$this->drupalPostForm($this->book->urlInfo('delete-form'), [], t('Delete'));
$this->drupalGet($nodes[0]->urlInfo());
$this->assertResponse(200);
$this->assertText($nodes[0]->label());
// The book parents should be updated.
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
$node_storage->resetCache();
$child = $node_storage->load($nodes[0]->id());
$this->assertEqual($child->id(), $child->book['bid'], 'Child node book ID updated when parent is deleted.');
// 3rd-level children should now be 2nd-level.
$second = $node_storage->load($nodes[1]->id());
$this->assertEqual($child->id(), $second->book['bid'], '3rd-level child node is now second level when top-level node is deleted.');
}
/**
......
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