1

Please how can I embed a view in a node template like node--blog--full.html.twig.

I have configured an about-the-author views to be embedded in full mode of blog content type using views-embed-view(), but it doesn't work. I have enabled the "Allow embedded displays" from views settings and followed a few tips online, but none seems to work.

  1. Is there a better way to embed views in D8 node template?
  2. How can I use views-embed-view correctly in D8?

Many thanks

1 Answer 1

1

Just to be sure, are you calling views-embed-view(), or views_embed_view() (Note the dashes/underscores). The second version (views_embed_view() is the correct one).

Also, how do you call the function? Which parameters do you pass?

As per this page, the correct way of embedding a view is 1 of the following ways.

Option 1:

// Get the embeded view and render it.
drupal_render(views_embed_view('VIEW_NAME', 'DISPLAY_ID'));

Option 2

// Get the embeded view and render it.
\Drupal::service('renderer')->render(views_embed_view('VIEW_NAME', 'DISPLAY_ID'));

In this example you will have to replace VIEW_NAME with the machine name of your view, and the DISPLAY_ID should be replaced with the ID of the display you want to use (Or can be omitted to use the default display).

Also note that the second option is the better one, since the drupal_render function is deprecated, and will be removed before Drupal 9.0.0 (More info).

9
  • Martie, where should I place the solution you provided, in node template or .theme file. If in .html.twig file, should it be wrapped in {{}} ?
    – odili
    Commented Mar 17, 2016 at 8:47
  • @chikaodili I didn't work that much with Drupal 8 yet myself, but I would most likely place this kind of logic in a preprocess function for your node, then add it to the variables. eg. $variables['my_embedded_view']. In your twig template you should be able to call {{ my_embedded_view }} to display the view.
    – Martie
    Commented Mar 17, 2016 at 8:58
  • I know we are all learning D8 together, its still not very clear, but we have to continue to try.
    – odili
    Commented Mar 17, 2016 at 9:21
  • based on your directions this is what I have done. I added this to .theme file : function THEMENAME_preprocess_node(&$variables){ $variables['about_author'] = \Drupal::service('renderer')->render(views_embed_view('about_the_author', 'block')); }. Then places {{about_author}} in the node template. However am getting this error `Notice: Only variables should be passed by reference in THEMENAME_preprocess_node() (line 70 of themes/custom/THEMENAME/THEMENAME.theme).
    – odili
    Commented Mar 17, 2016 at 9:28
  • @chikaodili I'm guessing that's caused by passing the views_embed_view() directly in the render() function. Try changing it to somethign like $about_author_view = views_embed_view('about_the_author', 'block'); \Drupal::service('renderer')->render($about_author_view);
    – Martie
    Commented Mar 17, 2016 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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