I am trying to render custom view and put the result as content of the node.
Here is my code
function my_theme_preprocess_node(&$variables)
{
if (isset($variables['node']) && $variables['node']->getType() == 'custom_type') {
$reference_view = $variables['node']->field_reference_view->first();
if (!empty($reference_view)) {
$view_name = $reference_view->getValue()['value'];
$view = \Drupal\views\Views::getView($view_name);
$view->setArguments(array());
$view->setDisplay('block');
$view->preExecute();
$view->execute();
$variables['embedded_view'] = $view->buildRenderable();
}
}
}
But this code shows only debug message and when I try to get output in a twig template file it is empty
{{ node.embedded_view }}
What is wrong with my code. How can I render a view and set it as a content of node or just node field in order to print it in a twig template file.