0

I want to add the attribute data-toggle="toggle" to a checkbox field.

I thought I could use the hook theme_checkbox which is:

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']) . ' />';
}

So I customised it to:

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';
  }

  //*******Added 3 lines below*******
  if ($element['#attributes']['id'] = 'edit-profile-carer-field-profile-active-und') {
      element_set_attributes($element, array('#attributes' => 'data-toggle="toggle"'));
  }

  _form_set_class($element, array('form-checkbox'));

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

However, instead of just adding the attribute data-toggle="toggle" to the checkbox field with the id edit-profile-carer-field-profile-active-und, it is adding the attribute to all the checkboxes on the form.

Can someone tell me what I am doing wrong? How can I target just the the checkbox I want? Or do I need to use another approach to do it?

0

I'd recommend you do this via a form alter. But that aside, the reason your code doesn't work is that you're not doing your comparison correctly.

if ($element['#attributes']['id'] = 'edit-profile-carer-field-profile-active-und') {

Should be:

if ($element['#attributes']['id'] == 'edit-profile-carer-field-profile-active-und') {

And you're also not setting the attribute correctly. So the final function should be:

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';
  }

  //*******Added 3 lines below*******
  if ($element['#attributes']['id'] == 'edit-profile-carer-field-profile-active-und') {
      $element['#attributes']['data-toggle'] = 'toggle';
  }

  _form_set_class($element, array('form-checkbox'));

  return '<input' . drupal_attributes($element ['#attributes']) . ' />';
}
4
  • Many thanks Aaron for your help. It is all working now. I did initially try hook_form_alter, but that added the attribute to the field containing div rather than the actual input element.
    – Ben
    Aug 12 '15 at 8:22
  • You could have done it with a form alter but would have had to identify the right element in the massive array tree. Anyhow...if my answer helped, @Ben, please click the check mark next to my answer to accept it.
    – Aaron
    Aug 12 '15 at 12:33
  • 1
    I clicked the check mark next to your answer when I added my previous comment. It is now showing green for me. Is it not showing as checked for you? Thanks again.
    – Ben
    Aug 12 '15 at 17:18
  • Ah, so it is. Weird, wasn't showing earlier. Caching...? Thanks!
    – Aaron
    Aug 12 '15 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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