Adding Node Counts to View Titles in Views 1.x
It is often desirable to add a node count to the title of a view to help users gauge how many nodes have been returned - especially when using exposed filters.
In Views 1.x, this is a fairly simple process using the "hook_views_pre_view(&$view, &$items)" function (documentation).
Let's say you have a view that displays titles and authors all content of type "story" on your site in table format. Furthermore, you've exposed the "Node: Author Name" field as an exposed filter for the view:

As the user plays with the "Author Name" exposed filter, as expected the title of the view doesn't change.
To modify the title, we can implement the hook_views_pre_view function as follows:
if (($view->name == 'test_view')) {
$view->page_title .= ' ('. $view->total_rows .')';
}
}
For this example, I created a new module called "view_title" and a view called "test_view". The "total_rows" value is part of the $view array, so it is as simple as appending the total_rows to the existing page_title. The result is then:

While it is possible to affect the view title without writing code when using arguments, I don't believe it is possible to add the node count to the view's title regardless of if you're using arguments or exposed filters.
Feel free to download the "view_title_count" module I wrote for this post below.
As far as doing this same type of thing in Views 2.x, I haven't yet figured it out - it does appear that the "total_rows" variable is not used in Views 2.x, so an alternative way will need to be found. If you know how to do this, feel free to leave it in the comments.
| Attachment | Size |
|---|---|
| view_title_count.zip | 783 bytes |

Views 2 ?
An idea for count the nodes a views in the second version of view ?
Thanks !
@akahn - While I haven't
@akahn -
While I haven't tried it, if you really wanted to do this at the theme layer, I suspect you could do it via a phptemplate_views_view override.
Copy the contents of theme_views_view into your template.php file as phptemplate_views_view, and change the lines
if ($type == 'page') {
drupal_set_title(filter_xss_admin(views_get_title($view, 'page')));
views_set_breadcrumb($view);
}
to
if ($type == 'page') {
drupal_set_title(filter_xss_admin(views_get_title($view, 'page')) . ' (' . count($nodes) . ')');
views_set_breadcrumb($view);
}
Again, totally untested, but I imagine it would work.
Thanks for this idea. I have
Thanks for this idea. I have created a module that adds a checkbox to the views edit form to turn this on and off, and am finding it really useful.
more difficult
In my opinion, it would be more difficult to do in the theme layer than with a small module like I showed. I believe you'd have to do it in your template.php file and modify the $vars['title'] variable in the _phptemplate_variables() function. You'll have to figure out how to get the $view object to access the "total_records" value.
Doing it as a module allows you to leverage the Views API and do it in (probably) less code.
-mike
How could you do this at the
How could you do this at the theme layer to provide a template variable that contains the count?
Post new comment