Community Documentation

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

Last updated July 15, 2010. Created by yurtboy on July 15, 2010.
Log in to edit this page.

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

Working!

Thanks for this great PHP snippet - it worked for me "out of the box" after changing if($term->vid == '2'){ to the VID of my vocabulary!

Alternate Solution - Similar Terms module

I didn't have much luck using the snippet, but found this project - http://drupal.org/project/similarterms - which did exactly what this snippet should do, and gives me a view to do further configuration.

Similar By Terms 6.x-2.x

Thanks for the pointer; I tried Similar By Terms 6.x-2.0-rc1, and it's pretty powerful and working well. Indeed it might be a full replacement for this code snippet. It definitely is easier to use and offers significantly more flexibility. However, there is one major drawback: You have less control over the views/page/block title (at least I havn't found a way to mimic this snippets title handling with 'Similar by Terms').

Similar by Terms is far easy and effective too!

The similar by term module seem to be far easy and yet quite handy to use too.. I could achieve my solution for a related video block in minutes!!!

eV@gabond

Nice documentation. i have

Nice documentation. i have been searching for two days for some solution like this.
Thanks man.

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 6.x
Audience
Programmers
Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.