I have a controller action that renders a list nodes and returns them, and only them. It's invoked via AJAX and the JS code just takes the result and inserts it into the page somewhere.
My problem is getting the cache metadata from the render array of nodes and passing it to the response. Here's the simplified action:
public function myControllerAction() {
$builder = \Drupal::entityTypeManager()->getViewBuilder('node');
// Get render array containing a few nodes.
$nodes = $builder->viewMultiple([1,2,3], 'teaser');
$response = new CacheableResponse();
$response->setCacheableMetadata(CacheableMetadata::createFromRenderArray($nodes));
return $response;
}
The problem is CacheableMetadata::createFromRenderArray()
expects you to not give it a nested render array, but that's what the view builder returns. It returns an array of render arrays.
How can I efficiently extract the metadata from each node in the list and apply it to the response?
merge
?