[contextual view] using node taxonomy term as default argument for a block view..

Cosmy - August 22, 2008 - 20:34

Hello.
I have a special node type with an associated taxonomy term.
i have also made a block view that accepts taxonomy term as argument to display news related to this term.
I want to make this view context sensitive, so i have to pass the node term to the view.
I've thought to use php as default argument type in the "Action to take if argument is not present:" part of the view ui editor, but i don't know how to catch the taxonomy term there..
Have you got any idea?

Same question here

Stef01 - December 13, 2008 - 22:16

Same question here:
- users have to select a taxonomy term in a content type
- I created a views2 block that shows the content in a block
- I want the block to be shown only on nodes that have the same taxonomy term as the term related to the content in the block

Looking at some examples in the Snippets I think it can be done with adding PHP code in the views header, but I didn't get it right yet.

Thanks.

I'm in the same boat. Can't

streever - April 30, 2009 - 17:32

I'm in the same boat. Can't find anything... and keep getting pointed to a video showing a very different goal. I feel like I'm missing something.

some help

streever - May 4, 2009 - 11:48

Hi all,
here is a spot of code given to me by WordFallz. Import it to Drupal Views. It will display nodes that are related, but in my case, it's not working because I want to display a different content type :)! So I'm working on that, but this should work for a lot of other people.

I hope it all helps, & all thanks to WorldFallz for first giving it to me.

$view = new view;
$view->name = 'related_nodes';
$view->description = '';
$view->tag = '';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('fields', array(
  'title' => array(
    'label' => '',
    'link_to_node' => 1,
    'exclude' => 0,
    'id' => 'title',
    'table' => 'node',
    'field' => 'title',
    'override' => array(
      'button' => 'Override',
    ),
    'relationship' => 'none',
  ),
  'created' => array(
    'label' => '',
    'date_format' => 'time ago',
    'custom_date_format' => '',
    'exclude' => 0,
    'id' => 'created',
    'table' => 'node',
    'field' => 'created',
    'override' => array(
      'button' => 'Override',
    ),
    'relationship' => 'none',
  ),
));
$handler->override_option('sorts', array(
  'created' => array(
    'order' => 'DESC',
    'granularity' => 'second',
    'id' => 'created',
    'table' => 'node',
    'field' => 'created',
    'override' => array(
      'button' => 'Override',
    ),
    'relationship' => 'none',
  ),
));
$handler->override_option('arguments', array(
  'type' => array(
    'default_action' => 'default',
    'style_plugin' => 'default_summary',
    'style_options' => array(),
    'wildcard' => 'all',
    'wildcard_substitution' => 'All',
    'title' => '',
    'default_argument_type' => 'php',
    'default_argument' => '',
    'validate_type' => 'none',
    'validate_fail' => 'not found',
    'id' => 'type',
    'table' => 'node',
    'field' => 'type',
    'override' => array(
      'button' => 'Override',
    ),
    'relationship' => 'none',
    'default_options_div_prefix' => '',
    'default_argument_user' => 0,
    'default_argument_fixed' => '',
    'default_argument_php' => 'if (arg(0) == \'node\' && is_numeric(arg(1))) {
      $node = node_load(arg(1));
      return $node->type;
    } else {
      return FALSE;
    }',
    'validate_argument_node_type' => array(
      'webform' => 0,
      'blog' => 0,
      'poll' => 0,
      'deadwood_category' => 0,
      'deadwood_item' => 0,
      'forum' => 0,
      'project_project' => 0,
      'project_issue' => 0,
      'panel' => 0,
      'book' => 0,
      'document' => 0,
      'feedtest' => 0,
      'feedtest2' => 0,
      'gallery' => 0,
      'image' => 0,
      'location_type' => 0,
      'page' => 0,
      'profile' => 0,
      'story' => 0,
      'test' => 0,
    ),
    'validate_argument_node_access' => 0,
    'validate_argument_nid_type' => 'nid',
    'validate_argument_vocabulary' => array(
      '1' => 0,
      '6' => 0,
      '5' => 0,
      '3' => 0,
      '2' => 0,
      '4' => 0,
    ),
    'validate_argument_type' => 'tid',
    'validate_argument_project_term_vocabulary' => array(
      '6' => 0,
    ),
    'validate_argument_project_term_argument_type' => 'tid',
    'validate_argument_project_term_argument_action_top_without' => 'pass',
    'validate_argument_project_term_argument_action_top_with' => 'pass',
    'validate_argument_project_term_argument_action_child' => 'pass',
    'validate_argument_php' => '',
  ),
  'nid' => array(
    'default_action' => 'default',
    'style_plugin' => 'default_summary',
    'style_options' => array(),
    'wildcard' => 'all',
    'wildcard_substitution' => 'All',
    'title' => '',
    'default_argument_type' => 'node',
    'default_argument' => '',
    'validate_type' => 'none',
    'validate_fail' => 'not found',
    'break_phrase' => 0,
    'not' => 1,
    'id' => 'nid',
    'table' => 'node',
    'field' => 'nid',
    'relationship' => 'none',
    'default_options_div_prefix' => '',
    'default_argument_user' => 0,
    'default_argument_fixed' => '',
    'default_argument_php' => 'if (arg(0) == \'node\' && is_numeric(arg(1))){
      return arg(1);
    } else {
      return FALSE;
    }',
    'validate_argument_node_type' => array(
      'webform' => 0,
      'blog' => 0,
      'poll' => 0,
      'deadwood_category' => 0,
      'deadwood_item' => 0,
      'forum' => 0,
      'project_project' => 0,
      'project_issue' => 0,
      'panel' => 0,
      'book' => 0,
      'document' => 0,
      'feedtest' => 0,
      'feedtest2' => 0,
      'gallery' => 0,
      'image' => 0,
      'location_type' => 0,
      'page' => 0,
      'profile' => 0,
      'story' => 0,
      'test' => 0,
    ),
    'validate_argument_node_access' => 0,
    'validate_argument_nid_type' => 'nid',
    'validate_argument_vocabulary' => array(
      '1' => 0,
      '6' => 0,
      '5' => 0,
      '3' => 0,
      '2' => 0,
      '4' => 0,
    ),
    'validate_argument_type' => 'tid',
    'validate_argument_project_term_vocabulary' => array(
      '6' => 0,
    ),
    'validate_argument_project_term_argument_type' => 'tid',
    'validate_argument_project_term_argument_action_top_without' => 'pass',
    'validate_argument_project_term_argument_action_top_with' => 'pass',
    'validate_argument_project_term_argument_action_child' => 'pass',
    'validate_argument_php' => '',
    'override' => array(
      'button' => 'Override',
    ),
  ),
  'tid' => array(
    'default_action' => 'default',
    'style_plugin' => 'default_summary',
    'style_options' => array(),
    'wildcard' => 'all',
    'wildcard_substitution' => 'All',
    'title' => '',
    'default_argument_type' => 'php',
    'default_argument' => '',
    'validate_type' => 'none',
    'validate_fail' => 'not found',
    'break_phrase' => 1,
    'add_table' => 0,
    'require_value' => 0,
    'reduce_duplicates' => 0,
    'set_breadcrumb' => 0,
    'id' => 'tid',
    'table' => 'term_node',
    'field' => 'tid',
    'relationship' => 'none',
    'default_options_div_prefix' => '',
    'default_argument_user' => 0,
    'default_argument_fixed' => '',
    'default_argument_php' => 'if (arg(0) == \'node\' && is_numeric(arg(1))) {
      $node=node_load(arg(1));
      return implode(\',\', array_keys($node->taxonomy));
    } else {
      return FALSE;
    }',
    'validate_argument_node_type' => array(
      'webform' => 0,
      'blog' => 0,
      'poll' => 0,
      'deadwood_category' => 0,
      'deadwood_item' => 0,
      'forum' => 0,
      'project_project' => 0,
      'project_issue' => 0,
      'panel' => 0,
      'book' => 0,
      'document' => 0,
      'feedtest' => 0,
      'feedtest2' => 0,
      'gallery' => 0,
      'image' => 0,
      'location_type' => 0,
      'page' => 0,
      'profile' => 0,
      'story' => 0,
      'test' => 0,
    ),
    'validate_argument_node_access' => 0,
    'validate_argument_nid_type' => 'nid',
    'validate_argument_vocabulary' => array(
      '1' => 0,
      '6' => 0,
      '5' => 0,
      '3' => 0,
      '2' => 0,
      '4' => 0,
    ),
    'validate_argument_type' => 'tid',
    'validate_argument_project_term_vocabulary' => array(
      '6' => 0,
    ),
    'validate_argument_project_term_argument_type' => 'tid',
    'validate_argument_project_term_argument_action_top_without' => 'pass',
    'validate_argument_project_term_argument_action_top_with' => 'pass',
    'validate_argument_project_term_argument_action_child' => 'pass',
    'validate_argument_php' => '',
  ),
));
$handler->override_option('access', array(
  'type' => 'none',
));
$handler->override_option('style_plugin', 'list');
$handler->override_option('style_options', array(
  'type' => 'ul',
));
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->override_option('block_description', '');
$handler->override_option('block_caching', -1);

Hi streever, Did you find the

sej123 - June 3, 2009 - 10:58

Hi streever,

Did you find the solution for this.

I am also looking for the solution to display content that share same taxonomy term as of the current node, but in my case I have 2 content types associated with different taxonomies but has same term names...

Can you please guide me.

Hi Cosmy, yes, definitely.

streever - June 5, 2009 - 22:05

Hi Cosmy,

yes, definitely. Create a view by importing the above.

That should allow you to display any content tagged with a taxo term. It worked perfectly for me.

I had to remove the content type from the arguments--just deleted it entirely.

I modified the above a bit to

mr.andrey - August 5, 2009 - 19:56

I modified the above a bit to suit my needs, so I can specify from which vocabulary I want to pull the related nodes.

Step-by-step:
* Create the view with whatever fields you need
* Add the following arguments (in this order):
> Node: Nid
Action to take if argument is not present: Display empty text
Exclude the argument
> Taxonomy: Vocabulary ID
Action to take if argument is not present: Display empty text
> Taxonomy: Term ID
Action to take if argument is not present: Provide default argument: PHP Code:

<?php
// arg(0) is "node", arg(1) is nid, arg(2) is the vocab id
if (is_numeric(arg(1))) {
 
$node=node_load(arg(1));
  return
implode('+', array_keys($node->taxonomy));
} else {
  return
false;
}
?>

Allow multiple terms per argument

Now you can embed this view on a node page and specify the vocab specifically:

<?php
views_embed_view
('video_type', 'default', $node->nid, 1); // 1 here is the vocab id
?>

Would it be possible to set

trupal218 - August 28, 2009 - 15:00

Would it be possible to set the node to not the current node but the current user's content profile node (which would be a different node from where the View is located)?

I would appreciate any support in getting this view to work.
Thank you very much in advance!

Yes-- you need to use a php function

emdalton - September 3, 2009 - 00:16

You'll need to use a PHP function to get the nid of the user's content profile node. I think you might want profile_view_field for this. Check here: http://api.drupal.org/api/function/profile_view_field/6 -- I don't know if you can get the nid that way, but it seems likely.

Just what I need! But how do I test it?

emdalton - September 3, 2009 - 00:29

Ok, I followed these directions to modify a view I already had, attempting to pass in the taxonomy value. Ultimately I will want to do this within a block, but the PHP code above should work fine there. However, I'm not getting anything back from the call. If I understand correctly, I should be able to test this within the Views editor by pasting (from your example)

'default'/$node->nid/1

Except I would need to substitute some actual nid, e.g. 100, for $node->nid, right? I've tried that with a page that has some appropriate taxonomy terms such that the view should be returning several lines, and the view runs (I see the query), but I'm not getting anything back. The relevant bit of the query looks like this:

(node.nid != 0) AND (vocabulary.vid = '11') AND (term_node_value_0.tid = 18098))

Any suggestions? The vid looks ok, but shouldn't that term_node_value_0.tid be a term ID, not a node ID? That value (18098) is what I'm putting in where you said to put $node->nid.

Never mind testing, PHP problem?

emdalton - September 3, 2009 - 13:25

I figured out what I was doing wrong with the testing (don't include 'default' in the views preview, for one thing) but after tinkering around for a while, I have something that I really think should work, but doesn't. I'm going to open an issue in views.

moved to the end of the

etcetera9 - October 28, 2009 - 15:59

moved to the end of the page...

This worked for me

emdalton - September 3, 2009 - 18:27

In the view, accept one argument, Taxonomy: Term ID. Set up the rest of your view as you please, and test using the preview mode by passing it a tid you think should work.

Then, in your page or block, use code like this:

<?php
$node
= node_load(arg(1));
if ((
arg(0) == 'node') && is_numeric(arg(1))) {
  
$taxes = taxonomy_node_get_terms_by_vocabulary($node, 11);
  
$all_taxes = (implode('+', array_keys($node->taxonomy)));
   print
views_embed_view(your_view_name', 'default_or_display_name, $all_taxes);
}
?>

Disadvantage: you will get an empty view display if the view contains no content. I'm working on code for that now, as I want to use this in a block that will display on a great many pages, not all of which will have content matching the page terms.

Did you crack it?

chrism2671 - October 21, 2009 - 17:47

Did you crack it?

Hey there, I just used the

etcetera9 - October 28, 2009 - 15:59

Hey there, I just used the code on the comment above (http://drupal.org/comment/reply/298810/1892988#comment-1892988), I mean this code:

<?php
// arg(0) is "node", arg(1) is nid, arg(2) is the vocab id
if (is_numeric(arg(1))) {
 
$node=node_load(arg(1));
  return
implode('+', array_keys($node->taxonomy));
} else {
  return
false;
}
?>

I just want to use the Views block on a sidebar, not to embed it. How can I select a vocabulary to take the term ID? I want to take terms from a specific vocabulary.

Also if I select "Allow multiple terms per argument.", it throws a warning message:

warning: Invalid argument supplied for foreach() in /home/relaxinb/public_html/tributespaid/modules/views/includes/handlers.inc on line 807.

 
 

Drupal is a registered trademark of Dries Buytaert.