Hi, I'm using category module and I need an alphabetically list of the nodes that are in a container/category. I tryed to do it with view, but I didnt succeded. I created a view that takes the node title has argument to sort, but if I apply it a category container, when I navigate to a single category that is in that container, I only obtaion a page with the title of the category, and I have not the list of the nodes are inserted.
I tryed to create the link for the view I created to understand if is working, and if I use it as a page, it displays me all the nodes in alphabetical order.
What is wrong?
Please help me with this issue.

Comments

pbarnett’s picture

Hi!

You need to add a filter (or argument, passed as part of the URL) on Taxonomy term to the view to select only nodes in the given category.

Pete.

JirkaRybka’s picture

To filter by Taxonomy filters, you must have the Category's taxonomy-wrapper installed (default on 5.x Category). The principle is that Category is not offereing its own Filters for Views, but using the Wrapper you'll get a mirror of your Category data in the old Taxonomy tables, so the Taxonomy filters may be used.

An alternative is to add native Category filters, fields etc. to Views - this is discussed at many places around the Forum ( http://drupal.org/node/85984 ), many versions from different people (I've also my own one), quite needed to come into the Category package... But still it's just incomplete patches. So use Taxonomy filtering if you can.

Also consider category_views, which allows to substitute the default category-listings with a View, giving it an argument for current Category, so no need to create a separate View for each category.

axtolf’s picture

Hi, thank you for all details.
Hi user this page: http://category.greenash.net.au/category_views_usage that is exacly what you are saying so I created a new view, I put as field to be view only node title, because is what I need, in Arguments I put taxonomy term id, and under sort criteria, I put node title in ascending.
That, if I had understand in the right way what you wrote me, is exacly what you're saying.

I'm starting to have some doubts about my installation of drupal. I've all modules I need enabled (views, taxonomy, category), I created a container with some categories, Categories created for me node fo eache category. In Taxonomy I've nothing, nothing was created. So this i right or there is something wrong?

pbarnett’s picture

Hi again.

I'll install views on a new Drupal 5.x site, then I can tell you what I did to get a view which shows all nodes with a given taxonomy term.

May take a while (1 hour or so), but I will reply soon.

Pete.

pbarnett’s picture

Hi again.

category_views is a different module, which I'm not using.

I have a Drupal 5.1 site with Taxonomy enabled and Views downloaded, installed, and enabled; I'm using Views 5.x-1.6-beta5.

Here's a walkthrough of the process.

I'm doing this as I type, so be patient :-)

Login as admin.

Go to Administer=>Content management=>Categories

On a new install like this, it says 'No categories enabled'.

Click on 'Add vocabulary'.

For 'Vocabulary name' enter a name which will appear as the name of the term; I used 'Category'.

For 'Types', select 'page'. You can add node types to this later.

To keep things simple for now, click on 'single' for the hierarchy. Select 'required' so that all new pages will require the user to select a category for the page when it's created. Again, you can change these options later.

Click on ''Submit'. You should see the categories list again with a message confirming that your vocabulary has been created.

The vocabulary 'Category' will be listed below the message.

Now we need to add some terms to the vocabulary.

Click on 'Add term'.

Type in a name in the 'Term name' field. I used 'First'. Click on 'submit'.

You should be back at the same 'Add term screen'.

Add as many term names as you wish; I added 'Second'.

You can use the 'Weight' field to control where in the list of categories the term will appear, but we won't worry about that now.

Now, when you go to 'create content' and select 'page', you should see a 'Category' dropdown with the term names you have created.

Select a category (I chose 'First'), enter a title and and some body text, and submit the page.

Create another page with the same category.

Create a page or two with another category. I used 'Second'.

OK; if you go to Administer=>Content management=>Content you should see your pages listed, but unless you have published them as 'Promoted to front page' they will not be visible on your site. On to the views...

Go to Administer=>Site building=>Views.

Click on 'Add'.

Enter a name in the 'Name' field. I used 'nodesbycategory'; this is just a name for the view and doesn't affect the URL. Note that you can only use alphanumerics and the underscore for the name.

Click on 'Page' to get the page view options.

Click on the 'Page view' checkbox and then enter a URL in the URL field. I used 'category'.

For 'View type' select 'List view'.

Click on 'Arguments', select 'Taxonomy: Term Name' from the dropdown box and click on 'Add argument'. This is where you tell the view to only display nodes (and in our case, only nodes that are pages) in the category [one of your categories]. In my case

It's a good idea to select 'Filters' and add 'Node: Published' or your view will show unpublished pages which could be spam.

Select the fields that you want to see in this list view. I used 'Node: Title', 'Option' 'As link', 'Sortable' 'Yes', 'Default sort' 'Ascending'.

Scroll to the bottom and click 'Save'. You should see a confirmation message telling you that your view was saved.

You can now go to http://yoursite.com/[the URL you chose for the view]/[term name] and see a list of links to pages in that category.

This URL can now be used in links, added to menus, and so on.

There's a live demo of this; email me for details, as it's on a low-bandwidth site.

And now that's working, you can go back and explore all those options I ignored :-)

Hope this helps...

Pete.

axtolf’s picture

Hi Pete.
First of all, thank you for time you spent trying to do what I need.
But its not exactly problem. Infact the view I created work if I use it as a single view, using the url I set up in the view config page, but will not work if I apply it to the category. In the category there is an option that allow you to use a specified view for a container, and this is my problem, if I apply a view that seems to me to be ok (since when I test the view as url, it works), I only obtain a page with the title of the category, and not with the list of the nodes that are under that category (if I disable the option to use a view for a container, nodes are shown, sort in the order of creation).
So this is my problem, I cant apply view to the category module.

JirkaRybka’s picture

Please check if you really have the Taxonomy Wrapper installed (on the Category module's settings page there must be a green message saying this) - this is a replacement for the old Taxonomy module, which must be active to give you a mirror of Category data in the Taxonomy tables (where Views looks), otherwise Categories work, but Views doesn't see it.

Even if the Wrapper is active now, something may have gone wrong before, and tables might be out of sync, giving you the trouble. You can check in your database, view the Taxonomy-tables to see if your Categories are there.

My favorite way is to avoid filtering by taxonomy at all, adding the native Category filters. See my post here: http://drupal.org/node/69861#comment-238417 - basically it's how I got it working on Drupal 4.7.3 with matching Category and Views modules. Now it's a bit outdated, but I'm still running it on 5.1 - I'll post my new views_category.inc file here within 24hours, as soon as I gain access to my home-machine containing the data. By the way - other patches from that post are dead, as theming goes into the template.php, and URL fix now took place in newly patched category_views. Both these additional things are probably unnecessary for you, though.

axtolf’s picture

Hi JirkaRybka. I checked and the wrapper seems to be correctly installed. I can see it in category module, and, in the module list, taxonomy moved for core section to category section, that I thing is another thing that indicated that in same way, all is normal.
I'm not sure how to check if the tables are sync. I can find the tables for category (that are named "category") but I can find tables of taxonomy since there is nothing that is called "taxonomy". Which are the tables please?

When I started this work, I installed category after drupal configuration, with taxonomy enabled, but without nothing inside. After installation of drupal, Category, views, I started to create a container and some category and begin to populate it with nodes. I dont know in which way table can be no sync, because I created content only after applying all I need.
To be sure I installed a new drupal, using another DB. And tried to do the same. And I obtain the same, view that I set up is empty.

I saw your code, I'll try the code for 5.x if you are so kind to post here. But anyway, I'm very curious to understand why I'm not able to use views with category, since is documented as features... there is something wrong somewhere.

axtolf’s picture

Found them...
Tables:
Drupal_term_data
Drupal_term_hierarchy

Are populated, even if in it I have only on thing (I have only one container, that is what I have in this table).

These tables:

Drupal_term_node
Drupal_term_relation
Drupal_term_synonym

Are empty.
I guess that is not correct. I way to try to sync the tables? I try to disabled/enabled the modules, but nothing changes.

JirkaRybka’s picture

Well, if your Drupal_term_node table is empty, then we've found the problem at last! This table is (as for the "fake Taxonomy") supposed to contain all the relations between nodes and terms, i.e. all existing ID-pairs of submitted nodes and their assigned Categories. THIS is where Views look to retrieve nodes for a given Category, so if there's nothing, you get no nodes listed then. (Also affects the Categories/Terms shown on node views, if supported by your theme.)

Weird thing is, that I also had this table empty once (just after upgrade and legacy import from Taxonomy to Category), so I suspect the Taxonomy-wrapper thing of having an undiscovered-yet bug causing this under some conditions. (Category package is not exactly bug-free, sadly.)

The table may be easily recovered by copying the contents from table Drupal_category_node, which is the original to be mirrored: Go to PhpMyAdmin (or similar), and do export of your Drupal_category_node table into SQL dump. Open the created file with a text-editor, and change all the occurences of "Drupal_category_node" to read "Drupal_term_node", and the column identificator "cid" to be "tid". Then import the tweaked file into your database, and if you're lucky, you'll have the tables in sync again. I've done exactly this on my site, and it did the trick with no side effects. But needless to say - always keep a backup of your database, while trying this kind of stuff.

But now another question arises: Are we sure that the tables won't come out of sync again in future, as new nodes are added to your site? Answer is: Not quite sure, neither of us know exactly what's happening. So I still suggest trying the solution I mentioned above, which is working perfectly well on my site:

Category module itself doesn't offer its own filters etc. for Views, it only just maintains "fake Taxonomy" - but not exactly bug-free, as we've seen. It works like this:

Node submission -> Drupal_category_node -> Taxonomy wrapper -> Drupal_term_node -> Filter by Taxonomy term ID -> Views

I've a more straight solution:

Node submission -> Drupal_category_node -> Filter by Category ID -> Views

This is logical, and it's pretty bombproof as it uses the "original" table directly, so we no more need to bother what happens with the poorly-maintained "mirror" (which might be still necessary for other purposes, though). All we need is a Category-filter for Views, the missing bit between the two modules.

To get this working, do the following:

--- Create a new file in sites/all/modules/views/modules/ (hopefully this is the path where your Views' .inc files are already located), name it views_category.inc, and paste the following inside:

<?php
//
// Here is where we expose category tables to views
//
function category_views_tables() {
if (module_exists('category')) {

  return array(
    'category_node' => array(
      'name' => 'category_node',
      'join' => array(
        'type' => 'left',
        'left' => array(
          'table' => 'node',
          'field' => 'nid',
        ),
        'right' => array(
          'field' => 'nid',
        ), // end right array
      ), // end join array

      'fields' => array(
        'cid' => array(
          'name' => t('Category ID'),
          'sortable' => true
         ), // end cid array
       ), // end fields array

      'filters' => array(
        'cid' => array(
          'name' => t('Category ID'),
          'operator' => 'category_handler_operator_eqto',
          'handler' => 'category_handler_filter_eqto',
          'help' => t('This filter allows to filter by category IDs.'),
        ), // end cid array
      ), // end filters
    ), // end category_node array
    
    'category' => array(
      'name' => 'category',
      'join' => array(
        'type' => 'inner',
        'left' => array(
          'table' => 'category_node',
          'field' => 'cid',
        ),
        'right' => array(
          'field' => 'cid',
        ), // end right array
      ), // end join array

      'fields' => array(
        'description' => array(
          'name' => t('Category: Description'),
          'handler' => array(
            'category_hand_fld_desc_lnk'   => t('With link'),
            'category_hand_fld_desc_nolnk' => t('Not a link')
           ),
          'addlfields' => array('cid'),
          'sortable' => true
         ) // end description array
       ), // end fields array

      'filters' => array(
        'cnid' => array(
          'name' => t('Container: ID'),
          'operator' => 'category_handler_operator_eqto',
          'handler' => 'category_handler_filter_eqto',
          'help' => t('This filter allows to filter by container-ID.'),
        ), // end cnid array
      ), // end filters

      'sorts' => array(
       'description' => array(
       'name' => 'Category: Description',
       'help' => t('Sorts by category description.')
       ) // end description array
      ) // end sorts array
    ), // end category

    'node_cat' => array(    // Another instance of node, to look after category names
      'name' => 'node',
      'join' => array(
        'type' => 'inner',
        'left' => array(
          'table' => 'category_node',
          'field' => 'cid',
        ),
        'right' => array(
          'field' => 'nid',
        ), // end right array
      ), // end join array

      'fields' => array(
        'title' => array(
          'name' => t('Category: Title'),
          'handler' => array(
            'category_hand_fld_titl_lnk'   => t('With link'),
            'category_hand_fld_desc_nolnk' => t('Not a link')
           ),
          'addlfields' => array('nid'),
          'sortable' => true
         ) // end description array
       ), // end fields array

      'filters' => array(
        'title' => array(
          'name' => t('Category: Title'),
          'operator' => 'views_handler_operator_like',
          'handler' => 'views_handler_filter_like',
          'help' => t('This filter allows nodes to be filtered by Category names.'),
        ), // end title array
      ), // end filters

      'sorts' => array(
       'title' => array(
       'name' => 'Category: Title',
       'help' => t('Sorts by category title.')
       ) // end title array
      ) // end sorts array

), // end node_cat


  ); // end return array
}
}


function category_views_arguments() {
  $args = array(
    'cid' => array(
      'name' => t("Category ID"),
      'handler' => 'category_handler_arg_cid',
      'help' => t('This argument filters for a given Category ID'),
      )
    );
  return $args;
}


function category_handler_arg_cid($op, &$query, $argtype, $arg = '') {
  switch($op) {
    case 'summary':
      $query->ensure_table('category');
      $query->add_field('description', 'category');
      $query->add_field('cid', 'category');
      $fieldinfo['field'] = "category.description";
      return $fieldinfo;
    case 'sort':
      $query->add_orderby('category', 'description', $argtype);
      break;
    case 'filter':
      $query->ensure_table('category_node');
      $query->add_where('category_node.cid = %s',$arg);
      $query->set_distinct();
      break;
    case 'link':
      return l($query->description, "$arg/" . intval($query->cid));
    case 'title':
      $result = db_query("SELECT description FROM {category} WHERE cid = %d", $query);
      $voc = db_fetch_object($result);
      return check_plain($voc->description);
  }
}

/**
*  Category-description/title as link (or not) handlers
*/

function category_hand_fld_desc_lnk($fieldinfo, $fielddata, $value, $data) {
  return l($value, "node/$data->category_cid");
}

function category_hand_fld_desc_nolnk($fieldinfo, $fielddata, $value, $data) {
  return check_plain($value);
}

function category_hand_fld_titl_lnk($fieldinfo, $fielddata, $value, $data) {
  return l($value, "node/$data->node_cat_nid");
}


/**
* A list of options to be used in equal to queries
*/
function category_handler_operator_eqto() {
  return array('=' => t('Is Equal To'), 'not' => t('Is NOT Equal To'));
}


/**
 * Filter for IDs
 */
function category_handler_filter_eqto($op, $filter, $filterinfo, &$query) {
  if (is_numeric($filter['value'])) {
    switch ($op) {
      case 'handler':
        $table = $filterinfo['table'];
        $column = $filterinfo['field'];
        $field = "$table.$column";
        $query->ensure_table($table);

        switch ($filter['operator']) {
          case '=':
            $query->add_where('%s = %s',$field,$filter['value']);
            break;
          case 'not':
            $query->add_where('%s != %s',$field,$filter['value']);
            break;
        }
      break;
    }
  }
}

(I know it's long, but I seem unable to attach files to this thread. The code basically came from a few other threads, and was later extended and heavily debugged by me. Still not done completely, perhaps you can see, some items around Argument page-titles still using Category description instead of title, not all the fields/filters defined that one might want... But for the purpose discussed here, it's fully working.)

--- Clear the Views' cache (button on Views, or just empty the table; if you get any problems, try clearing also other caches).

--- Now you should see in the Views some new things appear: Fields Category ID, Category description, Category title; filters by Category ID, Container ID, Category title; sorts by Category description, Category title; and the most important - Argument Category ID. Use this argument instead of previously suggested "Taxonomy term ID" on your View (better leave the other settings to this argument at default - other choices are not fully tested), and if you wish, you can also use the other new items, such as showing Category title assigned to each node (linked or not), and such.

axtolf’s picture

Hi.
First of all, I used your code, and in a couple of click I resolved my issue.
Thank you for your time, for your code and for all the info you gave me.
I'm sure that something wrong occurs with the wrapper. Infact you told that could be that even if you copy the category table in taxonomy one, could be that new contents will be not inserted in the table. Yes, in my case I created some nodes after installing the category and nothing gone in the taxonomy tables. So something strange, dont know if is for something I done or for something dab in the code somewhere.
Anyway, is normal that modules sometimes has problems.
After this reply I'll open a bug for category module. It's a very good and usefull module, so is a pity that this bug could be resolved.
In my case, I was able to solve everything with you code. Thank you again a lot.

axtolf’s picture

Hi, got another little problem that I'm sure you had. Breadcrumbs. When I'm on category or container breadcrumbs works well, but when I'm on the single node contained in a category, breadcrumbs will works well.
I suppose that the problem is the same about the view. Breadcrumbs are created using taxonomy terms ($terms), and since we have seen that there is a problem of sync between tables, I think I have to use another var to show correct breadcrumbs on a single node. Please, you have an idea to how to do it?
Thank you again

JirkaRybka’s picture

Breadcrumbs is a completely different issue: It's generally Drupal's problem that usually nodes miss a sensible breadcrumb. The breadcrumb may be set explicitly by a module (for example Forum does this), or else it is generated from the menu-structure, i.e. shows only for things having matching item in the navigation menu. Some people suggest putting all the nodes into menu (disabled entries, if one douesn't want to really show up there), but I strongly recommend to avoid this, as it makes menu-administration almost impossible, or even the site crash due to performance problems in the menu system.

We need a brand new module based on Category, which sets the wanted breadcrumb explicitly. Fortunately, I created exactly the said module a week ago for my site, so now it waits for approval in the Category issue queue: http://drupal.org/node/75276#comment-279550

To use it, just create a new directory "category_breadcrumb" under "sites/all/modules/category/contrib/", and put two new files inside: One is the category_breadcrumb.module file from my last post in the linked-above issue (rename it to remove .txt from filename - was only needed to attach the file to this comment), the other is a short category_breadcrumb.info file (may be taken from the archive attached to my previous post to that Issue, or try the one from comment #25 which also seems fitting). Then just enable the new module at the admin modules page, and choose your preferences at the "Category breadcrumb" settings page.

BTW - You don't need to worry about the other patch mentioned in the linked-above post, unless you're using Hidden containers and want to avoid them showing in breadcrumb.

axtolf’s picture

HI JirkaRybka
it works perfectly and do exactly what I need. Thank you very much for the issues you solved and for the great code you posted to the community.

mik77’s picture

JirkaRybka, your help with the category & views modules has been very useful. I'd wasted a lot of time on this, but you saved me. Thanks!!

axtolf’s picture

Hi, got another "problem", probably connected to category, view and breadcrumbs, since it appears after the last modification. Actually, when I create a new node or modify a node inserted in a category, drupal automatically create a voice menu for the single node. Even if I delete it, it will be created again anytime I modify the node.
I checked in the settings of category and the container, but I didnt find nothing. Any ideas?
Thank you

JirkaRybka’s picture

Category + menu = trouble...

First, I'm not using the "category_menu" module at all. It's still a bit too buggy, so I just diabled this module and put Categories to menu by hand, if necessary.

Second, there's (probably for you too) a menu-settings fieldset on node-submit forms, which should be left blank to avoid menu items creation. If already created, edit the node and check the box to delete menu item - otherwise menu will be updated each time you save the node.

This is not related to my breadcrumb-module, which doesn't touch menu at all (by the way, module is now added to Category project for future releases :-).

So, check category_menu and menu-settings on nodes in question for unwanted settings.

axtolf’s picture

Hi, thank you for your reply. Probably it was something related to some bugs, because after disabled/enabled the category menu module, all returned normal. Thank you again