Drupal Views Group by Field with Counts and Links - A Better Way
Alas, I came across a better way to take a bunch of rows from views, group them by a particular field, and provide a count for each.
Here is a basic example for Drupal 6 (probably very similar for Drupal 7).
Basic Settings
Style: HTML List
Fields
Node id (exclude from display)
Node title
Arguments
Node title
Action to take if argument is not present: Summary, sorted ascending
Style: Unformatted
Settings: Display record count with link
Conclusion
The trick here is setting up the argument to match that of the field, then choosing to use a 'Summary, sorted ascending' for the 'Action to take if argument is not present', then adjusting the settings for the argument so it will 'Display record count with link'.
This technique will provide a grouped list with counts and a link. However, for my case (and in the past) the link always comes out as a link to the base path, i.e. '/'. I have searched and searched to figure out how to set the path to use for the link in the 'Display record count with link' but I haven't found anything. So, we'll revert to good ol' tpl.php files.
Themeing the link
Grab a copy of the the template file in the views module (views/theme/views-view-summary-unformatted.tpl.php) and place it in your site's theme's directory (sites/all/themes/my_theme). Rename the file so it matches the file naming convention needed for views to discover your template, for example rename the file to: views-view-summary-unformatted--my-view.tpl.php
Flush the theme registry cache to have Drupal discover the new template file. Under your view's 'Basic Settings', 'Theme: Information', views will NOT discover your new template file, I'm not sure why it doesn't discover it. Additionally views doesn't even provide naming conventions for that type of template file, weird. But anyways, this will still work.
Now to actually set the path of the link to the way we want... just inside the 'foreach' loop inside your new template file, you can set the '$row->url' to whatever you'd like. For example:
$row->url = base_path() . "node/" . $row->nid;
Please use the Devel module to dsm or dpm the value of $row during each iteration. That way you can look to make sure you are accessing the correct variable name inside the $row variable.
Comments
Daniel (not verified)
Wed, 10/17/2012 - 05:02
Permalink
How would I do if I want to
How would I do if I want to display also other fields aprt from the node title? Maybe I also want to display the description and field X for each title. When I add the argument, only the field I've selected for the argument is displaying.
tyler
Wed, 10/17/2012 - 10:34
Permalink
I would just try adding them
I would just try adding them to the field listing and see what happens. But I am guessing for every field you use, and for each node that has a different value in the field compared to the other nodes of that content type, then you'll get a new row and count for each grouping instance.
I really only use the details of this blog post to get a simple list of node titles with their respective counts. If I can't get views to give me the data I want, then I'll typically make a custom block in my module and then hack up an SQL query to get what I want.