The background: Some months ago I began working with views to try to manage site information by taxonomy term. After working for a while on the problem, I turned to this forum for help (and committed myself to writing a tutorial when the problem was solved). I didn't figure out a solution through that thread, but have discovered others face similar issues. (http://drupal.org/node/139616)

I moved to other problems for a while, then returned and found this thread (http://drupal.org/node/128085) where several solutions (using views, php, and other approaches) are offered. It seems to have solved the issue for some users, but although I have imported and tried to modify the views posted in this thread to see if they would work for me, I've had little success.

Although I have learned lots about views by reading forum posts and looking at the views documentation (and particularly http://drupal.org/node/54432), and although I have managed to create some simpler views, I still have not been able to sort out what I think is probably a very straightforward views task, and one which I observe many users trying in frustration to get views to achieve.

Now I am going to try to think through the problem in the hope that someone with better understanding of the system has an Aha! moment and can tell me what I'm doing wrong. And perhaps by detailing my newbie approach, I can help the views experts make Views more accessible to less experienced users.

Note: I really want a Views solution here, partly because Views seems like it should be within my grasp, and partly because the various PHP and module solutions I have found are both very far from what I want and completely opaque (to me in any case) in terms of manipulating their output.

Here goes:

The problem: I have a CCK content type called "Resources". It's basically just an edit of the Story content type with link fields added. I created a vocabulary called "Resource Categories" whose terms refer to, well, specific categories of resources I want to publish on my site. That vocabulary is applied to the Resources content type and I have been happily creating new Resource nodes now for some months.

Of course, it isn't particularly useful to browse through so many nodes stored in reverse chronological order. So I would like to use Views to generate an alphabetical listing of terms used in Resource Categories, with each term followed by a nested list of titles to which that term is applied. The node titles should link to each individual Resource entry. Something like this:

  • Cars (3)
    • Buick
    • Niva
    • Volkswagen
  • Trucks (1)
    • Mack
  • etc.

This seems like it should be a straightforward Views task, but the closest I have come to approximating this list is something more like:

  • Cars (1)
  • Cars (1)
  • Cars (1)
  • Trucks (1)

Or even worse...

  • Resource Categories (1)
  • Resource Categories (1)
  • Resource Categories (1)
  • Resource Categories (1)

The way I see it, this must be a problem with the way I am creating the views, because if Views could not create a simple list like this, then it wouldn't be getting all the rave reviews.

Thinking through the process: Having dug into the sources mentioned above (among others) here is how I am approaching creating the view.

  1. I create a view called "resource_categories" with access for all users and the description, "Browse Resource Categories"
  2. I make it a Page view at URL resources/categories of view type List View. I use the pager and give it 30 nodes per page
  3. For now I ignore header, footer, and empty text
  4. I provide a menu as a tab, weight -10 (I want this to be a tabbed view, though I have not yet created another tab)
  5. For now, I ignore the block region; I will want a block later, but for now am focused on the page view
  6. Fields. Ok, here's where I think the critical stuff begins. As I understand it, Fields are essentially the information you want to have shown in the view. In my case, these are: Taxonomy: Terms for Resource Categories set with option "without links" (and label is blank) and Node: Title with label blank, handler normal, option "as link", sortable "no" (I don't want users to be able to sort this, though "yes" did not seem to make any difference) and default sort "ascending" (alphabetical)
  • My logic here: If I list the taxonomy terms first and node titles second in the Fields category, this is the closest approximation of the end result I want available to me in Fields.
  • Arguments: I still don't quite understand arguments. I understand they act like an optional filter--I assume sort of like an exposed filter. At first I tried my view with no argument, as I don't really want this view to have options. I want one list reflecting specific terms and content only. That didn't seem to work. In my current view (and following advice in the discussion at this thread (http://drupal.org/node/128085) I have applied Taxonomy: Term Name, Display all values. I have tried setting title to blank, %1, and %0, and option to blank, 0, or 1. Neither item seems to change the view regardless of what is inputted in those last two areas. Hmmm..... So what's an argument?
  • Filters. This seems like it should be straightforward. I select Published, Equals, Yes as the first filter, and Taxonomy: Vocabulary Name is One Of Resource Categories. With some views I have also tried Node Type is one of Resource, but this seems redundant as this is exactly the same data set represented by the vocab name filter.
  • Exposed Filters. None defined: I want a static index, not one that can be manipulated by users.
  • Sort Criteria. Sort by: Taxonomy: Term Name, Ascending (a weighted and alphabetical listing of terms in the Resource Categories vocabulary) and by Node Title: Ascending (an alphabetical listing of node titles, nested (hopefully) under the term(s) to which they are linked.
  • As far as I can tell, this view should give me the result I want. Instead I get a listing of my node titles (as links) and above each node title is an alphabetized, horizontal listing of all the Resource Category terms associated with the node. Hmmmm.... This (like other results) is close enough to give me hope, but not close enough to make me happy. I can study the list and see that the nodes appear in an order following the alphabetical order of the category terms, but that won't help my users.

    Here is the link to my current view (http://kjourn.net/resources/categories). It may change from what is described above as I play around with it. I will also post an export of my view current below.

    Now my questions (if you've stayed with me thus far, and I am grateful if you have): What am I doing wrong? How am I misusing views and ending up with something other than what I want? What suggestions do you have for fixing this?

    I renew my promise here: Once I have a solution for this, I will write it up as a tutorial. And now that I think of it, what you've read just now might serve as a new Drupal.org genre: the puzz-torial. I'm still looking for a solution. Thanks for listening. My exported view follows below:

    ******

    $view = new stdClass();
    $view->name = 'resource_categories';
    $view->description = 'Browse Resource Categories';
    $view->access = array (
    );
    $view->view_args_php = '';
    $view->page = TRUE;
    $view->page_title = 'Browse Resource Categories';
    $view->page_header = 'You are browsing a listing of Kjourn.net\'s resource categories.

    ';
    $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 = 'resources/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_tab_default_parent_type = 'tab';
    $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' => '',
    ),
    );
    $view->argument = array (
    array (
    'type' => 'taxletter',
    'argdefault' => '2',
    'title' => '',
    'options' => '',
    'wildcard' => '',
    'wildcard_substitution' => '',
    ),
    );
    $view->field = array (
    array (
    'tablename' => 'term_node_5',
    'field' => 'name',
    'label' => '',
    'options' => 'nolink',
    ),
    array (
    'tablename' => 'node',
    'field' => 'title',
    'label' => '',
    'handler' => 'views_handler_field_nodelink',
    'defaultsort' => 'ASC',
    'options' => 'link',
    ),
    );
    $view->filter = array (
    array (
    'tablename' => 'node',
    'field' => 'status',
    'operator' => '=',
    'options' => '',
    'value' => '1',
    ),
    array (
    'tablename' => 'term_data',
    'field' => 'vid',
    'operator' => 'OR',
    'options' => '',
    'value' => array (
    0 => '5',
    ),
    ),
    );
    $view->exposed_filter = array (
    );
    $view->requires = array(term_data, node, term_node_5);
    $views[$view->name] = $view;

    Comments

    nancydru’s picture

    Non-Views coding is not all that difficult and will scale to larger sites. Here are three different ways to do it, all of which should be more efficient that Views:
    Taxonomy Term Count
    Content Count by Taxonomy Term
    Content Count by Taxonomy Term - Pager Version

    Views is a great package and it can do a lot, but there are also things it's not so great at (e.g. it can't do users at all). I look at Views as primarily a prototyping tool. I would only leave it live on a site that will never have a great deal of traffic.

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

    netentropy’s picture

    nancy is it possible to do a alpha directory that shows terms that just link to the term page....

    i am thinking of moving my free homework help site to drupal

    the directory view almost does it but it takes you to a directory page for the term not the term page itself

    nancydru’s picture

    The "pager version" above already sorts the terms alphabetically.

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

    netentropy’s picture

    oh i guess i thought the link itself was a demo

    do i just past that inot a page block or do i make some sort of template file with that on it

    that is one thing i never understand...where are we suppose to stick that code?

    thanks

    nancydru’s picture

    You can put it in page, block, or even a module. You just select the "php code" for the input format.

    alpha horizontal links like in views - I don't understand what this means.

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

    netentropy’s picture

    sorry,

    the directory view list the first letter of each term

    A | B | C |

    those links take you to a list of terms

    those term links take you to all the nodes in that term BUT, instead of it being a term page it is a directory page. I would like this page to be the actual term page.

    nancydru’s picture

    netentropy’s picture

    thanks

    i was hoping to do it without views....i'll figure it out...thanks !!!

    nancydru’s picture

    netentropy’s picture

    i looked at it again

    what i meant was to have the alpha horizontal links like in views

    femrich’s picture

    Thanks for the links, Nancy. By the way, I've appreciated your contributions to drupal.org starting with the Drupal Cookbook. A very nice addition.

    I did experiment a bit with Taxonomy Term Count after seeing your mention of it in one of the long threads I linked to in my post above, but turned away from it because the output is not really close to what I want and I don't have a clue (at this point) where to begin with manipulating it. Also, having seen a number of messages indicating that PHP, if misused, can eat a site alive, I've been hesitant to get into it. But perhaps I should get over it and investigate a general PHP tutorial....

    Still, I'd like to figure out what I am doing wrong with Views.

    femrich’s picture

    Okay, now I am getting closer. I switched the argument to:

    Taxonomy: Term Name with Default as Summary, Sorted Ascending. I tried leaving the title blank or adding %1, and there is no apparent difference between the two. But this view gives me something much closer to what I am looking for:

    • Cars (3)
    • Trucks (1)
    • etc.

    Now I have to figure out how to get the titles of individual nodes to appear beneath the category terms...

    Nancy, I am not giving up on your suggestion. I just have to figure out this Views puzzle, then I will work on the puzzles you've provided...

    stamat’s picture

    Hi!
    I am also a newbie with drupal.
    While surfing the drupal.org , I have come upon this module, that you might find useful
    module. I havent tested it , but the description is somehow similiar to what you are trying to achive.

    nancydru’s picture

    That description is very sketchy. I probably would never try it unless someone suggested it.

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

    ianr’s picture

    thanks nancy, your script worked fine. NOBODY should do this with a view given how easy it is to do with a script!

    jfall’s picture

    femrich -
    Your original approach (first post) won't work - because a View is a list of nodes. Period. And that's just what you got - a list of nodes, each displayed with its list of associated tags.
    I think you are on a more productive track with summary View arguments.

    As I understand it, an "argument" basically allows you to define a view with one "filter" unspecified. The "argument filter" is supplied dynamically (usually in the path) to fully specify the view. Thus a view with an argument actually defines a whole set of pages, one for each possible argument value. E.g., the view "vehicleView" with an argument "Taxonomy Terms: Vehicle Type", might produce the following pages: mysite.ca/vehicleView/cars, mysite.ca/vehicleView/trucks, ... etc.
    Note that the trailing term on the URL supplies the argument value to fully specify the view!

    In addition, if no argument is supplied, then a "Summary View", at mysite.ca/vehicleView, is displayed, which is what you have shown...
    * Cars (3)
    * Trucks (1)
    * etc.
    The summary view simply supplies a list of all possible argument values with links to their respective "fully specified view" pages.

    I am finding this to be a VERY useful concept with wide application, EXCEPT, the summary view is not well documented and it seems few people are pushing it to do much exciting (this article, http://drupal.org/node/42605, is one of the few I've found that actually does something interesting with the summary.) I can think of at least 2 things I'd like to do:

    1. theme the summary list to pull in some data from one sample node for each argument;
    2. theme the summary list to pull in titles from all nodes and list them under each argument (this is your example I think, and a nice way to create a directory);

    Having gone down this road myself, one of the biggest stumbling blocks I'm running into (and perhaps someone has a solution?) is that now that I have created a view to display my nodes-by-term, there are now TWO pages on my site that display all the nodes from a given term: (1) the taxonomy generated page (/taxonomy/term/tid); and (2) the view generated page (/view/by-term/term). Worst of all - I don't want my users to see (1), after all I especially created (2)! BUT, the taxonomy terms, wherever they are listed, always link back to (2)... oh it hurts.

    I think in working with Views it is critical to keep in mind a mantra from the View documentation: "a view is a list. a list, a list, a list" - but remember, it is ONLY a list of nodes! It was never intended (I don't think) to be a list of taxonomy terms (or users, or any other thing) :-( The "summary view" comes so close to providing this functionality, but seems to fall a bit short...
    After many trials and much searching, I'm starting to conclude that NancyW may have the right approach - use Views for proto-typing, and then custom code for production. That said, I'm not giving up on Views just yet - I think tighter integration with Taxonomy (or perhaps a CCK type to replace taxonomy?) could yet resolve these issues. And despite these (relatively minor) shortcomings, Views is a FANTASTIC module - I really couldn't work without it.

    I'll keep watching your thread here, and eagerly await your tutorial!

    jfall’s picture

    Amazing how posting something can get you thinking....
    There IS a module to re-direct those taxonomy term links wherever you want (e.g., back to the view you just created for each term!).... I'm going to give it a try.
    http://drupal.org/project/taxonomy_redirect

    *Edit* Yup - that works nicely! Whew. Now, on to theming those summary views...

    Devis’s picture

    - Subscribing -

    Hoping to see a Views solution too, that's the only way for my customers.
    Thank you femrich.

    --
    Italy

    jfall’s picture

    OK femrich - I got kind of interested in this again, after having dropped the idea a few weeks ago. I found a few things that were really helpful:

    What I did was to create my own "bonus view type" called "directory". I swiped some of the Node Browser code to create a node tree for the nodes in the View, and VOILA! A views solution, with no PHP coding (for you - I had to write a little module).

    I posted a feature request for the Views Bonus Pack that includes a very preliminary version of the module - but it works, and it may actually be suitable enough for your purposes. You can find all the details and instructions at: http://drupal.org/node/223700
    I'd appreciate it if you would give it a try and post your thoughts there.

    jfall’s picture

    I just found a module snippet to do this:
    http://drupal.org/node/225426

    Still, I think a Views plug-in would be a simpler solution.

    dmnd’s picture

    views does not do sorting by multiple taxonomies, out of the box, although it does get the rave reviews any way.