"LogicException: The render array has not yet been rendered" (after it has)

Created on 24 June 2019, over 5 years ago
Updated 30 July 2024, 3 months ago

Problem/Motivation

I dug quite some time debuggung an ajax controller throwing that exception:
LogicException: The render array has not yet been rendered, hence not all attachments have been collected yet. in Drupal\Core\Asset\AttachedAssets::createFromRenderArray() (line 36 of core/lib/Drupal/Core/Asset/AttachedAssets.php)

Proposed resolution

* Roll a test from below code
* decide how to fix the condition of said exception
* implement it

Background: In \Drupal\Core\Asset\AttachedAssets::createFromRenderArray

    if (!isset($render_array['#attached'])) {
      throw new \LogicException('The render array has not yet been rendered, hence not all attachments have been collected yet.');
    }

But checking for '#attached' is not the correct condition here. We can fix this by either
* Checking for something else
* Making \Drupal\Core\Render\Renderer::doRender always set #attached=[] as said code expects.

In my ajax code this was triggered like so:

$torender = []; 
=> []
$response = new \Drupal\Core\Ajax\AjaxResponse();
=> Drupal\Core\Ajax\AjaxResponse {#3871
     +headers: Symfony\Component\HttpFoundation\ResponseHeaderBag {#3869},
   }
$command = new \Drupal\Core\Ajax\ReplaceCommand('#foo', $torender);        
=> Drupal\Core\Ajax\ReplaceCommand {#3885}
$response->addCommand($command);
LogicException with message 'The render array has not yet been rendered, hence not all attachments have been collected yet.'

... and an elementary test looks like this:

$torender = [];                                                 
=> []
$html = \Drupal::service('renderer')->renderRoot($torender)     
=> ""
$assets = \Drupal\Core\Asset\AttachedAssets::createFromRenderArray($torender)
LogicException with message 'The render array has not yet been rendered, hence not all attachments have been collected yet.'

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Fixed

Version

11.0 🔥

Component
Asset library 

Last updated 2 days ago

No maintainer
Created by

🇩🇪Germany geek-merlin Freiburg, Germany

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.

  • 🇦🇺Australia mstrelan

    Is this still relevant? I tried the first example in the issue summary with $response->addCommand($command); but could not reproduce the error. The second example with AttachedAssets::createFromRenderArray($torender) did result in the same error. So perhaps this is solved in another way?

  • Status changed to Fixed 3 months ago
  • 🇩🇪Germany geek-merlin Freiburg, Germany

    The \Drupal\Core\Ajax\CommandWithAttachedAssetsTrait::getRenderedContent now contains this line

          $html = \Drupal::service('renderer')->renderRoot($this->content);
    

    which in fact makes this fixed.

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

Production build 0.71.5 2024