1

I have two content types on my site: "mountains" and "trip reports" with the idea being that users can create a trip report for any particular mountain they have visited.

My "trip reports" content type uses an entity reference field so that the user can select from the list of "mountains" content type.

I want to create a view to display as a block on the "mountains" pages that lists the recent trip reports associated with that mountain. How do I structure my filter in Views? I can either get all trip reports to show up (i.e. trip reports associated with all mountains, or none to show up!

Here's my current view setup:

Fields:

  • Content: Title

Filter Criteria:

  • Content: Published (Yes)
  • Content: Type (= Trip Report)

Contextual Filters:

  • Content: Title

Relationships:

  • Entity Reference: Referencing entity
0

You need to include a Node ID contextual filter as well that is set to Content ID from URL. Then make sure that each of the Fields are set to relate to the relationship entity reference field you are using. I'll try to explain below.

Adding a Contextual Filter that looks for the Content ID from URL is required as the view will use the found nodeID and use that to know what node it should relate it's fields and relationships too.

You will then want to add a Relationship field that is the Entity Reference field that creates the relationship between the Trips (children) and the Mountain (parent).

Now with the fields, if you just add fields and do not modify any settings, the view will only display content that is available in the original node. For instance, adding the Title field will display the title of the Mountain and not the title of the Trip reports. This is because it doesn't know it should get the information from anywhere else, even though we added the Relationship field before.

This is why you need to modify the fields and set their Relationship to the Entity Reference field. This will now tell the view that even though we are on the Mountain node and have it's ID, the fields are going to output the content related to the entity that is referenced by the nid in the contextual filter.

  • This doesn't seem to work, either. Is this because I'm trying to display my view in a block, and a block doesn't have the context of which page it's being served from? – the_meter413 Mar 12 '17 at 3:49
  • That's what the NID contextual filter is for – Travis Johnston Mar 12 '17 at 3:51
  • Sorry I hit enter early. That filter tells the block where it is and what it should use to render the results. You can test this ahead of time by using the views argument input box above the preview and enter in the node ID. If it renders results there and not on the page when it's saved, then there's another problem. – Travis Johnston Mar 12 '17 at 3:53
  • Thanks, Travis! Here's what settings I used to make it work: Under Fields, I select "content:title" with no relationship. Under Relationships, "entity reference: Trip Location (Mountain)". Under Contextual Filters, "Content:NID," with relationship set to "content entity referenced from field_trip_location_mountain, and "provide default value: Content ID from URL." But I don't understand exactly why this works. Can you elaborate on your original answer to explain the rationale behind these settings? – the_meter413 Mar 12 '17 at 6:47
  • OK I did my best above. – Travis Johnston Mar 12 '17 at 14:09

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.