You are here

function bef_checkbox in Better Exposed Filters 8.3

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

Build a BEF checkbox.

Parameters

array $element: Original <select> element generated by Views.

string $value: Return value of this checkbox option.

string $label: Label of this checkbox option.

bool $selected: Checked or not.

Return value

[type] HTML to render a checkbox.

See also

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

1 call to bef_checkbox()
theme_select_as_tree in ./better_exposed_filters.theme
Themes a taxonomy-based exposed filter as a nested unordered list.

File

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

Code

function bef_checkbox($element, $value, $label, $selected) {
  $value = check_plain($value);
  $label = filter_xss_admin($label);
  $id = drupal_html_id($element['#id'] . '-' . $value);

  // Custom ID for each checkbox based on the <select>'s original ID.
  $properties = array(
    '#required' => FALSE,
    '#id' => $id,
    '#type' => 'bef-checkbox',
    '#name' => $id,
  );

  // Prevent the select-all-none class from cascading to all checkboxes.
  if (!empty($element['#attributes']['class']) && FALSE !== ($key = array_search('bef-select-all-none', $element['#attributes']['class']))) {
    unset($element['#attributes']['class'][$key]);
  }

  // Unset the name attribute as we are setting it manually.
  unset($element['#attributes']['name']);

  // Unset the multiple attribute as it doesn't apply for checkboxes.
  unset($element['#attributes']['multiple']);

  // @TODO: Render in a template
  $checkbox = '<input type="checkbox" ' . 'name="' . $element['#name'] . '[]" ' . 'id="' . $id . '" ' . 'value="' . $value . '" ' . ($selected ? 'checked="checked" ' : '') . drupal_attributes($element['#attributes']) . ' />';
  $properties['#children'] = "{$checkbox} <label class='option' for='{$id}'>{$label}</label>";
  $output = theme('form_element', array(
    'element' => $properties,
  ));
  return $output;
}