SOLVED: Views arguments & Taxonomy Term--nearly solved

streever - May 1, 2009 - 15:39

update: The code posted by WorldFallz does exactly what is needed.

I think I'm awfully close.

I have a page of content. That content is tagged with a tax. term. I want to display relevant content (i.e. any content also tagged with that term) in the sidebar.
Here is where it gets tricky.

I want this automated. So I want ONE view for all pages. I guess I could make 16 views, one for each Taxonomy term, but what if the client later adds another tax term? We have to add a new view. So that seems a bit too messy.

So, this view has to say, "What is the taxonomy term of the content block?" and then display a corresponding one.

I think this is the code I need:

$wanted_vocab = 1; // Change to the vocabulary id of the one you are using
foreach ( (array)$node->taxonomy as $term ) {
if ( $term->vid == $wanted_vocab ) {
return($term->tid);
}
}

Can anyone give me exact steps on using this? Do I paste it into the Arguments field? I don't think that will work. Arguments relies on the URL, which does not include my taxonomy term, as these pages are not reached via a taxonomy menu.

Does this make sense? Am I not providing enough information?

I've watched & read all available Views 2 documentation but none of it seems to address this. Most use the default features of Views, which is the issue :) I can do those functions easily, because they are pre-built--filtering by date, sorting, etc--this is not at all hard.

Does anyone have any advice?

My main skill set is in CSS/xHtml and I am happy to trade help on issues related to that with a clever programmer. Thank you.

_

WorldFallz - May 1, 2009 - 16:06

I just recently created a "related content" block like you describe. Here's the views export:

$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);

_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.

Thank you so much--I think

streever - May 1, 2009 - 16:14

Thank you so much--I think arguments won't work because my URL path doesn't include the taxonomy term though! If so I need to approach this with an entirely different module--is that correct?

_

WorldFallz - May 1, 2009 - 16:18

Nope-- it should work. Block views don't have a url to begin with-- which is why you need to add argument handling code or choose one of the 'provide default' argument options in the view. This block displays on any node page and uses the node id of the page to load the taxonomy terms of the node and query for nodes with the same terms.

_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.

Thank you again--it does pull

streever - May 1, 2009 - 16:37

Thank you again--it does pull in related content.

However, the content I want to pull in is contact information for the department whose page you are on. So, the user enters it into a different content type, with CCK fields. That way user only enters departmental contact info one time, even if they have 10 pages that all display it. Does that make sense?

This pulls in related content but only of the same content type. I thought that was due to the node type argument, so I deleted that, but that simply breaks it.

I also tried using a filter to have it only select the node type that I want to display, but this also causes nothing to display.

I'm sorry! Thank you for all of the help you have given me so far. I greatly appreciate it, and if I'm making this just too complicated, let me know. If there is a simpler way to do this I would gladly do it that way.

_

WorldFallz - May 1, 2009 - 18:02

You don't say if you imported the view or used it to create your own. But you should definitely be able to edit it to produce what you're looking for. I'm only grabbing the node title and post date, but you should be able to get any fields on the content type you wish. And yes, you should just be able to remove the node type argument and replace it with a filter if you like. Try clearing the cache (admin/settings/performance and on the Views "tools" tab).

If that doesn't work post your view and i'll take a look.

_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.

This is so helpful. I really

streever - May 1, 2009 - 18:27

This is so helpful. I really appreciate it.

Using just what you gave me (I imported it) it displays related content of the same content type. However, it doesn't display the content with same taxonomy id that uses a DIFFERENT content type.

So, I added content type: departmental contact page as a filter, which then returns nothing: and I have the code below now.

So I think I just need a line that tells it to look at nodes which are part of Department Contact Information?

$view = new view;
$view->name = 'related_content_departmental';
$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('filters', array(
'type' => array(
'operator' => 'in',
'value' => array(
'department_contact' => 'department_contact',
),
'group' => '0',
'exposed' => FALSE,
'expose' => array(
'operator' => FALSE,
'label' => '',
),
'id' => 'type',
'table' => 'node',
'field' => 'type',
'relationship' => 'none',
),
));
$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);

Oh, I figured it out. I don't

streever - May 4, 2009 - 18:22

Oh, I figured it out. I don't need the filter at all. I just have to remove Content Type from the arguments.

THANK YOU! You are truly my drupal hero.

I'll share this code with anyone looking for it.

 
 

Drupal is a registered trademark of Dries Buytaert.