I have a "Views Related Items Block" problem which I thought was simple but have stopped me dead in my tracks for the last 2 weeks. I have Googled lots and lots of "Drupal Views Related" to no avail and hope someone will be kind enough to help me out.

Here is a sample content node:

TITLE: ABC123 (simple text string)

SHAPE: Square (stored as a taxonomy vocabulary called "Shape" with possible terms of "Square / Circle / Triangle")

COLOR: Red (stored as a taxonomy vocabulary called "Color" with possible terms of "Red / Green / Blue")

I have successfully created a VIEW PAGE configured to display the details of a product item at the URL:


where "square" (as a taxonomy filter) is the shape of the node item and "abc123" is the title of the node.

I would like to create a related items block on this page that will display all items that have the same SHAPE AND COLOR as item "ABC123" (square and red in this case).

I've played with MANY different configurations of "Content: Has taxonomy term ID / Load default filter from node page, that's good for related taxonomy blocks". But it seems like I can only successfully filter for "shape" and not "color". I just don't seem to be able to extract "color" given the above URL path.

I am open to using PHP code filter or even pre_execute() SQL modification, but I have zero experience in doing that in Drupal.

Any help will be most appreciated!



  • Can you post the code of your current view, the one that displays mysite.com/products/square/abc123?
    – berliner
    Feb 11 '14 at 0:09
  • Thanks for trying to help! I finally got it kind of working and have answered my own question. Thanks again!
    – Andrew
    Feb 11 '14 at 13:09

Okay, I spent another day working on this and finally kind of got it working.

The goal here is to create contextual taxonomy filters that will retrieve all products with the same shape AND color as item "ABC123" as displayed on the browser URL, where item ABC123 happens to have a shape of "square" and color of "red".

Getting "square" into the taxonomy filter is easy,

  1. create a view block
  2. add a contextual filter of type "Content: Has taxonomy term ID"
  3. since this is a block with no path, we need to give it a default value, do that and select "Taxonomy ID from URL" and "Load default filter from node page, that's good for related taxonomy blocks"
  4. select the vocabulary you want (in this case "shape")

Now comes the hard part, we need to filter for "color" of item ABC123, but it is not on the browser URL in any shape or form. We need to somehow get the node contents of item ABC123, look into its "color" field, and then scan for all products with that color.

  1. create a 2nd contextual filter of type "Content: Has taxonomy term ID"
  2. click "Provide default value" and select PHP code

    $title = arg(2, drupal_get_path_alias()); // arg #2 is title of item on URL
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'product'); // product is the machine name of my content type
    ->propertyCondition('status', 1)
    ->propertyCondition('title', $title);
    // title is a node property, for other fields use ->fieldCondition

    $result = $query->execute();

    if (isset($result['node'])) {
    $items_nids = array_keys($result['node']);
    $items = entity_load('node', $items_nids);

    $first_item = key($items);

    $item = node_load($first_item);

    $color = field_get_items('node', $item, 'field_color');
    return ($color[0]['tid']);

For "Views Related Items Block", we also need to filter out ABC123, but that is an exercise left for another day.

Please note that this is the very first time I dabble into Drupal API calls, and the code above is clobbered together from various places on line. The code above make some assumptions (e.g. title is unique), is not efficient, may or may not handle translations and may not be totally secure. Reference at your own risk!

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.