I'm using the category and views modules. I've created a container, populated it with categories, and categorized some nodes with the categories. I've also installed the category module's taxonomy wrapper.
I now want to use the views module to create a block listing the categories in the container.
I selected Taxonomy: Term as my field. I've selected 2 filters: Taxonomy: Terms for [my container], and Distinct. This displays the categories in that container which have nodes assigned to them, but not those which don't. I can live with that, but it also displays multiple copies of those categories which have multiple nodes assigned to them. The distinct filter doesn't help.
I also tried selecting Taxonomy: Terms for [my container] as my field, but that displayed nothing.
I also note that the Taxonomy: Terms for [my container] filter produces a select control containing exactly the distinct list I'm looking for, but I don't know how to generate that list as the content of my views block.
If necessary, I'm willing to write some code to expose the category module to the views module, but I'm hoping there's a simpler approach that I'm somehow missing.
Any tips on how to display a distinct list of categories in a category would be much appreciated.
Thanks.
Comments
I can live with that, but it
Then the distinct filter is not working as it is specifically for the 'nodes assigned to multiple categories' situation. Perhaps you can raise an issue for this against views module.
Instead of taxonomy terms, try using node id as the filter because category module creates both containers and categories as nodes. In my few experiments with views module, I seemed to get better results this way.
Once you have created a view for your container, edit the container's 'category view' setting to specify that the container should use this view instead of the default listing.
But, do you really want to use views just to show a container's categories in a block? You can always create a new menu at admin >>menu and manually move these categories into that menu. After this, all you need to is to enable the menu's block at admin>>block.
Thanks
Thanks for the quick reply. I like your menu idea as a quick solution.
In the long run I'd like a solution where I can choose something like Taxonomy: terms in [my container] or Category: Categories in [my container] as the field, and it would produce the list I am looking for. This has the advantage that the view will dynamically update as users add, edit or delete categories in the container.
I suspect this may mean adding a file to expose the category module to the views module.
I suspect this may mean
Actually, category is one of the few modules that fully implements the views module API. It comes with its own category_views module (you should install the views module before enabling this).
Once installed, category_views lets you override the default listing styles for a container or container with one generated by a view. The trick is to select Node ID (each container and category is a drupal node) or Node type (both containers and categories are drupal node types) as the filter, create your view and in the container or category edit form's category_view settings, specify that this view should be used on the container or category page.
But, I suppose creating a menu and manually moving it is easier.
Containers and views...try node distinct
in your view. That worked for me when I had multiple categories for a content type. It posted nodes according to their respective categories and only once.
Lsabug
Actually, category is one of
Just want to add that I may be wrong about this. Apparently, the category_views module only lets you replace the display of categories/containers with a view. This is not the same as full integration. Some code has been contributed to do this and hopefully Jaza will find time soon to work it into the module.
same need, but no solution yet (?)
Hello,
Sid, can i ask if you managed to get this sorted out? I need to output a view containing the top ten most visited terms in my "Countries" container (300+ categories).
cheers and thanks for any help you might give
You must expose category table(s) to Views
In order to do anything with category module categories (not taxonomy vocabularies) in views, you must expose the category module tables to the views api. This is discussed in general here: http://drupal.org/node/42609
What I did was to write some code that I included in the "category.inc" file that does just that: expose category, category_node and category_hierarchy tables to views. Once this is done, you can manipulate category fields directly in views and don't have to try and work out the logic of using the taxonomy wrapper.
Here is the code I used for the fields I needed:
Good luck!
-ron
Any other functions needed?
What other code is need to expose category to views?
category_handler_operator_eqto and its category_handler_filter_eqto?
I know how to add them but not the right values to return.
As i tried i couldn't save the value in the views filters for category.
Tracking this thread too:
http://drupal.org/node/89154
category_handler_operator_eqto
Oops. Here's the rest of the code I created:
Thanks!
Thanks for the quick reply!
I first tried with a single '=' as operator and it let me save the filter field value.
Also, the argument for category ID was picked up by category_views inmediately.
It wasn't working for me because category legacy was missing during category creation
(taxonomy terms still shows 'uncategorized' in the test site).
Edit:
I don't know if these codes are enough for a patch.
The next step would be a container ID argument.
I want to using view to
I want to using view to filter node base on categoryID. can you show me how to do that
Manythanks
best regards
category ID the basic filter
"category: ID" appears in both "add argument" and "add filter".
It goes in place of the "taxonomy: term ID" option.
The options may vary depending on the patch used.
couple of tricks to do this:
thank you Somebody for sharing this code ;)
I am actually using a couple of "tricks" that let me do what we need:
1. the first one is to create a list of *all* my categories (Filters: Node: Type is one of Category), then filter down only the categories promoted to front page (Filters: Node: Front Page equals Yes) and of course set the publishing status of all the categories i want to output to "Promoted to front page".
2. the other method (probably more usefull if you want to create various lists of categories from different containers) is to create a CCK content-type (can be empty also) and assign it to a particular container with the category_transform.module that you find in the Category package (needed in order to make the trick work!). Then you can simply filter by that "categorized" content type, and all your corresponding categories will be listed.
Of course these are "trickeries" more than solutions, so the code above should be proposed as a patch to core Category so to make it list categories in Views out of the box. Maybe you already did?
Cheers!
Those are fine options.
Using cck as containers/categories is a good option, specially with the cvs contemplate.module.
They can be fully themed, and contemplate can embed other views inside cck fields.
Perhaps free tagging helps, multiple parents and all that, altough new filters may be needed in category.
If more complex filtering is need, creating views on the fly is an advanced solution.
scratch scratch..
pardon my doubt, but:
a. how do you put togheter the two pieces of code above and the patch that can be found in the Category issue queue at http://drupal.org/project/comments/add/57821 ?
b. will this let me set a "Argument: Category Hierarchy: Distant Parent" for dynamically outputting nodes listed under distant children categories?
I'm actually looking for someone with enough Category+Views skills to do this, if interested please drop me a line. Of course the results will be available to the community.
thanks!
from what i get: you can
from what i get: you can take the above two snippets, drop them in your category.module file and you should be all set.
I got it working
using the code in the comment below and setting category id as argument.
http://drupal.org/node/85984#comment-178189
http://www.signalkuppe.com
My version of the Category + Views thing
Thanks to SomebodySysop for his excellent code! Since we are using both Taxonomy and Categories on our site for some reasons, we're unable to install the "taxonomy wrapper", and so I really needed the patch.
I had some minor problems, probably caused by my inaccurate work while editing the files, but finally it works. But however, I consulted the Views API documentation, and made my own revision, which I believe is better for our site: For the sake of keeping things tidy and scripts short I removed the advanced "category-and container-relations stuff", which is unnecessary at our site (as well as many others I believe), and I also added the link-functionality to the category-description field, so that your views may really link to full lists of the categories, instead of just showing the names.
Emdedded views on category-pages step-by-step (on Drupal 4.7.3. with Category 1.118 - don't know about other versions, but it might work also):
--- Add the category-functionality to Views, by placing a brand new file "views_category.inc" into the "(your_site)/modules/views/modules", which I believe is easier and safer than editing any existing files. The content of this new file is:
--- Empty your cache by deleting all the content from table named "drup_cache" (or something like that depending on your installation), executing the MySQL query along the lines "DELETE FROM `drup_cache` WHERE 1", from the PhpMyAdmin for example. Only then the new functionality will show up.
--- Go to "administer views" and create a new view, providing a page-view with all the fields, filters and sorts as you like, the way your category-pages should look like. In the "Arguments" field, add argument called "Category ID" - otherwise your category pages will display all nodes, and not only chosen category! The default field may be left as is ("return page not found") since this never happens and is most safe option. Save your new view.
--- Go to the edit-page on container, where you are going to emded the view onto all category-pages. Under "category view settings" select your new view, and save the container. You might also want to turn off the display of "prev/up/next" links, if you don't like them on the top of your view.
--- If you like to use some Exposed filters on this embedded view (to allow users custom filtering of nodes, like we did on our site here), you'll probably encounter the problem, that after changing and submitting the filters your users end up on the view's page only, without being embedded in the category and limited to its nodes (or even a "page not found"). That's a bug in the views module, which may be corrected by this small patch (sorry, I didn't have time to search if there is another patch for this around this massive forum, so I wrote my own). In "(your_site)/modules/views", in the file "views.module", under the function "views_filters_form" I changed the last bit this way:
This patch ensures, that the form-submit URL is always a mirror of the actual page the view is embedded on, so the Exposed filters don't drop off from Categories. Note that we don't use Clean URLs on our site, so I can't check if the patch is okay also in that case! Also note that I changed the text on Submit button, to be different from submitting articles - I just needed to have different labels in the Czech locale for "submitting content" and "searching", which was impossible with identical English originals.
--- Another problem with exposed filters is, that the selectors are always styled as a table, which means they always stay in one horizontal line. That makes your page extremely wide (when there is a lot of exposed fileters, that is), forcing your users to scroll horizontally to find the submit-button. If you like to style the selectors as floats (to reflect variable widths of a resizable browser-window, like I did), you might like to include another patch into the views.module file - pretty close below the previous patch, replace this function:
Now the selectors are not a table, but a simple item-list, allowing you to do whatever you like (in your theme's CSS). You might like to add some DIV with a CSS-class around the "theme('item_list', $veci)" if necessary, or change the final "clear:left", which I needed just to tune my graphics more than my theme can do.
--- You can also add a new field "Category description" to any of your views, to show categories your nodes belong to. But beware, that this field shows just ONE of the categories possibly assigned to a node, plus maybe (unless the filter "Node: distinct" is used) you'll get multiple occurences of the same node where more categories are assingned. You can also choose whether or not the category-description field links to the actual category - this is a brand new functionality, as compared to the SomebodySysop's code above. Another thing to keep in mind is, that for the sake of reducing the MySQL load, this is NOT the actual category title! So you should insert some sensible names of your categories also into the "description" field under "category information" (on the category's edit-page), and this one will be shown in your views.
Hope that this post might help someone to get the whole thing working without extensive searchs in this forum.
Great step by step
Thanks for taking the time to write this!
Old code...
But I'm not entirely sure whether this is up-to-date. I've rewritten some parts of the code later, as I realized more features needed to be corrected for my site's needs, and I would love to see it inside category module - but unfortunately it's far from being complete (i.e. ready for all the sensible use cases).
Try on your own risk ;)
thank you very much~~!
I creat a container views with your help,thanks a lot~~~~
Ron, excellent code, works perfect
I have tried it, it works fine, and has resolved a lot of headaches.
Many thanks to you for your excellent contribution.
abramo
working on 5.1?
testing on a fresh 5.1 I got the following error assigning a view:
warning: Invalid argument supplied for foreach() in W:\www\sites\all\modules\category\category_display\category_display.module on line 286
not shure if this as something to do with the changes?
Did you do that on 4.7 or 5.x?
Other question, how to prevent that the notes assigned so categories will be displayed on the container-level as well?
5.x-1.1 is still byggy, try 5.x-1.0
had numerous issues with 5.x-1.1, so I had to re-install 5.x-1.0 - and this code works fine.
Tagedelic issue
hello everybody,
I play with drupal for a while and I can't figure out one thing...
I use tagedelic module to display some tags in a cloud, it works fine, let's say one tag from the whole cloud is "restaurant" and when I click on it whole page with all restaurants is displayed, but I want do display those restaurant in a list, let's say list of restaurants names and tags added to individual restaurant like this:
-restaurant name1
tag1, tag2, tag3
-restaurant name2
tag1,tag2
-restaurant name3
tag1, tag3, tag4
-.....
.....
Anybody knows how to achieve this kind of view after clicking on "restaurant" tag from tagedelic cloud???
I use:
taxonomy.module
tagedelic.module
views.module
drupal4.7.4