1

I need something like a filter on checkboxes #options in my form built via Form API. Is there a way to group checkboxes or add a class to filter them via js front side?

In the documentation options are simply 'value' => 'label'. Can I filter or group them?

2 Answers 2

1

To modify how drupal outputs checkboxes, override the default theme_checkbox function found in includes/form.inc. This allow to you add classes and all other stuff. I simply copied the function and changed the last 8 lines:

/* Override theme_checkbox to display titles as facebook-style beautytips  */
function MYTHEME_checkbox($variables) {
  $element = $variables['element'];
  $element['#attributes']['type'] = 'checkbox';
  element_set_attributes($element, array('id', 'name', '#return_value' => 'value'));

  // Unchecked checkbox has #value of integer 0.
  if (!empty($element['#checked'])) {
    $element['#attributes']['checked'] = 'checked';
  }
  _form_set_class($element, array('form-checkbox'));

  if (substr($element['#name'],0, 13) == "field_MYFIELD") :
    $element['#attributes']['class'][] = 'beautytips';
    return 
      '<input title="' . $element['#title'] .'" ' . 
      drupal_attributes($element['#attributes']) . ' />';
  else: // Return the regular value
    return '<input ' . drupal_attributes($element['#attributes']) . ' />';
  endif;
}

Thanks @clive for the inspirtation.

0

If you want to display check boxes options as optgroup, then Drupal already provide this functionality. You need to set multidimensional array for #options field.

Example:

$options = = array (

   'parent1' => array(
      3 => 'child1',
      4 => 'child2',
      5 => 'child3',
    ),
    'parent2' => array(
      8 => 'child1',
      9 => 'child2',
    ),
);


$form['FIELDNAME'] = array(
    '#type' => 'checkboxes',
    '#title' => 'Checkbox'
    '#options' => $options,
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.