Restricting list of exposed filter options
Campsoupster - April 16, 2007 - 06:36
| Project: | Views Filter Block |
| Version: | 5.x-1.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
Jump to:
Description
This is a a great module. However, I'd like to be able to limit the list of available options that appear in an exposed filter.
I can see how to do this with regular exposed filters in Drupal 5, but can't seem to figure out how to theme the exposed filters that this module produces. (see: http://drupal.org/node/84124).
Any ideas?
-s

#1
I implemented this in the 4.7 and only recently ported it to the 5.x (views_filterblock.module 1.4.2.11). You need the 5.x.dev release. Use You need the 5.x.dev release.
#2
Hey Doug,
I'm using the April 12th 5.x-1.x-dev release. Here's what I have in my template.php:
<?php
function phptemplate_views_display_filters_VIEWNAME($view) {
$form = drupal_retrieve_form('views_filters', $view);
// filter1 or filter2 or filter3 depending on which filter you need
$options = $form['filter0']['#options'];
// ... code to remove options you don't want ...
foreach ($options as $option => $text) {
if (!in_array($option, array('campsite', 'page', 'blog'))) {
unset($options[$option]);
}
}
$form['filter0']['#options'] = $options;
drupal_process_form('views_filters', $form);
return drupal_render_form('views_filters', $form);
}
?>
This works just fine with the normal filters, but not when I use the filter_block. Any suggestions regarding what I'm doing wrong?
A appreciate the support. This is a great module!
-s
#3
It looks like one of my sentences was chopped during my editing. Sorry. I meant to say
That is, you have two options. You can modify this in a custom module or you can modify it in the theme.
To modify it in a module:
<?php
function yourmodule_form_alter($form_id, &$form) {
if ($form_id == 'views_filterblock_output') {
// filter1 or filter2 or filter3 depending on which filter you need
$options = $form['filter0']['#options'];
// ... code to remove options you don't want ...
foreach ($options as $option => $text) {
if (!in_array($option, array('campsite', 'page', 'blog'))) {
unset($options[$option]);
}
}
$form['filter0']['#options'] = $options;
}
}
?>
To modify it in a theme:
<?php
function phptemplate_views_filterblock_output($form) {
// filter1 or filter2 or filter3 depending on which filter you need
$options = $form['filter0']['#options'];
// ... code to remove options you don't want ...
foreach ($options as $option => $text) {
if (!in_array($option, array('campsite', 'page', 'blog'))) {
unset($options[$option]);
}
}
$form['filter0']['#options'] = $options;
return drupal_render($form);
}
?>
Warning... this code is typed off the top of my head and untested. Please let me know how close it is, because I'll probably turn this it into an article.
#4
Oops, I just realized that the form_alter stuff doesn't work as advertised. You can still use the theme code.
#5
Continuing to experiment. I just tried:
<?phpfunction phptemplate_views_filterblock_output($form) {
$options = $form['filter0']['#options'];
foreach ($options as $option => $text) {
$output .= $option . "<br />";
}
return $output;
?>
Just so that I could see output.
That removes the fieldset around the select boxes, but doesn't change any of the output in those boxes.
If instead I use:
<?phpfunction phptemplate_views_filterblock($form) {
$options = $form['filter0']['#options'];
foreach ($options as $option => $text) {
$output .= $option . "<br />";
}
return $output;
?>
I can get a list of the first filter on the form...
I'll try a hook form alter approach tonight.
Thanks for working with me on this.
-s
#6
Look at my example again. When you theme something, you are responsible for doing the output. Did you add:
$form['filter0']['#options'] = $options;return drupal_render($form);
#7
Yeah, I tried that too.
I even tried just killing
<?phpfunction theme_views_filterblock_output($form) {
return drupal_render($form);
}
?>
by adding:
<?phpfunction phptemplate_views_filterblock_output($form) {
}
?>
But the most change I could get was removing the fieldset around the select box...
Any more ideas? Sorry, I'm pushing my limited php knowledge on this one and will do all I can to figure it out on my own.
-s
#8
This works because I've used it for a couple of projects.
Here's the template.php for 5.x project that does it, look at psnzen_views_filterblock_output:
https://svn.civicactions.net/repos/psn/trunk/themes/psnzen/template.php
The way phptemplate works, you can name your function phptemplate_views_filterblock_output or yourtheme_views_filterblock_output. As you can see from this example, I prefer to use the theme name.
Here's the template.php for a 4.7.x project, that's pretty much the same as the 5.x project:
https://svn.civicactions.net/repos/blogher/blogher/trunk/themes/blogherz...
Did you try the theming code in comment-3
#9
Doh! Those are both 5.x projects. Here's a 4.7.x project:
https://svn.civicactions.net/repos/mojo/trunk/themes/mojotheme/template.php
#10
yep, I copied and passsed the function into my template.php exactly as you wrote it there. I even added some bad php in the function to throw an error message so that I could know for sure that it was being called. The function is being called, but it doesn't affect what's in the filter options list. I tried using "filter0", "filter1", etc., no affect.
I'll keep trying. I'm usually pretty good a phptemplating. I can't figure out what I'm doing wrong here. Thanks again for all the help. -s
#11
Hey! I got this working. Thanks for the examples. What was causing the problem was the fact that the fieldset wasn't defined...
This worked:
<?php
function phptemplate_views_filterblock_output($form) {
// filter1 or filter2 or filter3 depending on which filter you need
$options = $form['fieldset0']['filter0']['#options'];
// ... code to remove options you don't want ...
foreach ($options as $option => $text) {
if (in_array($option, array('blog', 'page', 'forum', 'event', 'image', 'book'))) {
unset($options[$option]);
}
}
$form['fieldset0']['filter0']['#options'] = $options;
return drupal_render($form);
?>
Next, I'm interested in trying to incorporate http://drupal.org/node/84124#comment-232177, which might eliminate the need to define the fieldset and filter id explicitly.
Thanks for all your help. Posting those template.php files really helped!!
-s
#12
#13
Sometimes it will be nessecary to unset the key. In case of Taxonmy terms the array passed on to the filterblock lookes like:
<option value="1">First item</option><option value="2">Second item</option>In this case you must unset the key:
<?phpforeach ...... {
if (in_array($options, array('1' , '2'))) { // NOT array('First item' , 'Second item')
unset
}
?>
#14
clivesj, your solution worked for me! Thanks!