From 4397d8d5358bbf876bbc5a0b987572e30a9f20d4 Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Thu, 12 Nov 2020 19:28:07 +0000
Subject: [PATCH] Issue #2851504 by alexpott, vomiand, Vj, BalajiDS, cilefen,
 tamasd: "Illegal choice 0 in Book element" when switching the book outline
 field from anything to "- None - "

---
 core/modules/book/src/BookManager.php         |  2 +-
 .../BookJavascriptTest.php                    | 22 +++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/core/modules/book/src/BookManager.php b/core/modules/book/src/BookManager.php
index e7a6246f1457..504f042ea553 100644
--- a/core/modules/book/src/BookManager.php
+++ b/core/modules/book/src/BookManager.php
@@ -220,7 +220,7 @@ public function addFormElements(array $form, FormStateInterface $form_state, Nod
       // The node can become a new book, if it is not one already.
       $options = [$nid => $this->t('- Create a new book -')] + $options;
     }
-    if (!$node->book['bid']) {
+    if (!$node->book['bid'] || $nid === 'new' || $node->book['original_bid'] === 0) {
       // The node is not currently in the hierarchy.
       $options = [0 => $this->t('- None -')] + $options;
     }
diff --git a/core/modules/book/tests/src/FunctionalJavascript/BookJavascriptTest.php b/core/modules/book/tests/src/FunctionalJavascript/BookJavascriptTest.php
index bdeb1fa90f79..1c8616dcf14b 100644
--- a/core/modules/book/tests/src/FunctionalJavascript/BookJavascriptTest.php
+++ b/core/modules/book/tests/src/FunctionalJavascript/BookJavascriptTest.php
@@ -162,4 +162,26 @@ protected function assertOrderInPage(array $items) {
     $this->assertSame($items, array_values($strings), "Found strings, ordered as: $ordered.");
   }
 
+  /**
+   * Tests book outline AJAX request.
+   */
+  public function testBookAddOutline() {
+    $this->drupalLogin($this->drupalCreateUser(['create book content', 'create new books', 'add content to books']));
+    $this->drupalGet('node/add/book');
+    $assert_session = $this->assertSession();
+    $session = $this->getSession();
+    $page = $session->getPage();
+
+    $page->find('css', '#edit-book')->click();
+    $book_select = $page->findField("book[bid]");
+    $book_select->setValue('new');
+    $assert_session->waitForText('This will be the top-level page in this book.');
+    $assert_session->pageTextContains('This will be the top-level page in this book.');
+    $assert_session->pageTextNotContains('No book selected.');
+    $book_select->setValue(0);
+    $assert_session->waitForText('No book selected.');
+    $assert_session->pageTextContains('No book selected.');
+    $assert_session->pageTextNotContains('This will be the top-level page in this book.');
+  }
+
 }
-- 
GitLab