0

I have a view, which only appears when you navigate to certain nodes.

Each node which it appears under, has a taxonomy with a field called 'related_content_entity' referencing some related content. The node only has one taxonomy. I need the view to display data from the related_content_entity for the current node's taxonomy.

The brick wall which I have seemed to hit, isn't the entity reference, but filtering the results down to the one field referenced in the taxonomy associated with the node.

I get expected behaviour up to this point; creating a required relationship to the entity reference like this:

Relationship

Gives me this result:

Result

Which is entirely expected. But specifically what I can't figure out it how to filter these results down to the current node's taxonomy field. There's not a single contextual filter which I've tried which would filter the result down to only the node's taxonomy's field.

To put what I'd like to acheive more clearly:

Node-1 is under taxonomy-1, and taxonomy-1 has node-2 as an entity reference (though the entity reference module) in one of it's fields.

When view-A appears on Node-1, I'd like view-A to display the entity reference.

How do I do this?

Edit: This is my current set-up. This the input:

And this is the output:

@rooby, thanks for your help, but I don't actually get any results if I add any contextual filter under the sun.

  • Just as a friendly note. Getting upset about response times and bad answers is not the best way to get people to answer the question. I understand it can be frustrating but the people answering on this site are doing so in their own free time and have no obligation to answer anyone's questions at all. If you really need high quality support and you need it fast then paying a company or developer is a better way. On these sorts of forums unfortunately sometimes it just takes time (and even more unfortunately some questions never get answered). That's just the nature of forum sites like this. – rooby Oct 23 '14 at 23:43
  • This is definitely possible as I've done it before (although usually with 2 taxonomy fields or 2 entity reference fields, not one of each, but that should be beside the point). The view sql is sometimes useful in diagnosing these things (you can enable it to show near the preview on the views settings page). Can you post more of your view settings like fields & filters config? If you can upload a file you could also upload a txt file of the view export. Also, have you tried the distinct setting (it's in the right column of settings somewhere)? – rooby Oct 23 '14 at 23:52
  • I'm just bumping it. But I really think people should be called out about bad answers, it buries people's questions and confuses those looking for legitimate answers. Thanks for your help by the way. It's late where I live, and I keep my files on my development computer at work. I'll take a look at the view SQL, is there any hook that I should use to debug it? – Rollo Oct 23 '14 at 23:52
  • I'm in half a mind to grab the Content Nid, get it's taxonomy term, load that, load the entity ref, and then print out fields from there with code. All via code. Considering I've been using drupal for less than a month, I'd probably mess it up majorly. – Rollo Oct 23 '14 at 23:59
  • I'd like to see one of the other answers get improved as I think they're close but in the interest of timeliness I'll take a stab at it. – rooby Oct 23 '14 at 23:59
0

If you're displaying a view on a node page that is returning data relevant to that node then the view needs to know about the node it is dealing with.

To do that add a contextual filter of "Content: Nid".

In views, block views do not automatically inherit contextual filters from the URL so as far as the block knows there is no filter value available. So the way to get it to get the filter value from the url is configure the "When the filter value is NOT available" setting to "Provide default value".

Then under that select "Content ID from URL". Then if you put that block on a node page the view will pull the node ID from the URL.

Additionally you can also check the "Specify validation criteria" checkbox further down in the contextual filter settings. Then select "Content", the node types of the nodes you are putting the block on and select an action if it doesn't pass validation (often in cases like this you want the "Hide view" option so that if it doesn't get a valid node ID of the relevant content type it doesn't try to use it.

If you choose not relationship for this filter then to show fields from the linked node you need to use the relationship for those fields.

Otherwise you can use the relationship for this contextual filter and no relationship for the fields. It's up to you which way you prefer.

EDIT: It is also possible to use a taxonomy term contextual filter in the same manner but for the default value use "Taxonomy term ID from URL" and check "Load default filter from node page, that's good for related taxonomy blocks" and uncheck "Load default filter from term page" and restrict by your vocab.

In this case the contextual filter is doing the work of the relationship from the viewed node to its term, then you should only need one relationship from the linked node to the term.

These types of views can get a bit complex so you may still have to do some tweaking of your relationships and which fields & contextual filters use which relationships depending on your setup.

  • Thanks, this is nearly identical to one of my attempts, but any attempt to filter the view removes all the results. – Rollo Oct 24 '14 at 10:21
0

Taxonomy terms belonging to a node

Hi,

If you construct your view like the below and get the value from URL, you should be able to achieve.

EDIT: Allow me to guide you step by step:

  1. You go to /admin/structure/views
  2. You click on Add new view, and enter your view's name and description in the related fields in the next screen
  3. Choose Content from the Show dropdown and your display format from the Display format dropdown and click Continue & edit.
  4. Add Content: All taxonomy terms as a field, make your settings, and click Apply.
  5. In the advanced section (to the right most section), add Nid as a Contextual Filter and click Apply (all displays).
  6. Choose Provide default value and Content ID from URL, and click Apply (all displays

That's all there is to be done. You can test this by passing a node ID in the Preview with contextual filters field.

  • Please at least read the question. I'd downvote this if I could. – Rollo Oct 23 '14 at 14:17
  • I think this answer might be on the right track but you need to give way more information on what configuration you're talking about. You really can't tell much from that image. – rooby Oct 23 '14 at 23:54
-1

Select Taxonomy term in Contextual filter and then use hook_views_pre_view() to pass that taxonomy term argument to the view.

https://api.drupal.org/api/views/views.api.php/function/hook_views_pre_view/7

  • Hey could you explain more, which argument should I pass it too? I started using drupal 2 weeks ago. – Rollo Oct 23 '14 at 13:12
  • Like what settings should I ascribe that contextual filter? – Rollo Oct 23 '14 at 13:14
  • You select Taxonomy term from the Contextual filter options, look for it there, you'll find it then inside the hook load the node, get the term from the node like $node->field_xyz and then add that term value to the $args array. I can give you code sample later on, if you want. – rashidkhan Oct 23 '14 at 13:28
  • You're not being very specific... What do I set "WHEN THE FILTER VALUE IS NOT AVAILABLE" to? – Rollo Oct 23 '14 at 13:51
  • I don't know why you would need to write custom code to implement a hook to pass in the taxonomy term when the taxonomy term contextual filter already can do that via the UI. – rooby Oct 23 '14 at 23:55

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.