0

I want to display a specific view as a field in nodes. It’s the same view for all nodes of this type, making use of the contextual filter "Content ID from URL".

I found two solutions using contributed modules:

  • EVA: Entity Views Attachment: it’s specifically for this use case
    (there is also Viewfield, but that’s still in alpha)

  • Display Suite: it’s not its primary use case, but makes it possible by adding a "block field", where you can select the block generated by the view

Assuming that Display Suite is installed anyway (installing it just for this use case would be overkill, I guess), is one solution better than the other? Both seem to work fine, but I’m thinking of possible issues I can’t foresee now, like performance impact or compatibility with the Drupal ecosystem.

  • If it is the same view block on every full view of a node type, you don't need any module at all, just place the block and limit visibility of the block to the node type. You only need a module if you want to place the view block in between node fields. If that's the case, I agree with 4k4 (EVA for sitebuilders, Viewsreference for editors) – Hudri May 11 '18 at 9:33
  • @Hudri: Yes, the requirement is that the view can be used as field (so it can be moved under "Manage display"). – unor May 11 '18 at 16:43
  • @unor you can use Viewfield for what you need. See my steps to add it as it is quite simple. We have been using it for multiple views. – usernameabc Aug 12 '18 at 22:03
  • 1
    As mentioned below, the two options are Viewfield and Views Reference Field. However, they should be merged as per Merge the Views Reference Field and Viewfield modules. That's a better long-term plan. – colan Oct 25 '19 at 21:54
2

I think the best contender for EVA is Views Reference Field:

Both have stable releases. Main difference is the workflow. EVA is perfect if you want to impress your co-workers with the ability to attach a View to a content type like magic. Views Reference Field gives content editors the power to add Views to content themselves. Both modules don't need a block display, EVA provides its own and Views Reference Field can use any display type.

  • For paragraphs, we actually use drupal.org/project/block_field, which has the benefit that you can also use the same pattern for any block, things like forms and so on. And while the UX is not very good yet, there is I think more potential for improvements because you have the block configuration form, can use plugin contexts and so on. – Berdir May 11 '18 at 8:23
  • In case there is a misunderstanding: the solution I meant using Display Suite doesn’t add the blocks to the block layout (at /admin/structure/block). The blocks have to exist, but you don’t have to place them (i.e., creating a block display in Views is sufficient). – unor May 11 '18 at 8:31
  • Yes, but you have them in the select list when you place a block. I think Views Reference Field is the more flexible solution, because you can choose any Views display not only blocks. – 4k4 May 11 '18 at 8:50
  • @Berdir, I removed my comment about not using blocks in fields, there are many valid use cases. For Views I think you don't need this anymore, now that these two great modules are available. – 4k4 May 11 '18 at 9:26
2

We have found viewfield 8.x-3.0-alpha2 to be pretty stable even in alpha. Based on your description, here is how you can use it.

  1. Add field where type = viewfield
  2. select a view to be added to the content type.
  3. if you are wanting to use the node id as a filter, you need to set the filter on the field settings. It allows you to do use all the tokens, even custom one. In your case it should be [node:id]. I believe it would be under Arguments to be passed to the display
  4. Under Manage Display for the content type, ensure it is enabled and in the location you need it to be.
  5. the viewfield comes with two twig templates that you can override as needed with relate template_preprocess_viewfield() and template_preprocess_viewfield_item() hooks.

Considerations:

  1. Since it is a field on the content type, remember to add the field to either the twig template or display suite view so that it displays on the screen.
  2. Views have different displays so when you select what View to add to the field, remember to select the display you need. For example, master,block,page`, etc. can all be selected.
  3. You can reuse the field on multiple content types. Just remember to check that all the important field settings are set such as [node:nid] as a filter, the View display is enabled, and the correct view is selected.

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.