Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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!!!
www.evagabond.me
Nice documentation. i have
Nice documentation. i have been searching for two days for some solution like this.
Thanks man.
How to add a related tag pages on other tag pages
Hi, This is Keshav Solanki, I have a website . I have added the City names in Taxonomy term and i want to show links of 1 city to other city. The links are not a node but a taxonomy term and there is no module i find to do it.
Any guidance please?
Thanks and Regards,