i am trying to set up a block to display related content using drupal 7. So depending on the node you are viewing it should display similar content related to that node by taxonomy, similar to how youtube has it when viewing a video.

currently i tried to set the default argument by inserting exactly this code:

$node=node_load(arg(1));
if($node){
foreach($node->taxonomy as $term){$terms[]=$term->tid;}
return implode('+',$terms);
}else {return;}

but alas the block does not show up. instead i get the following error:

Notice: Undefined property: stdClass::$taxonomy in eval() (line 3 of C:\xampp\htdocs\cxcplus2\sites\all\modules\views\plugins\views_plugin_argument_default_php.inc(51) : eval()'d code).
Warning: Invalid argument supplied for foreach() in eval() (line 3 of C:\xampp\htdocs\cxcplus2\sites\all\modules\views\plugins\views_plugin_argument_default_php.inc(51) : eval()'d code).
Notice: Undefined variable: terms in eval() (line 4 of C:\xampp\htdocs\cxcplus2\sites\all\modules\views\plugins\views_plugin_argument_default_php.inc(51) : eval()'d code).
Warning: implode() [function.implode]: Invalid arguments passed in eval() (line 4 of C:\xampp\htdocs\cxcplus2\sites\all\modules\views\plugins\views_plugin_argument_default_php.inc(51) : eval()'d code).

Comments

Damani’s picture

So I got help for this. You can refer to #1033414: Looked forever for how to use a TID as an argument for the solution.

The solution is to Choose Term ID as the argument. Choose Provide default argument then and enter:

$node = node_load(arg(1));
if($node) {
foreach($node->field_activity_type[LANGUAGE_NONE] as $term) { $terms[] = $term['tid']; }
return implode('+',$terms);
}
else { return; }

What you want to replace is the $node->field_activity_type with your field name that you created for your "Tags" vocabulary. Go to the Content Type that "Tags" appears in, click on manage fields and you should see the name of the field there.

Hope this helps!

Damani’s picture

Damani’s picture

Status: Active » Closed (fixed)
borazslo’s picture

i will try it

bryancasler’s picture

nordicmaterial’s picture

Solution #1 gives a very similar error :

Warning: Invalid argument supplied for foreach() in eval() (line 3 of /Applications/drupal-7C/apps/drupal/htdocs/sites/all/modules/views/plugins/views_plugin_argument_default_php.inc(49) : eval()'d code).
Notice: Undefined variable: terms in eval() (line 4 of /Applications/drupal-7C/apps/drupal/htdocs/sites/all/modules/views/plugins/views_plugin_argument_default_php.inc(49) : eval()'d code).
Warning: implode() [function.implode]: Invalid arguments passed in eval() (line 4 of /Applications/drupal-7C/apps/drupal/htdocs/sites/all/modules/views/plugins/views_plugin_argument_default_php.inc(49) : eval()'d code).

Using views-7.x-3.x-dev ; and eva-7.x-1.x-dev.

nordicmaterial’s picture

Version: 7.x-3.0-alpha1 » 7.x-3.x-dev
Status: Closed (fixed) » Needs review
Issue tags: +taxonomy vocabulary, +related nodes, +related terms

This is how it goes in D7, with Views 7.x-3.x:

1. Create view (block)
2. Add fields, or use teasers, what works as well.
3. "Arguments" is from now on called "Contextual filters" , so go there.
4. Add the filter: "Content: Has taxonomy term ID"
5. Configuration on this filter:
-> When the filter value is NOT in the URL: -> Provide default value
-> Type: Taxonomy Term ID from URL
-> under that, check the box "Load default filter from node page, that's good for related taxonomy blocks."
-> now check the boxes of the taxonomies you want involved here

That's that. Save your view, display the block on the page where you'd like to see it and it should be there.
However, the current node will be one of the Related Items. You might not want that, so:

6. Now you need to add the second filter in order to exclude the current node from the list. Click [+] button of the contextual filters section.
7. Select Content: Nid
8. Set the 'When the filter value is NOT in the URL' to 'Provide default argument'
9. Set the 'Default argument type' to 'Content ID from URL'
10. Go to "MORE" , and check the "Exclude" box.
11. Don't forget to embed the block on your node pages
12. Save your view

nordicmaterial’s picture

dawehner’s picture

Component: taxonomy data » Documentation
Status: Needs review » Active
Issue tags: -taxonomy, -related content, -taxonomy vocabulary, -related nodes, -term ID, -related terms

Update issues things

bgibson’s picture

nordicmaterial,
Thanks for the step by step walk-through, that worked for me. Is there any way to display content from child taxonomy as well? For example, if I had a parent term called 'baseball' and a child term called 'Chicago Cubs', how can you configure the view for 'Chicago Cubs' to feed into the 'baseball' view? Thank you!

merlinofchaos’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

selwynpolit’s picture

Thanks for this nice set of instructions. I learned recently that if you want to put this view into a panels pane, you can easily do so and it is clever enough to work there too. Just remember to leave all the panels settings at default, don't set things like "send arguments"

Selwyn

samwillc’s picture

Thanks to #8 Posted by nordicmaterial on May 18, 2011 at 11:00am

That was just what I was after :)

maustyle’s picture

Great tutorial, spent heaps of time to find it.

maustyle’s picture

Great tutorial!!
Have spent heaps of time to find it.
Thanks nordicmaterial

dialn’s picture

Title: display related content in a block by taxonomy » Only works with first taxonomy term views3 D7

help, This works very well, but it only works with the first tag/taxonomy term applied to a node. For instance, if a post was tagged. Cheese, Recipe, Tomatoes, Pasta It would show other posts tagged with "Cheese", but not those tagged with "Recipe", "Tomatoes", or "Pasta". Is there a way to get it to consider all taxonomy terms, not just the first one?

Damani’s picture

Ok dialn I just tried this solution and it seems to work.... Just select "Reduce Duplicates" then under "More" select "Allow multiple values". That's it, you should have all the things being printed out and not just things related to the first term. Let me know if this helps.

ivanhi’s picture

Hi,

I have been reading a lot of post but I'm not able to do it.

I'm using Drupal 7 and Views 3.
I have done all the steps you suggest in the tutorial.

The point is if I select When the filter value is NOT available Taxonomy Term ID from URL the block don't display anything, (just the message of not results behavior).

However, if I select Fixed Value and write some term of the vocabulary of the taxonomy works great.
I also have tried to paste the next php code from another post:

$node = node_load(arg(1));
if($node){
foreach (field_get_items('node', $node, 'field_tags') as $term ) {
$terms[] = $term['tid'];
}
return implode('+', $terms);
} else { return; }

And I get the next error:

Warning: Invalid argument supplied for foreach() en eval() (línea 3 de /home/ispgrupo/public_html/videosempresas/sites/all/modules/views/plugins/views_plugin_argument_default_php.inc(50) : eval()'d code).
Notice: Undefined variable: terms en eval() (línea 6 de /home/ispgrupo/public_html/videosempresas/sites/all/modules/views/plugins/views_plugin_argument_default_php.inc(50) : eval()'d code).
Warning: implode() [function.implode]: Invalid arguments passed en eval() (línea 6 de /home/ispgrupo/public_html/videosempresas/sites/all/modules/views/plugins/views_plugin_argument_default_php.inc(50) : eval()'d code).

Do you have any idea?

Thank you very much.

f4o’s picture

#8 - works great. Thank you.

victoriachan’s picture

Hi,

Like ivanhi in #21, I've also tried following the steps instructed by nordicmaterial in #7, and it does not work for me. I am using Views 3.3.

It is either that it doesn't work on 3.3 (broken since the last version?) or that it has something to do with the fact that the field for my taxonomy term on the nodes involved is a multiple value field.

Or is it something else? Can someone please confirm that it works for them on 3.3?

Cheers,
Victoria

victoriachan’s picture

Hi,

I have tested it again, this time creating a new taxonomy term field that only uses single value (it was multiple before when it failed for me), and followed nordicmaterial's instructions in #7, and it worked correctly.

So it's only when I used a field with multiple values that it didn't return any results. Bug?

ivanhi, is your taxonomy field also a multiple value field?

Cheers,
Victoria

DravenDev’s picture

Priority: Normal » Major
Status: Closed (fixed) » Active

como agrego esta vista con filtros contextuales dentro de otra vista es decir:
tengo una vista donde muestro un campos (titulo, foto,resumen) del contenido que esta dentro de un nodequeue por posición, y quiero generar para esta vista sus notas relacionadas debajo estos campos como lo haría? con este tipo de vista de con filtros contextuales a esta otra vista? intente adjuntando pero no tengo resultados, tambien intente agregando un tipo de vista en el footer por Global: Área views

CarbonPig’s picture

Bump - same issue when trying to create a related content block by shared vocabulary, but there are multiple terms. Have been working on this for 3 days and have found this issue in multiple posts. None of the fixes work and none of the other modules satisfy my use case. Not complaining - love views - just helping to document.

Has anyone solved? Is this a regression from a previous version of views?

EDIT

I posted a more detailed issue here:

http://drupal.org/node/1833780

zombree’s picture

Issue summary: View changes

The fix in comment #20 worked for me -- thanks, Damani!

Here's how I configured my Related Content block in views:

Contextual Filters:

1. Content: Nid
-- When filter value is NOT available > Provide default value > Content ID from URL
-- More > Exclude

2. Content: Has taxonomy term ID
-- Provide default value >
------Taxonomy term ID from URL > Load default filter from node page (good for related taxonomy blocks) >
---------- Limit terms by vocab > whichever vocab you are using
---------- Multiple-value handling > Filter to items that share ANY term
-- Reduce duplicates
-- More > Allow multiple values

This worked for me to pull in other nodes that share any of the same terms assigned to the current node.

I'm not sure if some of the issues people are experiencing are due to complex issues or just simple "gotchas", but remember that the preview of this view won't show any results unless you put some arguments in the "Preview with contextual filters" field and hit "Update preview". Because of this, I found it easiest to test the functionality of the related content block by placing it on a page and testing it there.

And it never hurts to mention, keep in mind that if a field you include in your view is specific to one content type, and you are showing trying to show more than just that one content type in your view results, this could affect your output making it look like you have no results simply because you are not displaying the correct fields for other content types. Always best to start building your view with a universal field like Content: Title so little things like un-shared fields don't trip you up.

Just a couple "gotchas" that I remember encountering in the past when I was really new to Drupal. Let me know if this helped at all.

MustangGB’s picture

Priority: Major » Normal
MustangGB’s picture

Status: Active » Postponed (maintainer needs more info)

Is this still an issue?

MustangGB’s picture

Status: Postponed (maintainer needs more info) » Closed (won't fix)

No reply.