1

I want to render a particular view like Who's new,Recent content present in the core in my the twig template.Any suggestion on how to do it? I was thinking of using views_embed_view() because in Drupal 7 it used to work with php but I don't know how to do it Drupal 8. Thanks. For recent I tried following code:

function template_preprocess_module(&$variables) { $variables['view_output']=views_embed_view("Recent content"); }

and then in the twig template did this:

{{ view_output}}

  • This function still exists in Drupal 8 and takes the view name as the first parameter :) – alexej_d Jun 15 '15 at 20:14
  • @alexej_d I know this and tried to render in the twig template but it didn't worked for me. I have updated the question with the code. – holdnet Jun 15 '15 at 20:22
  • You are not using a machine name but the title you defined. – alexej_d Jun 15 '15 at 21:42
  • 1
    @holdnet views_embed_view must take a machine name and not title. – Sumit Madan Jun 16 '15 at 4:39
3

Right, twig no longer allows you to execute php code anymore. This is a main feature of twig and its security model.

I'd suggest to use a preprocess function for now and there call the function or even better add a render array with #type view, with #view_id and #display_id, as it gives you caching as well.

On top, maybe add an issue against core. That sounds like a really helpful thing to have

Oh and views_embed_view should be changed to enable caching as well.

2

You might be interested by the contrib module Twig Tweak.

Among other things, it will let you render views like this :

{{ drupal_view_result('view_name', 'view_display_name') }}

Cheat sheet : https://www.drupal.org/docs/8/modules/twig-tweak/cheat-sheet-8x-2x

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.