Node view breaks when the underlying book node is missing

Created on 27 January 2022, over 3 years ago
Updated 9 May 2023, about 2 years ago

Problem/Motivation

Whenever a page is part of a book of which the book page (bid) does not exist. The entity view of that existing page breaks due to a missing check in the book_node_view hook.

      $book_node = Node::load($node->book['bid']);
      if (!$book_node->access()) {
        return;
      }

$book_node will be NULL if the node no longer exists. This issue may happen when for example migrating books or deleting nodes without using the proper Drupal functions.

The website encountered an unexpected error. Please try again later.
Error: Call to a member function access() on null in book_node_view() (line 233 of core/modules/book/book.module).
book_node_view(Array, Object, Object, 'full')
call_user_func_array('book_node_view', Array) (Line: 403)
Drupal\Core\Extension\ModuleHandler->invokeAll('node_view', Array) (Line: 288)
Drupal\Core\Entity\EntityViewBuilder->buildMultiple(Array) (Line: 239)
Drupal\Core\Entity\EntityViewBuilder->build(Array)
call_user_func_array(Array, Array) (Line: 101)
Drupal\Core\Render\Renderer->doTrustedCallback(Array, Array, 'Render #pre_render callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was %s. See https://www.drupal.org/node/2966725', 'exception', 'Drupal\Core\Render\Element\RenderCallbackInterface') (Line: 786)
Drupal\Core\Render\Renderer->doCallback('#pre_render', Array, Array) (Line: 377)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 201)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 241)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 578)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 242)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 91)
Drupal\next\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 132)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 142)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 163)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 67)
Drupal\simple_oauth\HttpMiddleware\BasicAuthSwap->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 49)
Asm89\Stack\Cors->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 717)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Steps to reproduce

Create a book (A)
Add another page to this book (B)
Remove the book page (A) without altering the book itself
View the still existing node view (B)

Proposed resolution

Explicit check on whether Node::load returns null before building the book_navigation on the view page

Remaining tasks

Maybe an automated test?

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

Ensure entity view page does not break when book node is missing

๐Ÿ› Bug report
Status

Needs work

Version

10.1 โœจ

Component
Bookย  โ†’

Last updated 3 months ago

  • Maintained by
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States @pwolanin
Created by

๐Ÿ‡ณ๐Ÿ‡ฑNetherlands LaravZ

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

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.71.5 2024