How I can add views exposed filter as checkbox or radiobutton rather then select list

Comments

j_ten_man’s picture

Use hook_form_alter() on the views_exposed_form. You can then change the type from select to radios or checkboxes.

ashiwebi’s picture

Thank you

czeky’s picture

Hi, looking for the same thing, did You managed how to? Thank You

wintercat’s picture

I'm also trying to implement this, but I am new to php, and am struggling with the code. So far, I have created a basic custom module which is working. (The module currently uses hook_form_alter to remove a few fields from the node edit form, as described in a helpful tutorial at http://geeksandgod.com/tutorials/computers/cms/drupal/creating-simple-dr....)

I am now trying to add code to the mysite.module file to tell my Views multiselect taxonomy exposed filter to display as checkboxes.

I am getting the following error message:
Parse error: syntax error, unexpected ')', expecting ']' in...

My code is:

<?php
// $Id$
 
/*
 * @file
 * Drupal Module: mysite
 * Adds custom code specific to this Drupal 6 site.
 */
 
function mysite_form_alter(&$form, &$form_state, $form_id) {
 // print $form_id;
 // uncomment the line above and form names will appear at the top of your page
  switch ($form_id) {
    case 'makers_directory_listing_node_form':
      // remove some unwanted node editing fields
      $form['revision_information']['#access'] = 0;
      $form['menu']['#access'] = 0;
      $form['author']['#access'] = 0;
      // override node options permissions defined by 'administer nodes'
      $form['options']['#access'] = 1;
      $form['options']['status']['#access'] = 1;
      $form['options']['promote']['#access'] = 0;
      $form['options']['sticky']['#access'] = 0;
	// this next bit can be uncommented to show all the existing form elements, any of which can be altered.
	// $form['form_array'] = array(
	//   '#value' => '<pre>'. print_r($form, 1) .'</pre>',
	//   '#weight' => '99',
	// );
  }
	if ($form_id == 'views_exposed_form') {
	$form['taxonomy'][#type] = 'checkbox';
	);
  }
}

Any help is much appreciated! Thanks.

peter-boeren’s picture

Hi all,

showing exposed filters with a radio or a checkbox is something that is much needed by many people. I have started a module a month ago or so. It works and needs also tons of love and care before it is ready. But I've already received some positive feedback. You can give it a try (project page: Views filter pack)

wintercat’s picture

Thanks Peter. I'd seen your project page & the module sounds great.

I'm still hoping to get my snippet of code working in the meantime!

benschulkin’s picture

Change this:

if ($form_id == 'views_exposed_form') {
$form['taxonomy'][#type] = 'checkbox';
);

To this:

if ($form_id == 'views_exposed_form') {
$form['taxonomy']['#type'] = 'checkbox';

... that should get rid of your PHP error ... don't know if you'll get checkboxes though

wintercat’s picture

Thanks so much for the correction. You're right though - it got rid of the error, but just produced an extra checkbox rather than displaying my multiselect taxonomy as checkboxes... but at least I can move forward now. I have a few books on PHP so I'll keep experimenting :)

czeky’s picture

Hi, can't get it to work, I have custom CCK field called "field_district", but I'm lost how to find the right name for this select box

if ($form_id == 'views_exposed_form') {
$form['field_district']['#type'] = 'checkbox';
}

doesnt work

peter-boeren’s picture

Hey czeky,

have you already printed the $form-variable? Try something like this on the first line after mymodule_hook_form_alter(&$form, $form_state, $form_id)) {

var_dump($form);

or

print "<pre>";
print_r ($form);
print "</pre>";

this way you know how your form looks like before altering it. I hope this helps in your quest.

kind regards,

czeky’s picture

thanx, I'm a little bit more lost, there are more than 100 "field_district_xxx_yyy" items, could you please point me which one use?

looking like

[filter-field_district_value_many_to_one] => Array
(
[operator] => field_district_value_many_to_one_op
[value] => field_district_value_many_to_one
[label] => District
)

really thank you guys here

j_ten_man’s picture

100 seems like a lot. Normally you would do something like

  $form['field_district_value_many_to_one']['#type'] = 'checkboxes';

Not quite sure what or why you are seeing what you are...

j_ten_man’s picture

Can you save your results to a text file and then save it somewhere from your dump? I would like to take a look but we don't need all that information in this thread, just a link to download it would be great.

netentropy’s picture

nvm

mikeker’s picture

I just contributed a module called Better Exposed Filters which gives you a per-filter option of using the default select box or radio buttons/checkboxes. I've tested it with Taxonomy fields, CCK fields and core node fields and would appreciate any help with additional compatibility testing or suggestions.

I was having problems using Views Filter Pack with multiple exposed filters. BEF takes a different approach and comes at the problem from the theming side of things.

Hope that helps.

- Mike

- Mike

doublejosh’s picture

Even with Better Exposed Filters I couldn't get the "promoted to front page" filter to look/act as simply as I'd liked.

With (1) BEF, (2) AJAX turned on, (3) Checkboxes/radio buttons selected and (4) Optional set on... it showed three radio buttons for: "Any", "Yes" and "No".
This is probably useful for admins who want to hide featured to perhaps select others, but for normal users this is just too much.

I changed this to just an on/off checkbox with a custom label like this...

function myModule_form_views_exposed_form_alter(&$form, &$form_state) {
  // requires that the filter namespace value is "featured"
  unset($form['featured']['#options'][0]); // kill off the "No" option
  $form['featured']['#multiple']=true; // make this into an on/off choice
  if($form['#info']['filter-promote']['label']) $form['featured']['#options'][1]= $form['#info']['filter-promote']['label'];
  // above: Optionally, move the filter label into the "Yes" label.
  $form['#info']['filter-promote']['label']= ''; // kill off the master label used above
}
toutanne’s picture

Hi,

Does anyone found a solution for that. i'm looking for 3 days and no solution

regards