0

I just want to wrap checkboxes like this in all the forms, automatically.

<div class="inline field">
  <div class="ui checkbox">
    <input type="checkbox">
    <label>Checkbox</label>
  </div>
</div>

How can I achieve this? Is there any hook available for this?

1
  • You could always use Drupal.behaviors with some jQuery to apply them. Commented Feb 4, 2014 at 19:21

1 Answer 1

2

Yes, there is a theme hook that will let you alter every instance of a checkbox. The two theme hooks you'll want to look at are:

  1. theme_checkbox() - https://api.drupal.org/api/drupal/includes!form.inc/function/theme_checkbox/7
  2. theme_checkboxes() - https://api.drupal.org/api/drupal/includes!form.inc/function/theme_checkboxes/7

Sample Code

Here's some sample code, available in the API doc links I provided above.

function theme_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'));

  return '<input' . drupal_attributes($element['#attributes']) . ' />';
}

You can override the markup returned by implementing this theme hook.

2
  • Good solution, just mention don't forget Clear Cache to see result :D. vote up
    – Yuseferi
    Commented May 24, 2016 at 9:17
  • How to override this?
    – W.M.
    Commented Sep 16, 2016 at 9:19

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.