1

I try to create a view which would list related content (some articles) to a content type article.

Here is what i’ve done. I’ve created a field "Entity reference" in the content types "Article". It works. I can see in database the related article ID .

enter image description here

Then, I create a view with a block. It works, i can see all articles listed in that view (which is visible on each article).

Here is my issue. I need to display only articles related to the current article. So, in contextual filter, i have tried with Content: ID and also tried with advanced/Relationships but without success. I don’t see my related articles.

enter image description here

enter image description here

enter image description here

How can I do ?

I’ve also tried a different way (create a twig) and even if it works, it is not the best solution i think

1 Answer 1

4

Ignore using fields in the View. This has been and will always be problematic IMO for 95% of Views people are making.

enter image description here

You need the Content ID argument from the URL so the correct node is selected, but here is where it takes a turn:

Set the view to show "Content" and give it a view mode, for instance, create a view mode called "Related Content" for nodes. In the field display for that view mode, only set the "Related Articles" field to be visible. Set its field formatter to "Rendered Entity", and create a view mode for that content type and theme it. On that content type, you would have title and body, or whatever fields you want to show (hard to tell without seeing a design).

Now the View will pull the node, render it, and in turn render the nodes that are being referenced in a view mode.

Then, you would get something like this, for example:

enter image description here

That is a View that looks at the current node, and renders it. The view mode has a field (Related Content), which is a node reference, and those nodes are rendering in a "Preview" view mode - and this is the result. All of the markup lives in two twig files (for the node--node-type--view-mode.html.twig) and the Views configuration is super simple.

The Views display would be a block, and you can place that block in whatever region works for you.

5
  • Thank you for this detailed answer. I’m actually working on it. I’ve made 50% of the job I think. It is not so easy but I understand the logic. I’ll tell you when it is done. Dec 19, 2017 at 15:43
  • Once you do it you'll see how simple it is. I use this pattern all the time for showing parts of a node in different regions, just like the picture above (that block lives in one of the full width footers).
    – Kevin
    Dec 19, 2017 at 15:52
  • I think I’m almost done : I can display related articles linked to the current article. The only thing I have to do is to theme the fields (something like your screen shot). For now, i have {{ content.field_suggestion_articles }} in node--view--vue-suggestion-articles.html.twig which output all the related articles at the same time. I need to theme all the fields separately. should I work with an other twig tpl like field--node--field-suggestion-articles--article.html.twig ? Dec 19, 2017 at 16:06
  • You are on the right track by rendering the content element. So, each of these field values is rendering the Article node in a view mode - so you can provide a view mode for that content type and theme it up and it will appear like that. Within those twig files, you will get a new set of field suggestions - check your twig debugging output for name hints. Think of it like theming a node, within a node.
    – Kevin
    Dec 19, 2017 at 16:18
  • In view mode "Related Content", we have the field "Related Articles" and not title, image, summary, etc. Since the "Related Articles" field is repeatable, i have {{ content.field_suggestion_articles }} in node--article--related-content.html.twig. In field--node--field-suggestion-articles--article.html.twig, i have {% for item in items %}{{ item.content }}{% endfor %}So I can’t access properly to custom fields. I can do something but with ugly code based on drupal.stackexchange.com/questions/208723/… Dec 20, 2017 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.