8

How can I embed a view inside a content of a node in Drupal 8?

I found something similar, for example in the question about "How do I embed a view inside a node?" or the Viewfield module, but it is all for Drupal 7.

I tried with an Entity reference field pointing to a View but it display only a label and from the Manage Display tab I can choose only from Label and Entity ID. I need something like the "Rendered entity" option that is present on Entity reference fields pointing to other nodes.

enter image description here

9

Option 1

You can create a View Block. Then the view block will be available in the blocks section. Assign the view block to a region and show it on specific pages if you need to.

enter image description here

Option 2

Create a twig template file for your node and use

Twig Tweak

Twig Tweak module provides a Twig extension with some useful functions and filters that can improve developer experience.

Ex: <dd>{{ drupal_view('view_name', 'page_1') }}</dd>

Option 3

If you use Panels or Display Suite, you can embed the view as a field.

  • Thank you. I just tried Twig Tweak and it works great!.. It would be perfect for me. But I need to build a production website that needs to be stable and I see that this module is quite new, having only 312 downloads up to now. Do you know if is it reliable and it will be well maintained? – Andrea Apr 22 '16 at 9:28
  • @Andrea I think it is reliable as so far 0 bug reports. It's very new unknown module, but I am sure the number of people using it will go up as people discover it. – No Sssweat Apr 22 '16 at 9:32
  • 2
    Only the module developer knows that really @Andrea, the rest of us can only speculate or give an opinion based on experience. The release is marked stable and it's (allegedly) actively maintained. That's as much info as we can give here, but you might be able to get better assurances if you open a "Is this production ready?" issue in the module's queue – Clive Apr 22 '16 at 9:32
4

I help maintain the D8 branch of EVA, which is functionally similar to Twig Tweak's drupal_view() but works as a View display that attaches itself to specified entity bundles as an extra field. This means the View may be reordered within the entity Manage Display UI or e.g., dropped in a field group. Like Twig Tweak's function, it allows contextual filters to be passed as arguments.

The use case for EVA and for the Twig Tweaks solution differs from that for using an entity reference with viewsreference because the latter allows content editors to select different Views on each entity. EVA and drupal_view() inject the same View on every entity (although contextual arguments may change the View results).

If you're creating a bespoke theme there may not be any advantage to EVA vs. twig block injection, but if you want to allow sitebuilders to manage a View as if it were an entity field and configure where it appears using the UI, have a look.

2

There's now also a project called viewsreference, which allows you to embed whole views into nodes via a field (think entity reference, but for complete views): https://www.drupal.org/project/viewsreference

  • This module also gives the ability to configure options for the view, such as displaying the title and passing view arguments. – joekers May 20 '17 at 14:40

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.