Showing the nodes under a taxonomy term AND under the related terms

kiko - October 14, 2009 - 19:46
Project:Views
Version:6.x-2.6
Component:search data
Category:support request
Priority:normal
Assigned:Unassigned
Status:fixed
Description

Well, that's mostly it. I want to build a view where given a taxonomy, term the content under that taxonomy term and under all of the taxonomy related terms appear. Any help will be welcome!

I can not find a proper way to buil it on my own with the current views, and I'm not a php programmer so... :-(

#1

dereine - October 16, 2009 - 06:36
Status:active» postponed (maintainer needs more info)

What is your definition of related terms? I don' know such a name in standard-drupal.

#2

kiko - October 16, 2009 - 21:24

Taxonomy terms have two major extensions: synonyms and related terms. For each term you can define its related terms via e.g. Taxonomy manager.

I have now two terms: term1 and rterm2. I define a relation between term1 and rterm2. I'd like to show all the nodes tagged under term1 or rterm2 when I call to taxonomy/term/term1

K

#3

dereine - October 17, 2009 - 07:11
Status:postponed (maintainer needs more info)» active

Ups, i didn't read the issue enough :)

You can use views argument validation php code

<?php
$tid
= $argument;
$terms = taxonomy_get_related($tid);
foreach (
$terms as $term) {
 
$list[] = $term->tid;
}
$handler->argument = implode(',', $list);
?>

Perhaps you have to rewrite the code to use it for term-names not tids.

#4

gagarine - October 20, 2009 - 14:37

Hello,

Can i do something similar with filter instead of argument?

Thanks

#5

Dimm - October 29, 2009 - 16:00

<?php
$tid
= $argument;
$terms = taxonomy_get_related($tid);
foreach (
$terms as $term) {
 
$list[] = $term->tid;
}
if(
$list){
$handler->argument = implode(',', $list);
return
TRUE;
}else{
return
FALSE;
}
?>

#6

Faya - November 16, 2009 - 11:06

Thanks for this

#7

GiorgosK - November 20, 2009 - 12:19

@Dimm
where do I put this validation code from #5 ?

EDIT: I suppose you add arguments (from the Views UI)
but for what kind of argument (taxonomy term perhaps ?)
and what settings go in there ?

#8

kiko - November 30, 2009 - 06:39

Hi again

This code almost seem to work, but... What I've done is:
1.- Add a "Taxonomy: Term ID" argument
2.-Validator: PHP Code with the code:

$tid = $argument;
$terms = taxonomy_get_related($tid);
foreach ($terms as $term) {
  $list[] = $term->tid;
}
$handler->argument = implode(',', $list);
return TRUE;

3.- Save

The code seems to capture all the related arguments, but only the first one is passed to the view. I've tried both, checking and unchecking the option "Allow multiple terms per argument" and "Allow multiple arguments to work together", but nothing happens

Any idea?

#9

kiko - November 30, 2009 - 23:15

Almost solved. The idea is to add a "+" to the implode function, more than a ",". Probably my fault explaining it, I wanted to list everything under a taxonomy term OR its related terms. The code is finally:

$tid = $argument;
$terms = taxonomy_get_related($tid);
foreach ($terms as $term) {
  $list[] = $term->tid;
}
$handler->argument = implode('+', $list);
return TRUE;

But one problem remains. I can list everything under the related terms, but not those listed under the term itself. Any idea?
Thanks in advance

#10

merlinofchaos - November 30, 2009 - 23:25

Try this:

<?php
$tid
= $argument;
$list = array($tid);
$terms = taxonomy_get_related($tid);
foreach (
$terms as $term) {
 
$list[] = $term->tid;
}
$handler->argument = implode('+', $list);
return
TRUE;
?>

You can make this a bit less code like this:

<?php
$list
= array($argument);
foreach (
taxonomy_get_related($argument) as $term) {
 
$list[] = $term->tid;
}
$handler->argument = implode('+', $list);
return
TRUE;
?>

#11

kiko - December 1, 2009 - 22:14

It works!

Thanks a lot indeed!

#12

dereine - December 1, 2009 - 22:33
Status:active» fixed

So this is fixed. Anyone want to write some documentation?

#13

kiko - December 2, 2009 - 02:19

#14

GiorgosK - December 2, 2009 - 10:16

I have done as described above but when I use exposed (or even non exposed) filter to view nodes of certain term I don't get the related term nodes

here is an export of a very basic view
what am I doing wrong ?

AttachmentSize
view_with_related_terms.txt 4.81 KB

#15

kiko - December 3, 2009 - 11:27

I think the problem is that this method works properly with arguments introduced via URL (e.g. www.example.com/taxonomy/term/%), but not with filters.

 
 

Drupal is a registered trademark of Dries Buytaert.