Creating a block that shows related nodes based on a vocabulary...

Events happening in the community are now at Drupal community events on www.drupal.org.
yurtboy's picture

This was tough but a post on the web made it possible.
If looking at a node I want to find all other nodes related via a like tag.
So if node90 has tag test then when you are looking at it you should see node100 if it also has tag test.
Here is the php code in the argument Taxonomy Term ID

$node = node_load(arg(1));
if($node){
foreach($node->taxonomy as $term){
/* test if in tag vocab */
if($term->vid == '2'){
$status = 'TRUE';
$terms[] = $term->tid;
}
}
if($status == 'TRUE'){
  return implode('+', $terms);
} else { return; }
} else {
return; }

Here is an export of the view

$view = new view;
$view->name = 'relatedbytag';
$view->description = 'related kb items by tag';
$view->tag = 'KB';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 3.0-alpha1;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially /

/
Display: Defaults /
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->display->display_options['access']['type'] = 'none';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/
Field: Node: Title /
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['label'] = '';
$handler->display->display_options['fields']['title']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['title']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['title']['alter']['trim'] = 0;
$handler->display->display_options['fields']['title']['alter']['word_boundary'] = 1;
$handler->display->display_options['fields']['title']['alter']['ellipsis'] = 1;
$handler->display->display_options['fields']['title']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['title']['alter']['html'] = 0;
$handler->display->display_options['fields']['title']['hide_empty'] = 0;
$handler->display->display_options['fields']['title']['empty_zero'] = 0;
$handler->display->display_options['fields']['title']['link_to_node'] = 1;
/
Argument: Taxonomy: Term ID /
$handler->display->display_options['arguments']['tid']['id'] = 'tid';
$handler->display->display_options['arguments']['tid']['table'] = 'term_node';
$handler->display->display_options['arguments']['tid']['field'] = 'tid';
$handler->display->display_options['arguments']['tid']['default_action'] = 'default';
$handler->display->display_options['arguments']['tid']['style_plugin'] = 'default_summary';
$handler->display->display_options['arguments']['tid']['default_argument_type'] = 'php';
$handler->display->display_options['arguments']['tid']['default_argument_options']['code'] = '$node = node_load(arg(1));
if($node){
foreach($node->taxonomy as $term){
/
test if in tag vocab /
if($term->vid == \'2\'){
$status = \'TRUE\';
$terms[] = $term->tid;
}
}
if($status == \'TRUE\'){
  return implode(\'+\', $terms);
} else { return; }
} else {
return; }';
$handler->display->display_options['arguments']['tid']['break_phrase'] = 1;
$handler->display->display_options['arguments']['tid']['add_table'] = 0;
$handler->display->display_options['arguments']['tid']['require_value'] = 0;
$handler->display->display_options['arguments']['tid']['reduce_duplicates'] = 1;
$handler->display->display_options['arguments']['tid']['set_breadcrumb'] = 0;
/
Argument: Node: Nid /
$handler->display->display_options['arguments']['nid']['id'] = 'nid';
$handler->display->display_options['arguments']['nid']['table'] = 'node';
$handler->display->display_options['arguments']['nid']['field'] = 'nid';
$handler->display->display_options['arguments']['nid']['default_action'] = 'default';
$handler->display->display_options['arguments']['nid']['style_plugin'] = 'default_summary';
$handler->display->display_options['arguments']['nid']['default_argument_type'] = 'node';
$handler->display->display_options['arguments']['nid']['break_phrase'] = 0;
$handler->display->display_options['arguments']['nid']['not'] = 1;
/
Filter: Node: Type /
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
  'knowledgebase' => 'knowledgebase',
);
$handler->display->display_options['filters']['type']['expose']['operator'] = FALSE;

/
Display: Block */
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->display->display_options['block_description'] = 'Related By Tag v2';

Comments

The title is confusing

venusrising's picture

This will produce a block that lists nodes by term and vocabulary NOT just a block of nodes by vocabulary. Meaning if you want a block that shows all nodes (or say recent 10) by Vocabulary Fruit you would need to be on the same term page like Banana to show the related nodes. This would not produce all nodes under Fruit.

Thanks a lot

sachinsharma18's picture

Thank u so much for this wonderful code, i was in great need of this. for wonders in world.

Wonders How did You Use it?

venusrising's picture

Just wondering how you used it.

Thanks!

RedTop's picture

I found a similar snippet somewhere else but that didn't work flawlessly as I was getting false positives in certain instances.

Thanks heaps, I spent half a day fixing those errors. You provided the solution! :)

Venusrising: I'm using this to create a website based on (rather complex) linking using taxonomy, rather than menus. Menus are very rigid and to get pages in multiple places one would have to duplicate those pages. With taxonomy you can have a single page dynamically appear in different places.

I'm using this more dynamic approach to circumvent organisational silos and account for overlap in tasks etc between business units. People who've seen it love it!

Dynamic when you can

venusrising's picture

RedTop : Yes it is always great to be bale to generate dynamic content when you can. Moving away from rigid menus really does add a great deal of life to a site. I also like the idea that the changes follow you instead of the other way around so we use a bit of similar code to produce blocks of related nodes for further reading.

Western Massachusetts

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: