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.
Comments
_
I just recently created a "related content" block like you describe. Here's the views export:
Thank you so much--I think
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?
I provide free Drupal support on Thursdays: booking calendar coming soon.
_
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.
Thank you again--it does pull
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.
I provide free Drupal support on Thursdays: booking calendar coming soon.
_
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.
This is so helpful. I really
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);
I provide free Drupal support on Thursdays: booking calendar coming soon.
Oh, I figured it out. I don't
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.
I provide free Drupal support on Thursdays: booking calendar coming soon.
Multiple Terms?
WorldFallz,
Thanks for the code snippet. It made my day when I got it working beautifully matching blog nodes and image nodes (displayed in a views carousel block)! However, it does not appear to work when a node has multiple terms. I entered the term IDs into the view preview arguments field in the format (1+2), and it returned matching image nodes of both terms. It did not return the same results when I entered the term IDs separated by commas (1,2). Given this, I imagine it had something to do with how the php returned the term IDs of the blog node being viewed.
So I googled "php return implode", then replaced the the comma glue parameter with "+" in the php in the default argument of Taxonomy: Term ID, and preso, got it working!
Thanks for the code again!
jwant
jwant
_
EXCELLENT-- I'm glad it worked for you! ;-)
Adapt this for Taxonomy Term
Is it possible to adapt this code to use with the taxonomy term argument instead of taxonomy term id?
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node=node_load(arg(1));
return implode('+', array_keys($node->taxonomy));
} else {
return FALSE;
}
When combining this code in a related content view block with a read more link linking to the view page, I would like to use path mysite.com/news/% for example. Instead of term ID completing the path, I would like to use the term.
Any ideas?
Thanks,
jwant
jwant
Sumo-fighter of a query... takes 3.5 secs + 3.5 secs on pager
I'm using almost the same code to pull nodes related by a taxonomy term, and unfortunately joining multiple values with a + produces a very inefficient query:
Query took 3.4941 sec. Considering there's a pager query that takes about the same time to run, that's 7 seconds on just one view!
It's mostly the IN() that is IN-efficient (a mysql pun, ouch!)
The more related terms there are, the slower the query goes, which makes this a very deceiving solution. It will work at first when you have one or two terms, but it will not scale well at all (we've got 16 terms on this particular one).
Replacing IN() with a subquery reduces the execution time significantly:
Query took 0.8106 sec
Not exactly a dandelion puff of a query, but significantly better.
I can't seem to figure out how to get this done through provided arguments. Might have to write a custom one just for this.
I've got a good tutorial on writing Views 2 filters, but I haven't written custom arguments yet.
Ideas?
Best,
Andrey.
I just wanted to say THANK
I just wanted to say THANK YOU worldfallz!! I just stumbled across this post and it worked perfectly for me! THANK YOU THANK YOU!