I have a situation with three content types. (Simplified)

A Person is referenced in many Transactions, and a Transaction also has a reference to a Company. I.e. Person -< Transactions >- Company

Here the Transaction holds the entity reference to a Person, and a reference to a Company. I need to create a view that given a value from the Company content type, returns all the Person entities.

In SQL I would do something like this, to give me Persons who have had Transactions with a specific company:

    FROM Person P
    JOIN Transaction T on T.PersonId = P.Id
    JOIN Company C on C.Id = T.CompanyId
    WHERE C.Name = 'Drupal Co.'
  • And how company is going to be passed to your view? – Alireza Tabatabaeian Aug 14 '15 at 9:38
  • The company name is passed through a Contextual Filter, so the View's REST endpoint will be something like example.com/rest/myView/% – Glagnar Aug 14 '15 at 9:41

It's quite simple: Create a view which shows content of type Person (or users, if for Person you mean users).

Now let's say that Transaction has an Entity Reference field named field_person and another one named field_company.

In the view's Advanced section click on Add relationship, search for field_student and check "A bridge to the Content entity that is referencing Content via field_person" if Person as a node type or "A bridge to the Content entity that is referencing User via field_person" if Person is a user. In the configuration window that pop ups check "Require this relationship" and save. You have just configured the first join.

Add another relationship. How that you have configured the first one, you can search for field_company and check "A bridge to the Content entity that is referenced via field_company". In the configuration window click on "Require this relationship" and check that the Relationship combobox has the first relationship selected. Now you have configured also the second join.

Finally create a Contextual filter. Search for "Content: Nid" and check it. In the configuration window, the Relationship combobox must have the second relationship selected. Choose what to do "When the filter value is NOT available" (this depends on what you want to do).


EDIT: I noticed just now that you search Company by name, not by NID. Just change the field in the contextual filter!

| improve this answer | |
  • Thank you - the problem was understanding the 'Content using field_...' vs. 'Content referenced from field_...'. – Glagnar Aug 14 '15 at 11:01
  • You're welcome! The problem is when you have a lot of chained relationships, you get lost in a sea of referencing/referenced. In that case you can try to give them custom, more clear names. – user1527576 Aug 14 '15 at 11:11

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.