Views Exposed Filters as list of links instead of select
Using Drupal 6, Views 2 with exposed filters, I'm trying to determine the best way to convert the select list to a list of links, each with a count of matching nodes. For instance instead of what I get by default, as a select list:
<select name="state" class="form-select" id="edit-state开发者_如何学运维" >
<option value="All" selected="selected"><Any></option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
...
</select>
I'd like to get something like
<p>Restrict results by state:<br />
<a href="...">Alabama (15)</a><br />
<a href="...">Alaska (7)</a><br />
...
</p>
With each link showing the count in parentheses and drilling down in the same way that selecting one of the options in the first code block would.
Could you provide any pointers on how to approach this? Thanks.
You may want to look at the Better Exposed Filters module for views. The 7.x version has this functionality and there is a patch for the 6.x version as well which can be found at:
http://drupal.org/node/1159232#comment-4710372
Solr is slick, but also check out the summary option in Views. You can see an example of this in the arguments section of the "Archive list" view that comes with Views.
There are some other faceted browsing modules on drupal.org as well but I haven't got any personal experience with them.
What you are after is not something views was meant to do. It probably would be possible to do, but it will be slow and hard to implement.
Instead you should take a look at the module Acquia has made for Apache Solr. It does faceted search, which is what you really are trying to make. If your case is simple enough you might not need something so fancy. But it's just a matter of time before you do. Performance will become a big issue since you would need to do a query per state to get the counts.
Check this issue http://drupal.org/node/891974. The code is for the number of posts per page, but can easily be done to other filters too.
<code>
Please go through the following steps to display the select list as year links
//hook_theme
function mymodule_theme($existing, $type, $theme, $path){
return array(
'list_items' => array(
'template' => 'list_items', //tpl to display them as list
'path' => $path . '/templates',
'type' => 'module',
'variables' => array(
'list' => NULL,
'current' => NULL
),
),
);
}
/*
hook_form_alter
*/
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'views_exposed_form') {
$current = '';
$ranges = explode(':',$form['date_filter']['value']['#date_year_range']);
foreach($ranges as $key => $range) {
$ranges[$key] = date('Y', strtotime($range.' years', strtotime(date('Y-m-d H:i:s'))));
}
$startYear = $ranges[0];
$endYear = $ranges[1];
if($ranges[0] > $ranges[1]) {
$endYear = $ranges[0];
$startYear = $ranges[1];
}
$items = array();
$endYear = (int)$endYear;
$startYear = (int)$startYear;
if(empty($_REQUEST['date_filter']['value']['year'])) {
$items[] = 'All years';
} else {
$items[] = l('All years', current_path(),array('query' => array("date_filter[value][year]" => '')) );
}
for($i=$endYear; $i>=$startYear; $i--) {
if($_REQUEST['date_filter']['value']['year'] == $i) {
$items[$i] = $i;
} else {
$items[$i] = l($i, current_path(), array('query' => array("date_filter[value][year]" => $i)));
}
}
$list = theme('list_items', array('list' => $items, 'current' => $_REQUEST['date_filter']['value']['year']));
$form['html'] = array(
'#type' => 'markup',
'#markup' => $list,
);
}
}
/*
templates/list_items.tpl.php
*/
<ol><?php foreach($list as $key => $value) { if($current == $key || empty($current)) { ?><li class="active"><?php print $value; ?></li>
<?php } else { ?> <li><?php print $value; ?></li>
<?php } } ?></ol>
//mymodule.info
name = Filters customization
description = Filters customization
version = VERSION
core = 7.x
dependencies[] = date
</code>
精彩评论