I have a view which is displaying nodes in the teaser view mode.
I would like to to have all those teasers inserted into a details html element.
Here is my code for doing it (code 1):
function mymodule_preprocess_node(&$variables) {
$variables('content') = (
'#type' => 'details',
'#title' => $variables('label')(0)('#context')('value'),
'content'=>$variables('content'),
);
}
I have tried to use the following code without success (code 2):
function mymodule_preprocess_node(&$variables) {
$variables('content') = (
'#type' => 'details',
'#title' => $variables('label')(0)('#context')('value'),
'content' => (
'#theme' => 'node',
'#node'=> $variables('node'),
'#view_mode'=> 'teaser',
),
);
}
The idea was to re-use my teaser twig template but when doing this, the site is taking too much time to display the page (in fact it doesn’t display it…)
Same thing if I use 'content' =>Drupal::entityTypeManager()->getViewBuilder('node')->view($node,'teaser')
As only code 1 is working, I have added a template suggestion for the details element in order to re-create the layout of my teaser twig template. (same code in 2 places)
This is a general question: is it the best approach?