I have a single article (page) which has an attached view thats displays a load of nodes (in a content called 'courses').

These nodes have a term reference field called 'language' which assigns them to a specific language the course is taught in.

Each of these nodes have an attached view which lists a load of related nodes (in this case 'products'), and these are also assigned with the same taxonomy called 'language'.

What i am attempting to do is to list these nodes, with the attached view filtered by the same taxonomy 'language'.

I've attempted to set up the view using contextual filters with the notion of getting the parent nodes id (When filter is not available use taxonomy term id from URL) but this only gets me the id of the top level node. What i'm looking to get is to pass the language tid from each course to its attached view.

I've used hook_views_query_alter to add on the join table for the languages table which i'd like to then fill with the tid from the passed taxonomy term to do the filtering.

Anyone done such a thing? seems pretty straightforward as it feels like i'm doing something in the wrong order (ie its rendering the view before attaching to the node).

  • How do you attach the product view to a course node? Commented Feb 25, 2016 at 19:44
  • A views setup through the UI only will probably not get you there.
    – dmsmidt
    Commented Feb 26, 2016 at 10:41
  • @StefanosPetrakis I'm building my views via Display Suite. My courses node have a dynamic field which I attach my products view to. The view which lists the courses is attached to the parent article in the same means. Commented Feb 26, 2016 at 20:23

2 Answers 2


I would pursue the hook_views_query_alter() way, and using arg(1) and node_load() to load the node you are viewing and get the 'language' value from the node field. Add that value as a filter for your view programmatically.

Is this enough for you to continue on your own?

  • I have successfully set up my hook_views_query_alter() and passed it the value for the language into the query conditions, so that bit works. I have an additional filter i'm trying to pass which is specific to each course, and defines a subset of products which is why i'm trying to get this specific to each course rather that the parent article. Essentially, ARTICLES(with language) hasMany COURSES (with language, lesson_type) COURSES hasMany PRODUCTS (with language, lesson_type). I'm wonder whether TEMPLATE_preprocess_field() may help here. Commented Feb 26, 2016 at 21:29
  • Depending on your design you can create one view of your products and use a 'group by' 'lesson_type'. But if you need to show more info about the courses you still need two views. The products view needs a contextual filter for 'lesson_type'. Then you need to find a way to pass the 'lesson_type' argument to your products view. Doesn't you 'dynamic field' on the course node, which embeds the view, have an option for that? What module do you use for that? If my answer helped your initial question please accept it and create another question for the rest.
    – dmsmidt
    Commented Feb 29, 2016 at 8:32

Here is my take on this. I am not using the hook_views_query_alter approach and therefore I find @dmsmidt's answer to be closer to the original question. However, this answer solves the problem and avoids using an additional custom module.

Here are the key points of my approach:

  • Existing setup: A view is attached to node via DS Dynamic Field and Views content panes. This setup allows to pass contextual arguments to the view. Unfortunately it does not allow to pass the specific value of the "term reference" field that the parent Course node has, to use as filter for the "children" Product nodes. There is a workaround for that from inside the View's configuration (see below).
  • View's contextual arguments: The attached view is listing Products and has two contextual arguments:
    • Nid (this is expected to be the parent Course, and will be used to display only the Products that have a relation (entity reference) to the parent Course node).
    • Term (this is the language term, set on Courses and Parents, we want to display only the Products that have the same language term as the given parent Course node).
  • DS Dynamic's field configuration: The dynamic field is configured to pass only the current Node's NID as a value to the first contextual argument; the Content: Language Term parameter must be set to "No context"
  • Workaround: The view's second contextual filter is configured for the case "When the filter value is NOT available" and it uses PHP Code in the following way:
    1. Retrieves the NID passed from the Dynamic Field's configuration.
    2. Loads the node and retrieves the value of the node's language term TID
    3. Finaly, it returns that value to be used as the language term id

The php code that does the trick is:

$n = node_load($view->args[0]);
if( isset($n->field_LANGUAGE_TERM_REFERENCE[LANGUAGE_NONE]) && isset($n->field_LANGUAGE_TERM_REFERENCE[LANGUAGE_NONE][0]['tid']) ) {
  return $n->field_LANGUAGE_TERM_REFERENCE[LANGUAGE_NONE][0]['tid'];

Node's language property: A tiny remark about your general design: Had you used the core language functionality of drupal, e.g. with Content Translation, you wouldn't even need the PHP workaround inside views, the Course's language is available as an contextual argument parameter in the Dynamic Field's configuration. Then the condition Course:language == Product:language would have been a lot easier to configure in the view.

Finally, a screenshot that shows a view of all Articles (Courses) and their related Pages (Products) filtered by the parent's Course language:

enter image description here

  • Thanks for your answer. A note about my 'language' taxonomy - its not to switch the language of the page (ie content translation for multilingual sites) but rather to tag a page to a specific language taught. I'd not been clear that this site is for a language school and they teach their courses in 10 different languages. Your php code snippet looks like that should help with my filter. i'll give that a go and report back. Commented Mar 1, 2016 at 11:21
  • Hey there @brightstorm. I get your choice for a taxonomy now, it makes total sense. Let me know if this finally worked for you, i am curious. Cheers Commented Mar 2, 2016 at 10:20

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.