Select fields as exposed filters rather than text input fields

Events happening in the community are now at Drupal community events on www.drupal.org.
chrisrikli-gdo's picture

Is there a way (aside from using hook_form_alter or something similar) to have an exposed filter be a select element?

I have a view in which one of the exposed fields is "Category" and there are only 9 or 10 distinct values in the database field. Rather than having to type in one of the categories, I'd like for users to simply choose from a select box.

Comments

any luck with this?

stevepofd's picture

i have the exact same needs... i have been looking through the source code and as well some exported tree data from an existing view with exposed fields. i compared a field that outputs a select box, and one that outputs a text box. there was not much difference... when i tried swapping the values around i had no luck...

any help would be great! i'm gonna keep trying other methods, i'll report back if i find anything ;)

-steve

try this

jainrutgers's picture

This method will allow you to select a category for particular node when you create a content. You can set the permission not to display that field to any user but just you for the views functionality.

1) Create a content type. Add a cck text field to it. For widget option choose 'Select list' which will be there by default.

2) On setting page of that particular field you will have option to give allowed values. Put all the Categories name that you want to be exposed to user (turn off your any text editor if its ON before implementing this step)

3) Create a new view and in filter select the filed that you created in content type with allowed values. (There will be two version of that filed. choose the Allowed one). And choose expose the field to user.

4) In this way you will get a drop down menu and from which user can select what he want

For demo check this http://bio-1.rutgers.edu/match_job

Chetan

i get what you mean...

stevepofd's picture

thanks for the quick response, you are correct this will work.
however, i should have been more specific.
i want to be able to have a select box that will be populated by all of the possible values for that field in a specific content type.
these values would be input by users when creating new nodes of that content type.

does that make any sense? is there someway to override the automatic selection of widget type for the exposed views?

  • steve

Is there a way to apply this

elektrorl's picture

Is there a way to apply this to a select list of "node titles" in a View?

same problem: title as an

momper's picture

same problem: title as an exposed filter as select in a view

*reference filter

curtaindog's picture

The node and user reference fields appear to have this functionality (ie a dynamic select list driven by a query) so they might be a good place to start poking around to see what to do. I'm currently investigating whether there is (or one can be implemented easily) a generic view filter where the output of one view can be piped into a filter of another. Seems like this would hit a few nails.

Thanks for the hint, here's how I did it

fluxsauce's picture

<?php
/**
* Filter handler for NODE_TYPE
*/
class MODULE_handler_filter_NODE_TYPE extends views_handler_filter_many_to_one {
 
/**
   * Set selectable option values
   * @see views_handler_filter_in_operator::get_value_options()
   */
 
function get_value_options() {
   
// Get option values
   
$NODE_TYPES = array();
   
$sql_query  = 'SELECT {node}.nid ';
   
$sql_query .= 'FROM {node} ';
   
$sql_query .= 'WHERE {node}.type = "NODE_TYPE" ';
   
$sql_query .= 'AND {node}.status = "1" ';
   
$result = db_query($sql_query);
    while (
$row = db_fetch_object($result)) {
     
$node = node_load($row->nid);
     
$NODE_TYPES[$node->nid] = $node->title;
    }
   
// Set options
   
$this->value_options = $NODE_TYPES;
  }
}
?>

Also, if you use optgroups for the select, it'll mess up the rendering as it counts the number of elements in the options array, but doesn't check to see how deep the options are.

<?php
 
/**
   * Override size of select options
   * @see views_handler_filter_many_to_one::value_form()
   */
 
function value_form(&$form, &$form_state) {
   
parent::value_form($form, $form_state);
   
// Override the size of the select options to compensate for optgroups
   
$form['value']['#size'] = 8;
  }
?>

Views Developers

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: