A Couple of Views Solutions: Record Counts and Checkboxes
We recently launched the Member Directory at AASHE. The directory is powered by Views Exposed Fliters, Location, Migrate, and Better Exposed Filters. The data for our directory is coming via Salesforce into a separate MySQL database.
When working on the directory, I ran into a couple of unique requirements for Views.
Including a Count in the View
First, we wanted to have an active count of our Members and Associate Members that updated when users searched for new terms, locations, etc. I addressed this by putting the following PHP code into the header of the view.
<?php
//all full members
$members_total = count(views_get_view_result('member_directory', 'page_1','Full Member'));
//all associate members
$associate_total = count(views_get_view_result('member_directory', 'page_1','Associate Member'));
print '<ul class="legend">';
print '<li class="full-member"><a href="#"><span class="member-title">Members</span><span class="count">' . $members_total . '</span><span class="description">Institution of higher education</span></a></li>';
print '<li class="associate-member"><a href="#"><span class="member-title">Associate Members</span><span class="count">' . $associate_total . '</span><span class="description">Business, non-profit, other</span></a></li>';
print '</ul>';
?>In views_get_view_result, I am passing an argument "Full Member" or "Associate Member" to the view. While that argument isn't displayed anywhere in the interface of the Member Directory, it is the best way I found to segment out and get a count of the two types of members. Gathering the numerical count was done with "count()".
There are lots of other ways to embed a count into a view. If I just wanted count all of the active records without distinguising between member types, I could used "$view->total_rows".
Location Dependent Selection
The second problem I ran into was overriding Location module's requirement that a country be selected before a State/Province. Since the vast majority of our users would be selecting a location in the United States of Canada, I decided that the best method was to automatically set the Country as USA or Canada if State/Province was selected. I played around with a bunch of different implementations of this and would up using hook_views_pre_build and $_GET. I'm sure there's a much more elegant solution for this.
<?php
/**
* Implementation of hook_views_pre_build.
*
* Setting us by default when country but not state is selected
*
*/
function form_overrides_views_pre_build(&$view) {
if ($view->name=='member_directory') {
if (isset($_GET['province'][0]) && (!isset($_GET['country'][0]))) { //province is set and we are on the right view
$_GET['country'][0] = 'us';
$_GET['country'][1] = 'ca';
drupal_set_message("No country selected. Setting United States of America and Canada as default.");
}
}
}?>


