You are here

function theme_select_as_checkboxes in Better Exposed Filters 7.3

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 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.

Return value

string HTML representing the form element.

See also

http://api.drupal.org/api/function/theme_select/7

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 checkboxes enclosed in a collapsible fieldset.

File

./better_exposed_filters.theme, line 73
Provides theming functions to display exposed forms using different interfaces.

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']) ? empty($element['#default_value']) ? array() : $element['#default_value'] : $element['#value'];
  if (!is_array($selected_options)) {
    $selected_options = array(
      $selected_options,
    );
  }

  // Grab exposed filter description.  We'll put it under the label where it
  // makes more sense.
  $description = '';
  if (!empty($element['#bef_description'])) {
    $description = '<div class="description">' . $element['#bef_description'] . '</div>';
  }
  $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)) {
      $slice = array_slice($elem->option, 0, 1, TRUE);
      $option = key($slice);
      $elem = current($slice);
    }

    // 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 .= theme('bef_checkbox', array(
        'element' => $element,
        'value' => $key,
        'label' => $value,
        'selected' => array_search($key, $selected_options) !== FALSE,
      ));
    }
    if ($is_optgroup) {

      // Close group and item <div>s.
      $output .= '</div></div>';
    }
  }
  $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['#bef_select_all_none_nested'])) {
    $attributes['class'][] = 'bef-select-all-none-nested';
  }
  if (!empty($element['#attributes']['class'])) {
    $attributes['class'] = array_merge($element['#attributes']['class'], $attributes['class']);
  }
  return '<div' . drupal_attributes($attributes) . ">{$description}{$output}</div>";
}