You are here

function theme_select_as_checkboxes in Better Exposed Filters 7

Same name and namespace in other branches
  1. 6.3 better_exposed_filters.theme \theme_select_as_checkboxes()
  2. 6 better_exposed_filters.theme \theme_select_as_checkboxes()
  3. 6.2 better_exposed_filters.theme \theme_select_as_checkboxes()
  4. 7.3 better_exposed_filters.theme \theme_select_as_checkboxes()

Themes a select element as a set of checkboxes

Parameters

array $vars - An array of arrays, the 'element' item holds the properties of the element.: Properties used: title, value, options, description

Return value

HTML string representing the form element.

See also

theme_select(), http://api.drupal.org/api/function/theme_select/6

1 string reference to 'theme_select_as_checkboxes'
better_exposed_filters_theme in ./better_exposed_filters.module
Implements hook_theme().
1 theme call to theme_select_as_checkboxes()
theme_select_as_checkboxes_fieldset in ./better_exposed_filters.theme
Themes a select element as a collection of checkboxes enclosed in a collapsible fieldset

File

./better_exposed_filters.theme, line 51

Code

function theme_select_as_checkboxes($vars) {
  $element = $vars['element'];
  if (!empty($element['#bef_nested'])) {
    if (empty($element['#attributes']['class'])) {
      $element['#attributes']['class'] = array();
    }
    $element['#attributes']['class'][] = 'form-checkboxes';
    return theme('select_as_tree', array(
      'element' => $element,
    ));
  }

  // the selected keys from #options
  $selected_options = empty($element['#value']) ? $element['#default_value'] : $element['#value'];

  // Grab exposed filter description.  We'll put it under the label where it makes more sense.
  $description = '';
  if (!empty($element['#description'])) {
    $description = '<div class="description">' . $element['#description'] . '</div>';
    unset($element['#description']);
  }
  $output = '<div class="bef-checkboxes">';
  foreach ($element['#options'] as $option => $elem) {
    if ('All' === $option) {

      // TODO: 'All' text is customizable in Views
      // No need for an 'All' option -- either unchecking or checking all the checkboxes is equivalent
      continue;
    }

    // Check for Taxonomy-based filters
    if (is_object($elem)) {
      list($option, $elem) = each(array_slice($elem->option, 0, 1, TRUE));
    }

    /*
     * Check for optgroups.  Put subelements in the $element_set array and add a group heading.
     * Otherwise, just add the element to the set
     */
    $element_set = array();
    $is_optgroup = FALSE;
    if (is_array($elem)) {
      $output .= '<div class="bef-group">';
      $output .= '<div class="bef-group-heading">' . $option . '</div>';
      $output .= '<div class="bef-group-items">';
      $element_set = $elem;
      $is_optgroup = TRUE;
    }
    else {
      $element_set[$option] = $elem;
    }
    foreach ($element_set as $key => $value) {
      $output .= bef_checkbox($element, $key, $value, array_search($key, $selected_options) !== FALSE);
    }
    if ($is_optgroup) {
      $output .= '</div></div>';

      // Close group and item <div>s
    }
  }
  $output .= '</div>';

  // Fake theme_checkboxes() which we can't call because it calls theme_form_element() for each option
  $attributes['class'] = array(
    'form-checkboxes',
    'bef-select-as-checkboxes',
  );
  if (!empty($element['#bef_select_all_none'])) {
    $attributes['class'][] = 'bef-select-all-none';
  }
  if (!empty($element['#attributes']['class'])) {
    $attributes['class'] = array_merge($element['#attributes']['class'], $attributes['class']);
  }
  return '<div' . drupal_attributes($attributes) . ">{$description}{$output}</div>";
}