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