I currently have Two content types:

Content Type 1: Videos

Content Type 2: Politicians

The main Videos content type references the Politicians content type as a way of "tagging" Politicians that can be seen in the videos. What I'd like to do on each Video node is create a view that shows all other video nodes that have the same Politicians referenced within them. For some reason I cant seem to get it to work. This is what I have so far.

  • Create a view block with title field from the Video id like to see

  • Create a relation to the Politicians field with a reverse

  • Create a filter based on Content NID

For some reason when i go to a Video node I don't see any results at all. In fact I don't even see the video of the node I'm currently on.

If I change the relation to Politicians field without the reverse I only see the same node that I'm currently on. I should be seeing all videos with the same politician. What am I doing wrong here?

Keep in mind I and not using and wont be switching to the Entity Reference module since this is a large legacy project (I'm using the References module).


You could achieve your goal by building two views

The first is the view you have build with relationship to politicians content type, Add new field gets the politician id tagged in that video

The second is a view that gets videos for politicians ids sent as a contextual filter

that accepts multiple values by checking that checkbox as in the picture enter image description here

Install that module views field view that enables you to embed a view inside another view as a field

Add new field in the first view that embed the second view and send the politicians ids as tokens to that view

Now you could achieve your goal

  • Is it not possible to do with only one view? I'd really like to keep things clean if I can get away with it. Even if that means custom code – doodirock Oct 28 '15 at 14:12
  • So you could write custom code Implementing one of views hooks – Mohammed Gomma Oct 28 '15 at 19:53

I think @Mhmdgomma is right, Views Field View module is useful here. Furthermore, it can be done with one view, albeit containing more than one display.

We will create one view with two displays. One serves as the video overview page, the other display is used to pull in the related videos.

Start with installing the Views Field View module to achieve your one-view goal.

Create the view with the displays

  • Create a view "Videos" with a page display to display your video nodes. I named the page display "Overview Page".
  • Create a block display for that view. I named the block display "Related Videos Block". View configuration with 2 displays

Configure the Related Videos Block

  • Create a Relationship to field_politicians in video nodes. Do not pick the reverse reference.
  • Create a Contextual Filter with Content: NID, which uses the relationship field_politicians and allow multiple values.
  • Create another Contextual Filter with Content: NID, without a relationship, set it to exclude.

Test the block display by entering a NID from a politician node into the "Preview with contextual filters" field. You should see a list of videos tagged with this politicians NID. Related Videos Block configuration Relationship Contextual Filter for politician NIDs Contextual Filter to exclude current video node from related videos Test result when entering one or more politician NIDs

Configure your Overview Page

  • Create everything you like to display for your video nodes
  • Create another field from field_politicians, exclude it from display, use the Node ID Formatter, and display all values on one row with a comma separator and no additional space.
  • Create a field from Content: NID and exclude it from display. This is the current video nodes node id.
  • Create a field Global: View (from the Views Field View module), embed the current "Videos" view. As display, pick the "Related Videos Block".
  • Configure the Contextual Filter by using the provided Replacement Patterns. My configuration ended up being "[%field_politicians_1]/[!nid]".

The first parameter is the rendered output from the politicians field, a comma separated list of politician node ids. The second parameter is the video nodes NID, used to remove the current video from the list of related videos.

Hidden politician NIDs field used as input for the embedded view Hidden video NID field used as input for the embedded view The embedded view with parameters to trigger the Contextual Filters

Optionally add a Contextual Filter with Content: NID to display a single video with its related videos, or create another display for this detail view. If you enter a video node NID, you should see that single video with its related videos.

Optional Contextual Filter

  • I'm starting to think with all the hoops I would need to it would be easier to just write my own query and skip views completely. I really dont want to a view that replaces my node template that I've already styles and have working fine. – doodirock Nov 4 '15 at 22:29
  • Actually I wouldn't use it as a node replacement, but a view of a list of nodes. If you want to display the Related Videos Block on your Video Node page, you can embed the block view in your node template, passing the needed parameters from your video node to the views contextual filter. – suit4 Nov 5 '15 at 6:45

![Entity reference field clearly check the description A bridge to the Content entity that is referenced via field_politicians ]1

My complete view overlook

Check the sql Preview query for the field name "node_field_data_field_politicians" which we r going to use in code

enter image description here

[![enter image description here][5]][5]

select distinct for the query settings

My views query alter hook implementation

  • Im not using Entity Reference as described in my post. Im using the References Module – doodirock Oct 26 '15 at 20:51

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