In Drupal 7, I used the following code implementing views_embed_view in a render array.

  $view_output = views_embed_view($view_name, $display_id, $uid);

  $page['profile']['bio_mobile'] = array(
    '#type' => 'markup',
    '#markup' => $view_output,
    '#prefix' => '<div class="mobile-display">',
    '#suffix' => '</div>',

How do I do that using Drupal 8? Although the function is still documented, it return nothing more than just the array I passed in.


I don't know about using #markup but I can create a variable with a renderable View array like so:

  // Staff snippet.
  $view = Views::getView('staff');

  if (count($view->result)) {
    $variables['user_snippet'] = $view->buildRenderable('user_snippet');

Then in the twig file I am sending the variable to:

{% if user_snippet %}
  {{ user_snippet }}
{% endif %}
  • Thank you for the feedback. Seems so complicated compared to the good old days of Drupal 6 and 7. However, that may be the best (only) way... So, how do you "send the variable" to a twig file? – sea26.2 Feb 3 '19 at 22:30
  • 1
    I actually think this is better. If you check the old function it just calls this code anyway. This was done in a preprocess hook for a node. It calls ‘buildRenderable’ to fetch a renderable array of that View. It’s added to the $variables array, which is what is sent to Twig. – Kevin Feb 3 '19 at 22:35
  • I'm marking this as the solution. Though the answer from noSsseat also works. It seems that there is more control here with the use of the template... The only thing that really bothers me is that I can not figure out how it is working. We are passing the Twig Template an array... how does it "know" what to do with it? I can pass a simple string to it (like "hello world") and it works... magic powers??? Thank you for the help! – sea26.2 Feb 4 '19 at 17:45
  • 1
    The $variables is just an array of items for a template. It can be any value and it can also be a renderable array. The buildRenderable method returns a renderable array for that View. The information on how to render it is inside the array, which means it works the same way as rendering content in a node like {{ content.FIELD_NAME }}. Any preprocess hook for Views to change how its theming or what its theming should still work. Drupal 7 worked the same way. – Kevin Feb 4 '19 at 17:58

Indeed, #markup only allows string, but you can by using a block type

$view_output = views_embed_view($view_name, $display_id, $uid);

$page['profile']['bio_mobile'] = array(
  '#type' => 'block',
  'content' => [
    'system_main' => $view_output,
  '#prefix' => '<div class="mobile-display">',
  '#suffix' => '</div>',
  • Bingo! I knew there had to be a way to digest the output from that function. I'm guessing there are existing #themes that would probably handle it as well. I'm favoring the first answer because it gives more flexibility with the theme file that I would have had to build into the view before embedding it. Thank you helping - I'll probably use that logic in another spot. – sea26.2 Feb 4 '19 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.