0

I'm currently trying to output views referenced in a node (with Views Reference Field) inside page.html.twig Twig template.

I know I can just add or embed a view using {{ drupal_view('machine_name', 'block_1') }} (with Twig Tweak) in my Twig template. But I want the editors to have the flexibility to add or change views in the node and have them printed without writing code.

I can't seem to find any documentation on how to output a referenced view to my Twig template.

enter image description here

enter image description here

  • 3
    Its just a field, so the views reference field can be rendered with {{ content.field_your_views_reference_field }}. If you want to change the output of the view, use the views template suggestions. – Hudri Jul 26 '18 at 5:44
  • I tried that on my page.html.twig node.field_view_works.title but it outputs a numerical value 1 – clestcruz Jul 26 '18 at 8:09
  • For the field label you can use {{ node.field_myfield.fieldDefinition.label }}. For nested field values from inside your view, you must use the proper view templates, you can't easily access them in page or node. – Hudri Jul 26 '18 at 8:28
  • if I'm not mistaken, views template is somewhat like this views-view-list.html.twig How can I call the template within my page.html.twig? – clestcruz Jul 27 '18 at 8:41
  • You don't call them directly, you just create a file with a corresponding name in your theme folder and it gets called automatically by {{ content }}. I'd suggest to take a look at core theme stable and look for view-*.html.twig there to find out which variables are available in those files. Remember there a different view types, this tutorial has a good overview which view uses which file names. – Hudri Jul 27 '18 at 11:06
4
+50

Got it!

You have to pass the view ID and the display ID to drupal_view() dynamically.

But first let me tell you that what you are doing, printing node stuff inside page level templates, should be considered bad practice. Good practice is to leave templates untouched, and to offer field formatters and other stuff instead that keeps the admin UI intact. So, when you get your first maintenance breakdown in a year or so, don't tell us we didn't warn you.

page.html.twig

{% if node.field_views and not node.field_views.isEmpty() %}
  {% for view in node.field_views.value %}
    {{ drupal_view(view['target_id'], view['display_id']) }}
  {% endfor %}
{% endif %}

You won't get the view title from here. Simply add some Global: unfiltered text to your view HEADER with any title markup. That will get printed.


drupal_view() requires Twig Tweak.

  • 1
    Hey thanks for this, honestly I didn't know about this. I tried printing the view reference field {{ node.field_news_view.value }} just to see if there was any output that will be printed to my twig template, but it came out empty – clestcruz Jul 31 '18 at 2:01
  • As @leymannx points out is a bad practice to hard code field rendering inside a template, although you have a formatter provided by the module itself check \Drupal\viewsreference\Plugin\Field\FieldFormatter\ViewsReferenceFieldFormatter. For more versatility you may use different view modes to output your the field. – d70rr3s Aug 1 '18 at 2:00
2

I've managed to render the view using view reference field module by printing {{ page.content }} but is there a way to explicitly to print and instead of using {{ page.content }}

At the moment you have the Views Reference Field in a content type and with {{ page.content }} you print the node, which is the main content of a node page.

If you want to render the View anywhere else on the page, without code, then

  1. Create a custom block type: /admin/structure/block/block-content/types
  2. Add a Views Reference Field to the block type.
  3. Create a custom block for this block type: /admin/structure/block/block-content
  4. Fill the reference field with a View.
  5. Create a custom region: https://www.drupal.org/docs/8/theming-drupal-8/adding-regions-to-a-theme.
  6. Place the block in the custom region: /admin/structure/block
  7. Print this region anyhwere in the page template: {{ page.custom_region }}

This is of course only necessary if content editors should be able to add or change the View. To control the permissions for block types the same way as for content types install the module Block Content Permissions. If you want to display this block only on certain node pages you can configure the block visibility.

  • Just to clarify, I was looking for a way to print the views reference based on what the user has chosen on the node edit to my twig template i.stack.imgur.com/a5dOB.png. – clestcruz Jul 30 '18 at 16:09
  • Page layout in core is block based. I've added some further explanations how you can make the block content available to site users. – 4k4 Jul 30 '18 at 16:41

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.