Using Views to Generate A Taxonomy List

bobchristenson - March 15, 2007 - 15:50

I've been trying to find a simple way to do this, and it's not easily found.

I want to create a View (either a block or page, I would have application for both) that can list taxonomy terms in a given vocabulary. Unfortunately, I can't figure out a good way to do this.

When I've tried, it's very inconsistant, and prints duplicates of the taxonomy terms (using node: distinct doesn't fix it)

Is there a way to do this?

The application I outlined here would be a good example: http://drupal.org/node/128080#comment-211402 (I didn't realize until after the post, that creating a list of galleries (which are actually taxonomy terms) would be the hard part)

I looked into the Category module (with category views) and it either doesn't do what I want, or I didn't understand it.
Any help/direction here?

Maybe I didn't get your

Anonymous (not verified) - March 15, 2007 - 16:44

Maybe I didn't get your problem, but doesn't this work for you:
http://www.Yoursite.Com/?q=directory

It presents me with a nice A (12) B (44) C (17) ... Z (5) list, which, upon clicking, opens the taxonomy terms beginning with that particular letter. But as I said, I don't know if that was your problem...

Ludo

In a block

bobchristenson - March 15, 2007 - 17:59

Hey Ludo...
Thanks, but no, that's not what I'm looking for. (maybe I confused you by saying I'd like it in a page too)...

But, i"d like the taxonomy terms (only from a certain vocabulary) to display in a listing...so I can use them in a block.

For example, on our podcast, each 'series' of episodes we do is a taxonomy...I want to do a block listing of all of our series, which means listing those taxonomies. Does anyone know how to do this?

-Bob Christenson
Owner/Designer, Mustardseed Media, Inc.
MustardseedMedia.com

Access Denied

NancyDru - March 21, 2007 - 22:51

I get "Access Denied" and I'm logged in as user/1.

Nancy W.
now running 5 sites on Drupal so far
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in Your Database

A little more clarification -

jastraat - March 15, 2007 - 19:38

Do you want the block of taxonomy terms to be related to the node in which the block is displayed?

If not - it might be easier to not use views at all, and just create a block using something like taxonomy_get_tree() for each vocabulary using that vocabulary's vid.

This sounds good

bobchristenson - March 15, 2007 - 20:20

If i understand this right, it might be the answer. I don't know PHP, so i'll have to play around with it, but I think all the info I need is on this page: http://api.drupal.org/api/HEAD/function/taxonomy_get_tree

Do i just format this into a block using PHP and it should list my terms for the vocab I designate?

-Bob Christenson
Owner/Designer, Mustardseed Media, Inc.
MustardseedMedia.com

Bob, To create a block with

jastraat - March 15, 2007 - 20:47

Bob,
To create a block with php-generated content, go to Administer >> Site building >> Blocks and add a new block. Change the input format to php. I found the following in another post that may be helpful:

<?php

$vid
= 1// Set the vid to the vocabulary id of the vocabulary you wish to list the terms from
$items = array();
$terms = taxonomy_get_tree($vid);
foreach (
$terms as $term ) { 
   
$count = db_result(db_query("SELECT COUNT(nid) FROM {term_node} WHERE tid = %d", $term->tid)); 
   
$items[] = l($term->name, "taxonomy/term/$term->tid") . " ($count)";
}
if (
count($items) ) {  print theme('item_list', $items);}
?>

Once you've created the block, you'll have to enable it in a page region. You can also designate on which pages you'd like the block to appear.

Perfect

bobchristenson - March 16, 2007 - 00:53

This looks perfect, jastraat. You're my hero.

But, is it just me, or is it strange that Views can't do this? Seems like it would be easy to implement. (Although, on second thought, views is good at listing nodes, but nothing else, sooo...maybe it's the wrong tool).

Anyway, thanks!

-Bob Christenson
Owner/Designer, Mustardseed Media, Inc.
MustardseedMedia.com

*grin

jastraat - March 21, 2007 - 14:52

Glad to help.

Many thanks for this code

Quint - July 16, 2007 - 04:06

Nice and simple solution.

Quint

Works great!

jiangxijay - July 26, 2007 - 00:32

I'm also adding a comment so I can track this solution.

Nice, but one problem I found...

colkassad - November 10, 2007 - 11:47

Wow, this is what I was looking for. Thanks. However, the selection does not take into account articles, etc, that are unpublished. For instance, if I have only one story under a certain term, and it's set to unpublished, it still shows up in the list along with the count in parenthesis. Perhaps the SQL could be modified to exclude unpublished articles?

That is correct

NancyDru - November 10, 2007 - 14:03

There are cases where I want it this way, and there are cases where I'd prefer it the "standard" way. Try changing:

$count = db_result(db_query("SELECT COUNT(nid) FROM {term_node} WHERE tid = %d", $term->tid));

to:

$count = db_result(db_query("SELECT COUNT(tn.nid) FROM {term_node} tn JOIN {node} n USING (nid) WHERE tid=%d AND n.status=1", $term->tid));

This is untested, but should be real close.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

Yep :)

colkassad - November 10, 2007 - 14:16

Great! That seems to work. Thank you, Nancy!

BTW

NancyDru - November 10, 2007 - 14:22

Your site is great

skyredwang - March 28, 2008 - 06:52

Thanks for your information and work.

BTW

NancyDru - March 28, 2008 - 13:25

I have now taken over the Taxonomy List module and some of those things are gradually working their way into the code.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

Same problem

Didaci - November 29, 2007 - 14:02

Hello,

I updated from 4.5.3 to 5.3 and i got the same problem, my categories block disappeared.

Your code fix it but would be posible to order it with the last updated term? And would be posible to show the last updated in days, hours, minutes ( examples: 1 years 11 weeks ago , 1 days 16 hours ago)?

Thanks.

Unfortunately, no

NancyDru - November 29, 2007 - 14:33

Taxonomy does not keep track of when terms are added. It could be added with a module, but apparently there are few people who want it.

Formatting intervals is easy: http://api.drupal.org/api/function/format_interval/5

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

Thanks!

pierrelord - December 11, 2007 - 01:24

Thanks!

Thank you.

excell - March 1, 2008 - 15:40

I have spent an entire day trying to figure out how to acheive this - generate a block with the terms for the category selections for a section! Two seconds of work and your answer is implemented and working! I had tried everything, searched many places and was totally frustrated.

Only thing I had to do was to add the code for if the term was empty and luckly I had a person with php knowledge available...
Just adding the line:

if ($count)

above the line: $items[] = l($term->name, "taxonomy/term/$term->tid") . " ($count)";

and - PERFECT!
Thanks sooooo much for sharing...

<?php$vid = 1;  // Set the

mz906 - April 2, 2008 - 14:56

<?php
$vid
= 1// Set the vid to the vocabulary id of the vocabulary you wish to list the terms from
$items = array();
$terms = taxonomy_get_tree($vid);
foreach (
$terms as $term ) {
   
$count = db_result(db_query("SELECT COUNT(nid) FROM {term_node} WHERE tid = %d", $term->tid));
   
$items[] = l($term->name, "taxonomy/term/$term->tid") . " ($count)";
}
if (
count($items) ) {  print theme('item_list', $items);}
?>

can you do this using the views UI?

lol

jfall - April 2, 2008 - 17:16

this discussion started with bob trying to use Views!
The upshot - Views produces a list of NODES not TERMS, so not really.
That said, here is a little "modulette" (just a packaged snippet) that uses a View to perform the queries - nodes are listed by term, using the View to select and format the node listing under each term.
http://lasqueti.ca/books/design-notes/custom-theme/directory
As a bonus, nodes for each term can optionally be put into collapsible fieldsets and organized into a number of columns to tidy up the display.
Here's an example of what this snippet can do:
http://lasqueti.ca/marketplace/directory
the View is defined to select only nodes that should be displayed in the directory (i.e., it filters out nodes I don't want displayed here) and specifies a Table view type with teaser image + title as link.

I am experimenting with

mariagwyn - April 7, 2008 - 18:57

I am experimenting with this, but am getting an error:
Parse error: syntax error, unexpected T_VARIABLE in .../sites/mariagwyn.com.snqclean/themes/snq/template.php on line 390. This line is the line where I set the variable $vid. I have it set to the correct image vocab, and the directory_view.php file is uploaded, set with require_once ('directory_view.php');

Any ideas?

Thanks,
Maria

???

NancyDru - April 2, 2008 - 19:35

I, personally, can't.

If you want to get even fancier, rather than hard-coding "taxonomy/term/..." the more correct implementation would be:

$items[] = l($term->name, taxonomy_term_path($term->tid)) . " ($count)";

This will allow modules like Taxonomy Redirect to function from your list.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

Shouldn't this be...

willdashwood - April 20, 2008 - 17:27

$items[] = l($term->name, taxonomy_term_path($term)) . " ($count)";

failed

Viranil - April 16, 2008 - 20:08

I tried this, and the import views method and neither returned my list. when i go to the /taxonomy/term/# page i see my nodes, but the query in the php code, and in the views approach seems to return nothing.

I like the php approach better because I am very new to the views module.
This way i just change the $vid to my term number and save the block. am i doing something wrong?

This has answered my

anotheraviator - May 16, 2008 - 21:21

This has answered my prayers. I does EXACTLY what I needed. I am building a directory of items that are all catagorized, then sub-categorized, then sub-catagorized again where the nodes ONLY exist in the last category. I wanted to allow users to click through categories until they reach the nodes. (Similar to the old Yahoo.com if anyone remembers that far back)

Now.. I've figured out how to use this code and blocks to do it for each subcategories page... but that requires creating a block for each and every cat/subcat. Is there a way to make this work generic so that I only need to create one block and then assign that single block to all my cat/subcat pages.

I guess what I am looking for is something that would set the parent value automatically based on something rather than me making multiple blocks like :

taxonomy_get_tree($vid,1,-1,1);
taxonomy_get_tree($vid,2,-1,1);
taxonomy_get_tree($vid,3,-1,1);
taxonomy_get_tree($vid,4,-1,1);
etc.

Maybe

NancyDru - May 16, 2008 - 22:15

The Taxonomy Context module may do something like that; you can look at it for inspiration.

Nancy Dru (formerly Nancy W. until I got married to Drupal)

Vocab list filtered by content type

johnphethean - May 29, 2008 - 20:39

I found the same script in the book pages that jastraat kindly quoted, but there isn't an explanation of how to filter by content type. Has anyone come across this?
Some vocabularies can be used by more than one content type, and I want to just view the list for one of them - is this possible?
Thanks in advance for any help...

Yes

NancyDru - May 30, 2008 - 00:20

Depending on whether you want to change the code or not. One way is the Node Type Filter module. Also there was a similar request by "summit" I think that I helped him with and it's posted somewhere (possibly in this thread). Here it is: http://drupal.org/node/128085#comment-811814

Hey there. I'll try to give

chriswb - March 15, 2007 - 20:08

Hey there. I'll try to give you a step-by-step guide for what you'd like to do.

Create a new view under administrator, site building, views.

In the basic information area, give it a name. You can also choose to limit the visibility to certain roles. Close the basic area.

Open the Page area. Select Provide page view. Edit the View Type to List. Make sure "use pager" is not checked. Enter the amount of items you want to list in the "nodes per page" area. If you don't want there to be a limit, put in 0. Close the page area.

Open the block area. Select Provide block. View Type, List. Give it a title and, once again, determine the number of items you want to list. Close the block area.

Open the filters area. Add a filter: Node: Published

Node Published equals: Yes

Add a second filter: Taxonomy terms for CATEGORY

Where "CATEGORY" is the category you would like to display.

Select the value you would like to be displayed.

Close the filter area.

Open the Sort Criteria area.

Add criteria "Node: Title" and choose ascending or descending.

Walla! You are done.

Not quite...

bobchristenson - March 15, 2007 - 20:16

Hey Chris...
Unfortunately, this is what i've been trying to do for a long time. However, it creates multiple listings for the same taxonomy item...and adding "Node: distinct" doesn't cure that....

Don't suppose you know how to fix that?

-Bob Christenson
Owner/Designer, Mustardseed Media, Inc.
MustardseedMedia.com

Bob, Sorry, I have no idea

chriswb - March 15, 2007 - 22:53

Bob,

Sorry, I have no idea why it'd do that. Strange!

The reason...

mikemccaffrey - May 16, 2007 - 19:00

...that multiple listings of taxonomy terms are showing up is because the View is actually returning nodes and just showing the node's taxonomy. So if multiple nodes share the same taxonomy the terms will be duplicated.

Views only returns nodes, so you just want a list of category terms then you need to use a different solution, like some of the ones below.

- Mike McCaffrey

Filtering taxonomies

jasio - May 12, 2007 - 11:33

Hi,

I prefer another filter:

Taxonomy: Vocabulary Name
"Is all of"/"Is one of" (does not matter, if only one vocabulary is selected)
Your vocabulary selected from list

For some reasons I feel more comfortable with it.

--
Best regards,

(js).

I think this is included in

catch - March 16, 2007 - 10:52

I think this is included in the views bonus pack (untested)

Panels: By term, 3 columns
Requires panels.module -- terms presented as tables within columns such as http://sfbay.craigslist.org/.<

http://drupal.org/project/views_bonus

Would the taxonomy_dhtml

reggie75 - March 22, 2007 - 04:56

Would the taxonomy_dhtml module be of any use?

http://drupal.org/project/taxonomy_dhtml

Hi, I've done this with

lukathonic - March 27, 2007 - 20:55

Hi,

I've done this with views - here is an export of my view:

$view = new stdClass();
$view->name = 'categories';
$view->description = 'Browse Subject Headings';
$view->access = array (
);
$view->view_args_php = '';
$view->page = TRUE;
$view->page_title = 'Browse Subject Headings';
$view->page_header = 'You are browsing a listing of the Land Centre\'s primary subject headings. For a complete listing of categories and subcategories, click here.

';
$view->page_header_format = '3';
$view->page_footer = '';
$view->page_footer_format = '1';
$view->page_empty = '';
$view->page_empty_format = '1';
$view->page_type = 'list';
$view->url = 'view/categories';
$view->use_pager = TRUE;
$view->nodes_per_page = '30';
$view->menu = TRUE;
$view->menu_title = 'Subject Headings';
$view->menu_tab = TRUE;
$view->menu_tab_default = TRUE;
$view->menu_tab_weight = '-10';
$view->sort = array (
array (
'tablename' => 'term_data',
'field' => 'weight',
'sortorder' => 'ASC',
'options' => '',
),
array (
'tablename' => 'node',
'field' => 'title',
'sortorder' => 'ASC',
'options' => '',
),
array (
'tablename' => 'node',
'field' => 'created',
'sortorder' => 'DESC',
'options' => 'normal',
),
);
$view->argument = array (
array (
'type' => 'taxletter',
'argdefault' => '6',
'title' => '%1',
'options' => '',
'wildcard' => '',
'wildcard_substitution' => '',
),
);
$view->field = array (
array (
'tablename' => 'node',
'field' => 'title',
'label' => '',
'handler' => 'views_handler_field_nodelink',
'sortable' => '1',
'options' => 'link',
),
);
$view->filter = array (
array (
'tablename' => 'node',
'field' => 'status',
'operator' => '=',
'options' => '',
'value' => '1',
),
array (
'tablename' => 'node',
'field' => 'type',
'operator' => 'OR',
'options' => '',
'value' => array (
0 => 'resources',
),
),
array (
'tablename' => 'node',
'field' => 'status',
'operator' => '=',
'options' => '',
'value' => '1',
),
array (
'tablename' => 'term_node_3',
'field' => 'tid',
'operator' => 'OR',
'options' => '',
'value' => array (
0 => '706',
1 => '1148',
2 => '471',
3 => '664',
4 => '1046',
5 => '796',
6 => '10',
7 => '1136',
8 => '13',
9 => '355',
10 => '1119',
11 => '9',
12 => '497',
13 => '70',
14 => '14',
15 => '822',
16 => '823',
17 => '821',
18 => '880',
19 => '11',
20 => '466',
21 => '49',
22 => '184',
23 => '51',
24 => '1211',
25 => '676',
26 => '819',
27 => '40',
28 => '626',
29 => '814',
30 => '700',
31 => '989',
32 => '243',
33 => '450',
34 => '303',
35 => '1210',
36 => '324',
37 => '1098',
38 => '648',
39 => '36',
40 => '170',
41 => '174',
42 => '1050',
43 => '173',
44 => '755',
45 => '1044',
46 => '521',
47 => '1195',
48 => '488',
49 => '313',
50 => '1169',
51 => '1107',
52 => '342',
53 => '181',
54 => '333',
55 => '299',
56 => '12',
57 => '8',
58 => '1133',
59 => '448',
60 => '805',
61 => '695',
62 => '506',
),
),
);
$view->exposed_filter = array (
);
$view->requires = array(term_data, node, term_node_3);
$views[$view->name] = $view;

Import this as a new view, then you'll need to change a few fields in the "filters" section to make it work, since your taxonomy terms are certainly different than these here.

This site is still under development, but if you want to see how this is working go to http://landcentre.dreamhosters.com/?q=view/categories

Cheers -

Incredible

NancyDru - March 28, 2007 - 00:08

All that to do what jastraat did in just a few lines and no extra modules...

Nancy W.
now running 5 sites on Drupal so far
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in Your Database

Just Views Code

bobchristenson - March 28, 2007 - 13:31

Actually, Nancy...if this works, it would be MUCH easier for me to use than jastraat's PHP code..this is just an export of his View...and for us non-coders, it's much easier to update/tweak.

Another wonderful thing about views...import/export. I love this module! Thanks for utilizing this feature.

-Bob Christenson
Owner/Designer, Mustardseed Media, Inc.
MustardseedMedia.com

I've been having the exact same problem

rdurrette - March 29, 2007 - 23:36

Whew...this node is a godsend...good thing I found it. I've been trying to create a professor rating system for a student-run website, and we've got each professor as a term within the category.

...kept running into your problem of having the duplicate terms.

I will try this and see if it works!

Views is definitely the way ..

illSleepWheniDie - February 1, 2008 - 11:01

Using views would always be better than hacking away .... and definitely more accessible to non-developers ..

Non-Developers

leoklein - February 4, 2008 - 14:12

Exactly, I'm doing a demo. If I told them to add code somewhere, they'd turn green.

I need something like what lukathonic has put together: list of taxonomy terms only using views.

Views needs nodes

NancyDru - February 4, 2008 - 14:52

Unless it's changed very recently, Views cannot display anything that is not attached to nodes. So something like a taxonomy or user list is not possible. Earl says he will do it some day, but probably not today.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

Shows Tax Terms in Use

leoklein - February 4, 2008 - 15:03

As I understand what's happening, this method -- "Taxonomy: Term Name/Summary, unsorted/: %1" -- shows terms that are currently being used (i.e. attached to a node). That's fine for me.

The people adding content and assigning terms will see the whole list in a Select List (i.e. the old fashion way) when they create a node.

excellent!

pcs305 - March 24, 2008 - 19:00

Following this discussion

Landcentre

Willdex - April 4, 2007 - 05:03

Looking at the site I suppose that it is possible to make a site's design look a bit like Craigslist? As least the classifieds section.
--
Billy | Major: Accounting | CityTech@CUNY | Student Block.

...

NancyDru - April 4, 2007 - 05:17

This was Michelle's "subscribe" comment. I'm no longer interested in this thread and tired of having to open it to get it out of my new items. I can't delete it since there's replies so I'm changing the name to nancyw since she's active in the thread.

Ditto

femrich - April 4, 2007 - 08:43

I'm also posting to keep track of this thread.

I'm tracking this thread

Tom-182 - April 4, 2007 - 14:12

I'm tracking this thread too, I'm still looking for a solution for this issue.

http://www.geektips.net

Tag

joshua_cohen - May 10, 2007 - 16:17

I am also looking for a good solution to this. I was using Taxonomy_DHTML, but it's very slow when there are thousands of terms involved. Bookmarking for now..

Josh

This is the answer to get a taxonomy list!

kuahyeow - April 9, 2007 - 06:03

So, the secret is choose an argument

"Taxonomy: Term Name" where the default is "Summary, sorted as view", and title is %1

Amazing!

--
Cheers,
Thong

Tip: http://drupal.org/forum-posting
Website: http://www.edoodle.co.nz

what does the 1% do exactly?

bjraines - April 11, 2007 - 16:25

what does the 1% do exactly?

The %1 puts the taxonomy

joachim - April 17, 2007 - 21:48

The %1 puts the taxonomy term name into the title for the "subview" (the view once it's given an argument and is showing you nodes for a particular term, not sure if that's the right word for it).
In other words, when you click on a term in the main view, and see the nodes for that term, the title is also the term.
To get a different title, change the %1 to something else. Eg, on my site I'm putting "News from %1" because the terms are village names.

Excellent :) Is there a way

joachim - April 17, 2007 - 21:42

Excellent :)

Is there a way to get it to show terms for which no nodes exist yet?

yes

NancyDru - April 18, 2007 - 03:03

How?

joshua_cohen - May 12, 2007 - 02:02

I would love to hear how this is done. Did you have to hack Views to accomplish this? Please do share, I'm looking to do the same thing. I need a complete list of taxonomy, regardless of whether a node has been associated with that term yet.

Thank You,

Josh

Nice, this solved my issues,

eferraiuolo - June 12, 2007 - 17:38

Nice, this solved my issues, plus added a nice bit of information about how many nodes are tagged with each term! Great!

Wow

goldentoque - April 11, 2007 - 16:11

Was having a hard time getting a page with only on instance of each taxonomy term listed using views. This example worked like a charm and had the added bonus of displaying the number of nodes in that category, as well as having the second listing page of the individual node titles.

Nicely Done

But What to Edit?

Dustin Boston - May 3, 2007 - 06:48

Hey I'm not quite sure how to edit this script...here's what I did.


$view = new stdClass();
$view->name = 'categories';
$view->description = 'Browse Subject Headings';
$view->access = array (
);
$view->view_args_php = '';
$view->page = TRUE;
$view->page_title = 'Browse Subject Headings';
$view->page_header = 'You are browsing a listing of the Land Centre\'s primary subject headings. For a complete listing of categories and subcategories, click here.

';
$view->page_header_format = '3';
$view->page_footer = '';
$view->page_footer_format = '1';
$view->page_empty = '';
$view->page_empty_format = '1';
$view->page_type = 'list';
$view->url = 'view/categories';
$view->use_pager = TRUE;
$view->nodes_per_page = '30';
$view->menu = TRUE;
$view->menu_title = 'Subject Headings';
$view->menu_tab = TRUE;
$view->menu_tab_default = TRUE;
$view->menu_tab_weight = '-10';
$view->sort = array (
array (
'tablename' => 'term_data',
'field' => 'weight',
'sortorder' => 'ASC',
'options' => '',
),
array (
'tablename' => 'node',
'field' => 'title',
'sortorder' => 'ASC',
'options' => '',
),
array (
'tablename' => 'node',
'field' => 'created',
'sortorder' => 'DESC',
'options' => 'normal',
),
);
$view->argument = array (
array (
'type' => 'taxletter',
'argdefault' => '6',
'title' => '%1',
'options' => '',
'wildcard' => '',
'wildcard_substitution' => '',
),
);
$view->field = array (
array (
'tablename' => 'node',
'field' => 'title',
'label' => '',
'handler' => 'views_handler_field_nodelink',
'sortable' => '1',
'options' => 'link',
),
);
$view->filter = array (
array (
'tablename' => 'node',
'field' => 'status',
'operator' => '=',
'options' => '',
'value' => '1',
),
array (
'tablename' => 'node',
'field' => 'type',
'operator' => 'OR',
'options' => '',
'value' => array (
0 => 'resources',
),
),
array (
'tablename' => 'node',
'field' => 'status',
'operator' => '=',
'options' => '',
'value' => '1',
),
);
$view->exposed_filter = array (
);
$view->requires = array(term_data, node);
$views[$view->name] = $view;

Basically I removed the references to term_node_3 because I was getting errors associated with that. It imported but my list looks like this:

# Interesting Links (1)
# Interesting Links (1)
# Interesting Links (1)
# Interesting Links (1)
# Interesting Links (1)
# Interesting Links (1)
# Interesting Links (1)
# Interesting Links (1)
# Interesting Links (1)
# Interesting Links (1)

Rather than

# Interesting Links (10)

Any ideas?

View

shrop - May 21, 2007 - 21:59

I was able to get this view to work by changing term_node_3 to term_node_2... in my case. The number represents the vid of the vocabulary you are trying to list. I also had to eliminate the filters and sorting stuff that was in there and then add back what I needed (ie: published = yes). Works well, but I think the php would work just as well and maybe easier since you can't just import this view and have it work out of the box.

It is good to know how to do this both way.

Thanks,
Shrop

Same problem

Aren Cambre - July 4, 2007 - 06:08

I am getting the same problem with this view. Here's my view (still contains a lot of the text and settings from the original example):

  $view = new stdClass();
  $view->name = 'categories';
  $view->description = 'Browse Subject Headings';
  $view->access = array (
);
  $view->view_args_php = '';
  $view->page = TRUE;
  $view->page_title = 'Browse Subject Headings';
  $view->page_header = 'You are browsing a listing of the Land Centre\'s primary subject headings. For a complete listing of categories and subcategories, click here.

';
  $view->page_header_format = '3';
  $view->page_footer = '';
  $view->page_footer_format = '1';
  $view->page_empty = '';
  $view->page_empty_format = '1';
  $view->page_type = 'list';
  $view->url = 'view/categories';
  $view->use_pager = TRUE;
  $view->nodes_per_page = '30';
  $view->menu = TRUE;
  $view->menu_title = 'Subject Headings';
  $view->menu_tab = TRUE;
  $view->menu_tab_weight = '-10';
  $view->menu_tab_default = TRUE;
  $view->menu_tab_default_parent = NULL;
  $view->menu_parent_tab_weight = '0';
  $view->menu_parent_title = '';
  $view->sort = array (
    array (
      'tablename' => 'term_data',
      'field' => 'weight',
      'sortorder' => 'ASC',
      'options' => '',
    ),
    array (
      'tablename' => 'node',
      'field' => 'title',
      'sortorder' => 'ASC',
      'options' => '',
    ),
    array (
      'tablename' => 'node',
      'field' => 'created',
      'sortorder' => 'DESC',
      'options' => 'normal',
    ),
  );
  $view->argument = array (
    array (
      'type' => 'taxletter',
      'argdefault' => '6',
      'title' => '%1',
      'options' => '',
      'wildcard' => '',
      'wildcard_substitution' => '',
    ),
  );
  $view->field = array (
    array (
      'tablename' => 'node',
      'field' => 'title',
      'label' => '',
      'handler' => 'views_handler_field_nodelink',
      'sortable' => '1',
      'options' => 'link',
    ),
  );
  $view->filter = array (
    array (
      'tablename' => 'node',
      'field' => 'status',
      'operator' => '=',
      'options' => '',
      'value' => '1',
    ),
    array (
      'tablename' => 'node',
      'field' => 'type',
      'operator' => 'OR',
      'options' => '',
      'value' => array (
  0 => 'facts',
),
    ),
  );
  $view->exposed_filter = array (
  );
  $view->requires = array(term_data, node);
  $views[$view->name] = $view;

All it does is show my taxonomy terms that begin with A, and it repeats the taxonomy term for each time it is attached to a node. E.g.,:
- Aa (1)
- Aa (1)
- Aa (1)
- Aa (1)
- Aa (1)
- Aa (1)
- Aa (1)
- Aa (1)
- Aa (1)
- Aa (1)
- Aa (1)
- Ac (1)
- Ac (1)
- Ac (1)
- Ac (1)
- Af (1)
- Af (1)

Am I missing something?

I have a bad feeling that this isn't intended behavior for the view, so it could disappear in future Views module releases.

I fixed that ..

illSleepWheniDie - February 4, 2008 - 02:42

Try changing Arguments => default to "summary, unsorted" .. that did the trick for me .. I dunno how i figured that .. i was wondering how to fix it, and my hand just pulled down the selector and chose that option and *BAM* it worked ... paranormal drupal coding .. !creepy!.

Other fields in the view

iaminawe - April 7, 2008 - 15:17

Hi,

Really great thread... only thing is, when using "Summary, Unsorted" it seems to only spit out the taxonomy terms and not any other specified fields.

So for example I am using taxonomy images and I would like the relevant images to appear next to each taxonomy term listed with the number next to it.
I define Taxonomy image in the fields section but the view only outputs the text.

Anyone have any ideas?

It needs to be in a view and not a block as I am using Services to access the view info and feed it to flash.

Thanks in advance

That's a nice example for us

SirMoby - May 14, 2007 - 17:46

That's a nice example for us poor hacks.

Thanks

Hmm...

NancyDru - May 14, 2007 - 20:27

Not indeed...

rtandon - August 29, 2007 - 19:10

Hi Nancy, I have seen your appreciation for jastraat's code in this thread at multiple places. Indeed it's simple and elegant, I appreciate that too. I also happened to view your modified version of code, which I would have liked to include here for instant reference but would not do so 'coz you have the right to post that!

Now two points in this context
1) Your code follows more or less same logic, but jastraat's code includes another logic - which permits printing, only where there are non zero no. of terms in the vocabulary, which you have skipped. On similar lines you have also done the trick to include terms with non zero no of nodes. Would it not be best to combine both? I could have done that, but you might prefer to post it yourself.
2) While theses codes are elegant, perhaps quicker too - yet assuming views are cached, wont using the view be faster (just due to caching)

Thanks

NancyDru - August 29, 2007 - 21:46

I left out terms with no nodes because I find that very confusing, especially in larger vocabularies. It is very simple to change.

Views, cached or not requires considerably more code (read: CPU) to do what this code can do easier. Further, Views requires several more queries in order to do it (caching will only mask that to some extent). Simple code is going to be more scalable than complex code.

<p>This is a sample of how to create a list of all terms that are being used from a particular vocabulary (category).</p>
<?php
  $vid
= 2;         /* <---- put correct vocabulary ID here */
 
$show_pic = module_exists('taxonomy_image');
  echo
'<div class="use-pin">';
 
$items = array();
 
$terms = taxonomy_get_tree($vid);
  foreach (
$terms as $term ) {
     
$count = taxonomy_term_count_nodes($term->tid);

      if (
$count) {   /* don't show terms with 0 count */
       
if ($show_pic) { $pic = taxonomy_image_display($term->tid, 'hspace="5"'); }
        else {
$pic = NULL; }

       
$name_and_count = l($pic . $term->name,'taxonomy/term/'.$term->tid, NULL, NULL, NULL, NULL, TRUE)." (".$count.") - ".$term->description;
       
$items[] =  $name_and_count;
       } 
/* end if count */
  
} /* end foreach */

 
if (count($items)) { print theme('item_list', $items); }
  else print
'No terms found';
  echo
'</div>';
?>

This code now also includes support for the Taxonomy Image module.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

Refining this list by node type

newswatch - September 10, 2007 - 16:32

Thanks Nancy for this. A great help so far. But then this provides a block ACROSS all no types. I want to create a block which will only be for "story" nodes. Is there any way to do that?

Thanks.

I am using this code:

<?php
  $vid
= 2; /* <---- put correct vocabulary ID here */
 
$items = array();
 
$terms = taxonomy_get_tree($vid);
  foreach (
$terms as $term ) {
   
$count = taxonomy_term_count_nodes($term->tid);
    if (
$count) { /* don't show terms with 0 count */
     
$items[] = l($term->name,'taxonomy/term/'.$term->tid)." (".$count.") - ".$term->description;
    }
  }
/* end foreach */
 
print theme('item_list', $items);
?>

Well, actually

NancyDru - September 11, 2007 - 02:01

Actually, this only counts the nodes for which the vocabulary is applicable. If the vocabulary is defined only for "story" type, then only "story" would be counted. However, if it's defined for "story" and "page" then it will count both.

I do have another method that separates the count by type on my site. http://nanwich.info/content_count_taxonomy_term

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

refining by node type

As If - February 5, 2008 - 10:52

This is a modified version of Nancy's code (see link above) to output an item list instead of a table:

<?php
  $vid
= 1; /* <---- put correct vocabulary ID here */
 
$typ = 'forum'; /* <---- put correct content type here */
 
$vocab = taxonomy_get_vocabulary($vid);
  foreach (
$vocab->nodes as $key => $value) {
   
$type[] = $value;
  }
 
$terms = taxonomy_get_tree($vid);
  foreach(
$terms as $term) {
   
$count = 0;
    foreach(
$type as $content_type) {
      if(
$content_type == $typ) {
       
$count = taxonomy_term_count_nodes($term->tid, $content_type);
      }
    }
    if (
$count) {
     
$items[] = l($term->name,'taxonomy/term/'.$term->tid)." (".$count.") - ".$term->description;
    }
  }
  echo
theme('item_list', $items);
?>

-------------------------------------------
Interactive Worlds and Immersive Obsessions
http://www.asifproductions.com

Hmm...

NancyDru - February 5, 2008 - 13:06

It appears that you are only interested in a single content type here, so you can simplify the code.

The "foreach ($type..." [BTW, note the space in keeping with Drupal standards] isn't needed at all. The only part of that loop you need is $count = taxonomy_term_count_nodes($term->tid, typ); and note the change to the second parameter.

Also, if you have a rather large taxonomy, "taxonomy_term_count_nodes" is not the best performing function to use.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

Hmm again...

As If - February 6, 2008 - 04:21

I think you mean the if($content_type == $typ) statement isn't needed. (in other words, the $count = ... line does not need to be held within a conditional.) If so, you're right. I just tested it. But the foreach ($type... loop is actually needed.

"taxonomy_term_count_nodes" ;-) I just lifted that straight from your example!

-------------------------------------------
Interactive Worlds and Immersive Obsessions
http://www.asifproductions.com

Hmm...

NancyDru - February 6, 2008 - 04:35

Maybe I'm reading it wrong (which has been known to happen) but I think you only need "$count = taxonomy_term_count_nodes($term->tid, typ);"

"taxonomy_term_count_nodes" is something that everyone has. I was just warning you that there are more efficient versions of that available. For example, if you have my Site Documentation module, it uses a more streamlined version called "sitedoc_term_count_nodes" that can be called from external code. It just will be less resource-intensive.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

Count but not showing node-title/body

Summit - February 6, 2008 - 08:18

Hi,
In almost all the posts I see the node count. Is it possible may be to show how to ouput the nodetitle and nodebody/teaser under the termnames instead of the count behind the termnames?
I would very much be helped with a efficient version of this code then!
Thanks in advance for your reply! ,
greetings,
Martijn

Yes! precisely like that, I

Summit - February 6, 2008 - 13:19

Yes! precisely like that, I like the different theming also :)
I like to use it with return theme('item_list', $links); [=> because other code where this peace should be implemented is made with this] Is this possible please?
See: http://drupal.org/node/128085#comment-717939 for the function I want to expand with this code.
Thanks a lot in advance!

Greetings,
Martijn

Here you go...

NancyDru - February 6, 2008 - 15:25

<?php
  $vid
= 1;         // <---- put correct vocabulary ID here
 
$terms = taxonomy_get_tree($vid);
 
$items = array();

  foreach (
$terms as $term) {
   
$count = taxonomy_term_count_nodes($term->tid);

   
// Note: the number of nodes selected per term is controlled by 'feed_default_items' from the RSS publishing settings page.

   
$result = taxonomy_select_nodes(array($term->tid), 'or', 0, FALSE, 'n.sticky DESC, n.created DESC');
   
$children = array();
    while (
$node = db_fetch_object($result)) {
     
$children[] = node_view(node_load($node->nid), TRUE, FALSE, FALSE);
    }
   
$items[] = array(
     
'data' => l($term->name,'taxonomy/term/'.$term->tid)." (".$count.") - ".$term->description,
     
'children' => $children,
      );
   }
   echo
theme('item_list', $items);
?>

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

Altered panels_taxonomy_child_terms_list

Summit - February 7, 2008 - 13:51

Hi Nancy,

This is what I made of it, and it is working with Panels_taxonomy!

function panels_taxonomy_child_terms_list($tid, $vid, $args, $argidx){
  $count ='';
  foreach (taxonomy_get_children($tid, $vid) as $child_tid => $child_term) {
    $args[$argidx] = $child_tid;
    $items = array();
    $result = taxonomy_select_nodes(array($child_term->tid), 'or', 0, FALSE, 'n.sticky DESC, n.created DESC');
    $children = array();
    $count = taxonomy_term_count_nodes($child_term->tid);   
    while ($node = db_fetch_object($result)) {
       $children[] = node_view(node_load($node->nid), TRUE, FALSE, FALSE);
    }
    $items[] = array(
      'data' => l($child_term->name,'taxonomy/term/'.$child_term->tid)." (".$count.") - ".$child_term->description,
      'children' => $children,
      );
    }
  return theme('item_list', $items);
}

Thanks a lot for your help with this! It is working.
EDIT: May be very strange question.
But should I use:
taxonomy_render_nodes(taxonomy_select_nodes(array($child_term->tid), 'or', 0, FALSE, 'n.sticky DESC, n.created DESC');
);

Instead of taxonomy_select_nodes?

Greetings,
Martijn

You could

NancyDru - February 7, 2008 - 14:36

You could, but you lose the theme_item_list that you said you wanted. Then you would also need to change the "children" building.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

You could

NancyDru - February 7, 2008 - 14:38

You could, but you lose the theme_item_list that you said you wanted. Then you would also need to change the "children" building.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

Hi Nancy, I leave it than. I

Summit - February 7, 2008 - 14:49

Hi Nancy, I leave it than. I don't know how to change this, and what the benefits and disadvantages are. It is working now, so I leave it this way, right?
Greetings, Martijn

Hi Nancy, Do you know how I

Summit - February 18, 2008 - 09:49

Hi Nancy, Do you know how I can get rid of the bullets? They look ugly on my site?
Thanks in advance for your reply!

Greetings, Martijn

CSS

NancyDru - February 18, 2008 - 15:08

Put something like this in your CSS:

.no-bullets {
  list-style-type: none;
}

And then make sure the <ul> gets a class="no-bullets".

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

Hi Nancy, This is my code.

Summit - February 18, 2008 - 15:52

Hi Nancy,

This is my code. It works fine, but shows the bullet and I would like only to show the nodes of the sto