BookManager->addFormElements needs to check for book before adding elements

Created on 22 May 2019, about 5 years ago
Updated 23 April 2024, 2 months ago

Problem/Motivation

With book, media, and media library installed, the "Add media" form sometimes fails.

Steps to Reproduce

  1. Drupal 8.7.1, PHP 7.1.12
  2. Enable book, media, and media_library.
  3. Add a reference field of type Media, allowing images, on a basic page. Use the Media library widget on the form display.
  4. Add a basic page and upload or select an image from the media library.
  5. Edit that page and remove the image, then try to upload or select a different image.
  6. The image thumbnail fails to appear, and an error from BookManager appears in the console log and in recent log messages.

Note that the content does not need to be in a book in order for this failure to occur.

Location: /node/1/edit?_wrapper_format=drupal_ajax&ajax_form=1

Message:

TypeError: Argument 1 passed to Drupal\book\BookManager::addParentSelectFormElements() must be of the type array, null given, called in /drupal-8.7.1/core/modules/book/src/BookManager.php on line 201 in Drupal\book\BookManager->addParentSelectFormElements() (line 357 of /drupal-8.7.1/core/modules/book/src/BookManager.php) #0 /drupal-8.7.1/core/modules/book/src/BookManager.php(201): Drupal\book\BookManager->addParentSelectFormElements(NULL) #1 /drupal-8.7.1/core/modules/book/book.module(150): Drupal\book\BookManager->addFormElements(Array, Object(Drupal\Core\Form\FormState), Object(Drupal\node\Entity\Node), Object(Drupal\Core\Session\AccountProxy), true) #2 /drupal-8.7.1/core/lib/Drupal/Core/Extension/ModuleHandler.php(539): book_form_node_form_alter(Array, Object(Drupal\Core\Form\FormState), 'node_page_edit_...') #3 /drupal-8.7.1/core/lib/Drupal/Core/Form/FormBuilder.php(835): Drupal\Core\Extension\ModuleHandler->alter('form', Array, Object(Drupal\Core\Form\FormState), 'node_page_edit_...') #4 /drupal-8.7.1/core/lib/Drupal/Core/Form/FormBuilder.php(417): Drupal\Core\Form\FormBuilder->prepareForm('node_page_edit_...', Array, Object(Drupal\Core\Form\FormState)) #5 /drupal-8.7.1/core/lib/Drupal/Core/Form/FormBuilder.php(626): Drupal\Core\Form\FormBuilder->rebuildForm('node_page_edit_...', Object(Drupal\Core\Form\FormState), Array) #6 /drupal-8.7.1/core/lib/Drupal/Core/Form/FormBuilder.php(319): Drupal\Core\Form\FormBuilder->processForm('node_page_edit_...', Array, Object(Drupal\Core\Form\FormState)) #7 /drupal-8.7.1/core/lib/Drupal/Core/Controller/FormController.php(93): Drupal\Core\Form\FormBuilder->buildForm('node_page_edit_...', Object(Drupal\Core\Form\FormState)) #8 [internal function]: Drupal\Core\Controller\FormController->getContentResult(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch)) #9 /drupal-8.7.1/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array) #10 /drupal-8.7.1/core/lib/Drupal/Core/Render/Renderer.php(582): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() #11 /drupal-8.7.1/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure)) #12 /drupal-8.7.1/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) #13 /drupal-8.7.1/vendor/symfony/http-kernel/HttpKernel.php(151): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() #14 /drupal-8.7.1/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1) #15 /drupal-8.7.1/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #16 /drupal-8.7.1/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #17 /drupal-8.7.1/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #18 /drupal-8.7.1/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true) #19 /drupal-8.7.1/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #20 /drupal-8.7.1/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #21 /drupal-8.7.1/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #22 /drupal-8.7.1/core/lib/Drupal/Core/DrupalKernel.php(693): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #23 /drupal-8.7.1/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request)) #24 {main}.

Proposed resolution

An if ($node->book) statement around the following pieces of BookManager->addFormElements seems to make everything work properly again:

$form['book'] = [
  '#type' => 'details',
  '#title' => $this->t('Book outline'),
  '#weight' => 10,
  '#open' => !$collapsed,
  '#group' => 'advanced',
  '#attributes' => [
    'class' => ['book-outline-form'],
  ],
  '#attached' => [
    'library' => ['book/drupal.book'],
  ],
  '#tree' => TRUE,
];
foreach (['nid', 'has_children', 'original_bid', 'parent_depth_limit'] as $key) {
  $form['book'][$key] = [
    '#type' => 'value',
    '#value' => $node->book[$key],
  ];
}

$form['book']['pid'] = $this->addParentSelectFormElements($node->book);
πŸ› Bug report
Status

Closed: cannot reproduce

Version

8.8 ⚰️

Component
BookΒ  β†’

Last updated 1 day ago

  • Maintained by
  • πŸ‡ΊπŸ‡ΈUnited States @pwolanin
Created by

πŸ‡ΊπŸ‡ΈUnited States Kasey_MK

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024