I tried solving the following with "node_reference" and "entityreference" modules but failed.

I have the content types recipe and cook. A recipe has a mandatory field which points to a cook.

When Drupal renders a recipe I want to have a block on the sidebar (implemented via "views") which will be displaying info on the recipe's cook.

How can the block know which recipe is being rendered on screen in order to use the relationship and pull the cook's data as fields which I'll use for display within the block?


More info: The problem is that in the block I'm getting all referenced (from recipes) cooks and not just the one cook for the specific recipe. So for the following data:

recipe#1 -> cook#1
recipe#2 -> cook#1
recipe#3 -> cook#1
recipe#4 -> cook#2

When I visit any of the recipes the block displays data from:


2 Answers 2

  1. Create a Block views display.
  2. Add "Filter Criteria" of Content: Type and select (tick) your "Recipe" content type.
  3. Add "Relationship" (may need to expand "Advanced" fieldset on your Views edit screen), and for your Cook reference field that exists in your "Recipe" node type. Afterwards, any fields such as Content: Title should have a Relationship dropdown on their settings screen. Make them use this new Relationship you just added. (I beleive you got up to this part judging from other answers' comments. Here's the key part:)
  4. Add "Contextual Filter" and select Content: Nid field, and on the field settings set "WHEN THE FILTER VALUE IS NOT AVAILABLE" to "Provide default value" and set the "Type" to "Content ID from URL".

Now when you visit a Recipe page that has this block somewhere, or if you put a Recipe content's Nid into the Views preview arguments box you should get the Cooks for that Recipe.

  • YES! That's it. Thanks a lot. I knew I was somehow missing the "current node" filtering.
    – cherouvim
    Apr 3, 2012 at 8:49

You would need to add a relationship from the reference field in views (under advanced > relationships). This will extend the scope of fields available to the view include those of the referenced node (Cook).

However this will as you state show all cooks as it will come from all recipes. In order for the block to know how to filter out all irrelevant cooks (ones not referenced) you will need a contextual filter which knows what recipe you are currently on.

On this contextual filter you can specify the relationship to the one you have already created, then you will need to let the block view know which recipe you are on. You do this with the setting "provide default value" and "Content ID from URL". This will tell the view which content you are on and hopefully apply the filter. Sorry I do not have access to D7 right now for a better explanation

  • The problem is that in the block I'm getting all referenced cooks and not the cook for the specific recipe. Would I need anything special in the "Filter Criteria" section?
    – cherouvim
    Apr 2, 2012 at 11:10
  • How many recipes do you have referencing a cook? Because the view might need some filtering of the actual recipes if there are too many. Try filter down to a single recipe and you should then ONLY have the cook from which this single recipe refers to.
    – WestieUK
    Apr 2, 2012 at 11:13

Your Answer

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

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