Problem/Motivation

We have a hook_rebuild that calls Url::fromRoute()->toString().
We calling drush cr, it works correctly. But when calling drush updb, it crashes with this error:

TypeError: SplObjectStorage::offsetExists(): Argument #1 ($object) must be of type object, null given in /var/www/web/core/lib/Drupal/Core/Render/Renderer.php on line 655 #0 /var/www/web/core/lib/Drupal/Core/Render/Renderer.php(655): SplObjectStorage->offsetExists(NULL)
#1 /var/www/web/core/lib/Drupal/Core/Render/Renderer.php(626): Drupal\Core\Render\Renderer->getCurrentRenderContext()
#2 /var/www/web/core/lib/Drupal/Core/Render/MetadataBubblingUrlGenerator.php(87): Drupal\Core\Render\Renderer->hasRenderContext()
#3 /var/www/web/core/lib/Drupal/Core/Render/MetadataBubblingUrlGenerator.php(110): Drupal\Core\Render\MetadataBubblingUrlGenerator->bubble(Object(Drupal\Core\GeneratedUrl), Array)

Steps to reproduce

Call something like this in a context where there is no current request:

// The route needs to exist.
Url::fromRoute('sitemap.page')->toString();

Proposed resolution

Renderer::getCurrentRenderContext() should check if $request is null before using it.

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3497935

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

prudloff created an issue. See original summary.

prudloff’s picture

Issue summary: View changes

prudloff’s picture

Issue summary: View changes

I just noticed that I did not have the error on another website.
Turns out Drush usually provides a current request but I was using this patch that somehow removed the request from the request stack (I am not sure how honestly).

I am keep the MR open because it might still be worth it to make Renderer::getCurrentRenderContext() more robust when there is no request.

quietone’s picture

Version: 10.3.x-dev » 11.x-dev
Issue summary: View changes

Changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to the Core change policies.

@prudloff, in the future can you leave all headings in the issue summary. They are used to keep track of progress.

prudloff’s picture

Status: Active » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Thank you for including test coverage! Makes it super easy to review

Left 2 small comments on the MR

If you are another contributor eager to jump in, please allow the previous poster at least 48 hours to respond to feedback first, so they have the opportunity to finish what they started!

prudloff’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Feedback appears to be addressed

  • catch committed ae7c8a62 on 10.4.x
    Issue #3497935 by prudloff, smustgrave: Renderer::...

  • catch committed e66f0fc3 on 10.5.x
    Issue #3497935 by prudloff, smustgrave: Renderer::...

  • catch committed fc979f15 on 11.1.x
    Issue #3497935 by prudloff, smustgrave: Renderer::...

  • catch committed e7bb6157 on 11.x
    Issue #3497935 by prudloff, smustgrave: Renderer::...
catch’s picture

Version: 11.x-dev » 10.4.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 11.x and cherry-picked to 11.1.x, 10.5.x, 10.4.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.