Drupal - How can I embed a View inside a content of a node?

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.


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.


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

Tags:

Views

Nodes

8