0

I have created a view, which basically queries files of type image. That view creates a block, which associated with a certain content type. So the block is displayed whenever one of the nodes of that type is displayed, exactly what I would like to have.

Each of the files displayed has an entity reference field, which contains multiple references to that content type. So that is a one to many relationship, where one image can have multiple nodes of that type associated.

Is it possible within the views ui, to set a contextual filter that it sorts out each file, whose entity reference field (to that content type) contains the current node id?

In pseudo SQL the statement should look like:

SELECT filename, path, node_refs FROM files WHERE node_refs contains <current node id>;

Thanks in Ahead!

1
  1. Add a relationship as Entity Reference: Referencing entity

  2. Name it with the name of your content type for instance (easier to read in the view settings)

  3. Check the Require this relationship option

  4. Add a contextual filter with Content: Nid

Here comes the trick!

  1. Choose Provide default value in the WHEN THE FILTER VALUE IS NOT IN THE URL fieldset

  2. Type should be Content ID from URL

This will make Drupal fetch the context from the current URL where your block is displayed and loop through the files referenced by the current node.

TA-DAAA :-)

  • Thanks a lot, that trick did the trick and was the missing part! – philipp Nov 16 '15 at 10:43

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.