Skip to: Site menu | Main content

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:

Base view display

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:

function view_title_count_views_pre_view(&$view, &$items) {
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:

View with title count

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.

AttachmentSize
view_title_count.zip783 bytes
Submitted by michael on Fri, 11/14/2008 - 3:23pm
Filed under:

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

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

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <table> <tr> <td> <th> <div> <span> <p> <br> <blockquote> <hr>
  • Lines and paragraphs break automatically.

More information about formatting options