Problem/Motivation
After upgrading to 8.8, our tests started throwing exceptions in various places that weren't checking if $route_match->getRouteObject() returned NULL. One of those places is in layout_builder_system_breadcrumb_alter
I'm not entirely sure what changed between 8.7 and 8.8, but it seems to be to do with rendering content controlled by layout_builder outside of a route context, for example during search indexing.
See the full stack trace below:
1) Drupal\Tests\apra_publications_search\Functional\PublicationsSearchTest::testPublicationsSearch
Error: Call to a member function hasOption() on null
/data/app/core/modules/layout_builder/layout_builder.module:336
/data/app/core/lib/Drupal/Core/Extension/ModuleHandler.php:539
/data/app/core/lib/Drupal/Core/Breadcrumb/BreadcrumbManager.php:94
/data/app/core/modules/system/src/Plugin/Block/SystemBreadcrumbBlock.php:72
/data/app/core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php:104
/data/app/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php:111
/data/app/core/modules/layout_builder/src/SectionComponent.php:90
/data/app/core/modules/layout_builder/src/Section.php:86
/data/app/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php:317
/data/app/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php:276
/data/app/core/lib/Drupal/Core/Entity/EntityViewBuilder.php:351
/data/app/core/modules/node/src/NodeViewBuilder.php:24
/data/app/core/lib/Drupal/Core/Entity/EntityViewBuilder.php:293
/data/app/core/lib/Drupal/Core/Entity/EntityViewBuilder.php:250
/data/app/core/lib/Drupal/Core/Security/DoTrustedCallbackTrait.php:100
/data/app/core/lib/Drupal/Core/Render/Renderer.php:781
/data/app/core/lib/Drupal/Core/Render/Renderer.php:372
/data/app/core/lib/Drupal/Core/Render/Renderer.php:200
/data/app/core/lib/Drupal/Core/Render/Renderer.php:156
/data/app/core/lib/Drupal/Core/Render/Renderer.php:573
/data/app/core/lib/Drupal/Core/Render/Renderer.php:157
/data/app/modules/contrib/search_api/src/Plugin/search_api/processor/RenderedItem.php:276
/data/app/modules/contrib/search_api/src/Item/Item.php:272
/data/app/modules/contrib/search_api/src/Entity/Index.php:975
/data/app/modules/contrib/search_api/src/Utility/PostRequestIndexing.php:84
/data/app/modules/custom/apra_tests/tests/src/Functional/ApraTestBase.php:90
/data/app/modules/custom/apra_publications_search/tests/src/Functional/PublicationsSearchTest.php:33
Proposed resolution
Check $route_match->getRouteObject() is not NULL.
| Comment | File | Size | Author |
|---|---|---|---|
| #19 | 3104980--19.patch | 2.19 KB | danflanagan8 |
| #19 | 3104980--19--FAIL.patch | 1.12 KB | danflanagan8 |
Comments
Comment #2
acbramley commentedI'm not sold that this is the right fix, but it gets around the issue for us for now.
Comment #3
tim.plunkettLooks exactly correct to me.
Whoever wrote that (me) should have checked the return value on
\Drupal\Core\Routing\RouteMatchInterface::getRouteObject()which clearly indicates it can be NULL.This may be tricky to test, but we should try! Marking NW for that.
Comment #4
acbramley commented@tim.plunkett awesome! I'll have a bit of a poke around and try and come up with a test.
Comment #5
acbramley commentedI've been trying to work on this on and off, I think a Kernel/Unit test would probably be best but I was playing around with this Functional test too. I couldn't get it to fail as expected but here it is so far.
Comment #6
tim.plunkettComment #7
jhedstromThis appears to happen, for instance, when indexing search_api items via drush that might be rendering a layout-builder-based page.
Comment #8
jhedstromI think this test instead of being functional (which on request will always have a route object), if it were to instead be a kernel test and attempt to render a node that is layout-builder-enabled, it would fail as expected.
Comment #10
josephdpurcell commentedI ran into this issue after upgrading from 8.7.7 to 8.8.8. I am using Search API (with SOLR) to index the rendered content.
Thank you for this patch. After applying #2 I can confirm the error went away during indexing.
I'm linking other tickets that I also ran into errors with, or found to describe similar symptoms / resolution. It would be convenient if Core had a way to return an object still instead of null, but no idea if thats possible -- I didn't see a ticket in core about this.
It's unclear why this is "needs work", so setting back to "needs review" just in case that was done on accident.
Comment #11
tim.plunkettThe patch needs automated tests. #5 didn't fail, so that was insufficient.
Comment #12
AmiOta commentedHi,
I have the same problem on my drupal 8.9.x with the module Sarch API installed, the patch #2 solved the issue for me.
Comment #15
kim.pepperComment #16
thomaswalther commentedI have drupal 8.9.13 and search_api 8.x-1.19
patch #2 do not worked for me still got the error.
Comment #17
tim.plunkettThat error message points to the easy_breadcrumb contrib module
Comment #18
danflanagan8Here's a go at test coverage. I stumbled upon the rarely used
NullRouteMatchclass, which looks perfect for us. Lots of false starts before I found that, though.Comment #19
danflanagan8Let's try again...
Comment #21
acbramley commentedNice find! Looks good to me!
Comment #22
andypostTest is great, looks ready
Comment #23
danflanagan8It looks to me like the FAIL patch is set to "re-tests every 2 days". Is there a simple way to indicate that it's the other patch that should instead be tested?
Comment #24
andypostFixed, just set retest to required patch
Comment #25
larowlanCommitted ef493a7 and pushed to 9.3.x. Thanks!
As there is little risk of disruption here, backported to 9.2.x