Drupal 6: Change Views Exposed Filter Form Select Element To A List Of Checkboxes

Whilst creating a view for a Drupal site the other day I created an exposed form but rather than the usual select boxes that get displayed I needed them to be a list of check boxes. Whilst searching about on Google I eventually found this article, which kindly provides a function that will do this. Here is the function in full.

/**
 * hack to make exposed dropdowns appear as checkboxes
 * (can't do it w/ form_alter b/c whole structure is different)
 * just render the options directly as checkboxes, pass the values the same as the SELECT,
 * tricks the form api and views to think it's a dropdown
 * [registered w/ hook_theme, toggled w/ #theme in form_alter]
 */
function theme_select_as_checkboxes($element) {
  $output = '';

  $selected_options = (array) $element['#post'][$element['#name']]; // the selected keys from #options

  foreach ($element['#options'] as $key => $value) {
    $id = $element['#id'] . '-' . $key; // custom

    // is this option selected?
    $selected = (array_search($key, $selected_options) !== false); // (returns key or false)

    $checkbox = '<input type="checkbox" '
      . 'name="'. $element['#name'] . '[]' .'" ' // brackets are key -- just like select
      . 'id="'. $id .'" '
      . 'value="'. $key .'" '
      . ($selected ? ' checked="checked" ' : ' ')
      . drupal_attributes($element['#attributes']) .' />';

    $output .= '<label class="option" for="'. $id .'">' . $checkbox .' '. $value .'</label>' . "\n";
  }
  return theme_form_element($element, $output); // wraps it neatly
}

The only problem was that the author didn't also include the rest of the functions needed to integrate this function into the site. As a result there are a lot of comments asking how to do this. After a bit of tinkering I managed to get this function working so I thought I would put the solution here in case it is of use to anyone. Before you start it is important to note that all of these functions must sit in a module. They currently won't be picked up by Drupal if you put them in your template.php file.

The first thing we need to do (after the view has been created) is create a theme hook so that we can let Drupal know about the theme_select_as_checkboxes() function so that it can use it in a theme() call.

/**
 * Implementation of HOOK_theme.
 *
 * @return array An array of theme hooks.
 */
function mymodule_hooks_theme() {
  return array(
    'select_as_checkboxes' => array(
      'function' => 'theme_select_as_checkboxes'
    ),
  );
}

The final step is to include a hook_form_alter() function with a name in the form of:

<modulename>_form_<formname>_alter()

The exposed form name is views_exposed_form, so in a module called "mymodule" the function would be called modulename_form_views_exposed_form_alter(). All this function does is to alter the form so that the any elements we need to change to checkboxes are given the correct theme.

/**
 * Implementation of HOOK_form_alter().
 *
 * @param array $form        The form
 * @param string $form_state The current state of the form.
 */
function mymodule_hooks_form_views_exposed_form_alter(&$form, &$form_state)
{
    // We only want to change a certain form, so stop Drupal looking at any
    // other exposed view form
    if ($form['#id'] == 'views-exposed-form-myview-page-1') {
        // Make the select box appear as a list of checkboxes
        $form['formelement']['#theme'] = 'select_as_checkboxes';
        $form['anotherformelement']['#theme'] = 'select_as_checkboxes';
    }
}

Once everything is in place you should see your select element being printed out as a list of checkboxes. Everything about the form will work in exactly the same way, the display layer is all that has been changed.

An alternate solution to this is to use the Better Select module. This module will change all multi-select form elements into a list of checkboxes anywhere that they are found in Drupal. Better Select is an easier solution to implement, but might not be what you want to do.

Comments

Hello there, I noticed you are using Drupal on your website and was wondering why you don't use WordPress instead?
Permalink
Awesome !
Permalink
Because it's prescriptive and limited?
Permalink
Hi, Can we do the same in drupal 8?
Permalink

This write-up is amazing. Why? The first code listing is from the cited article. My advice? Use the original article info and then read this to find out the missing elements.

Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
6 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.