跳转到主要内容
ThomasHuang 提交于 12 September 2012

原文地址:http://drupal.org/node/855476

如果查看一个节点的时候,我想根据tag(标签)查找所有其他相关节点。

如果node90有一个test标签,当node100也有test这个标签时,当你查看node90时你可以看到node100。

以下是分类词汇ID参数的PHP代码

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

以下是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';