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
Comment #1
Damani CreditAttribution: Damani commentedSo 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!
Comment #3
Damani CreditAttribution: Damani commentedComment #4
Damani CreditAttribution: Damani commentedComment #5
borazslo CreditAttribution: borazslo commentedi will try it
Comment #6
bryancasler CreditAttribution: bryancasler commentedRelated? #512304: implode views_plugin_argument_default_php.inc
Comment #7
nordicmaterial CreditAttribution: nordicmaterial commentedSolution #1 gives a very similar error :
Using views-7.x-3.x-dev ; and eva-7.x-1.x-dev.
Comment #8
nordicmaterial CreditAttribution: nordicmaterial commentedThis 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
Comment #9
nordicmaterial CreditAttribution: nordicmaterial commentedComment #10
dawehnerUpdate issues things
Comment #11
bgibson CreditAttribution: bgibson commentednordicmaterial,
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!
Comment #12
merlinofchaos CreditAttribution: merlinofchaos commentedComment #14
selwynpolit CreditAttribution: selwynpolit commentedThanks 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
Comment #15
samwillc CreditAttribution: samwillc commentedThanks to #8 Posted by nordicmaterial on May 18, 2011 at 11:00am
That was just what I was after :)
Comment #16
maustyle CreditAttribution: maustyle commentedGreat tutorial, spent heaps of time to find it.Comment #17
maustyle CreditAttribution: maustyle commentedGreat tutorial!!
Have spent heaps of time to find it.
Thanks nordicmaterial
Comment #18
dialn CreditAttribution: dialn commentedhelp, 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?
Comment #20
Damani CreditAttribution: Damani commentedOk 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.
Comment #21
ivanhi CreditAttribution: ivanhi commentedHi,
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:
And I get the next error:
Do you have any idea?
Thank you very much.
Comment #22
f4o CreditAttribution: f4o commented#8 - works great. Thank you.
Comment #23
victoriachan CreditAttribution: victoriachan commentedHi,
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
Comment #24
victoriachan CreditAttribution: victoriachan commentedHi,
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
Comment #26
DravenDev CreditAttribution: DravenDev commentedcomo 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
Comment #27
CarbonPig CreditAttribution: CarbonPig commentedBump - 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
Comment #28
zombree CreditAttribution: zombree commentedThe 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.
Comment #29
MustangGB CreditAttribution: MustangGB commentedComment #30
MustangGB CreditAttribution: MustangGB commentedIs this still an issue?
Comment #31
MustangGB CreditAttribution: MustangGB commentedNo reply.