2

Is there a way to set descriptions to each checkbox option? What I would like is for Option #1 to display and then directly below it a description for it, then Option #2 along with its description. I have:

 $form['field_options'] = array(
                    '#type' => 'checkboxes',
                    '#options' => array(
                        0 => t('Option #1'),
                        1 => t('Option #2')),
                    '#title' => t('Select Options'),
                    '#description' => t('Entering a description here will display below the last item in the array'),
                    '#default_value' => array(0)
            );

Also, for some reason $form_state['values']['field_options'] in function hook_node_submit() shows

Array
(
    [0] => 0
    [1] => 0
)

shouldnt it show what the states of the checkboxes are like this?:

Array
(
    [0] => 1
    [1] => 0
)

Thanks

4 Answers 4

1

The #title will be displayed as a description next to the checkbox, description below it if you use single checkboxes in a for-loop.

You need to change it a bit:

$options = array('Option 1' => t('Description1'), 'Option2' => t('Description 2');
$i = 1;
foreach ($options as $title => $description) {
    $form['field_options']['option_' . $i]= array(
      '#type' => 'checkbox',
      '#title' => $title,
      '#description' => $description,
      '#default_value' => 0,
    );
  $i++;
}

Set the value with #default_value = 0; (not an array).

About the 0 value in the array: From drupal.org Form API about checkboxes: Description: Format a set of checkboxes. #options is an associative array, where the key is the #return_value of the checkbox and the value is displayed. The #options array cannot have a 0 key, as it would not be possible to discern checked and unchecked states.

3
  • That will give you exactly one checkbox every time...
    – Clive
    May 14, 2014 at 17:10
  • Ah i see there is a problem with this one, forgot to iterate he fieldname, i will update some (it will be overwriting).
    – Marcel
    May 14, 2014 at 17:30
  • That's definitely one way to do it, but it's possible to override the default behaviour. I've chucked some code in an answer, something similar to that should work
    – Clive
    May 14, 2014 at 20:32
1

The individual checkboxes are extracted in form_process_checkboxes(), so I guess something like this would work (un-tested):

function MYMODULE_form($form, &$form_state) {
  $options = array(1 => t('Option #1'), 2 => t('Option #2'));

  // Keyed the same as $options
  $descs = array(1 => t('Desc #1'), 2 => t('Desc #2'));

  $form['field_options'] = array(
    '#type' => 'checkboxes',
    '#options' => $options
    '#title' => t('Select Options'),
    '#description' => t('Entering a description here will display below the last item in the array'),
    '#default_value' => array(1),
    '#checkbox_descriptions' => $descriptions,
    '#process' => array('MYMODULE_process_checkbox_descriptions'),
  );

  return $form;
}

function MYMODULE_process_checkbox_descriptions($element) {
  foreach (element_children($element) as $key) {
    if (!empty($element['#checkbox_descriptions'][$element[$key]['#return_value']])) {
      $element[$key]['#description'] = $element['#checkbox_descriptions'][$element[$key]['#return_value']];
    }
  }

  return $element;
}
1
  • I modded your code a little, for the most part it works. But now the check boxes are gone and only $form['field_options']['#description] is showing. I suppose there is an error when rendering? function MYMODULE_process_checkbox_descriptions($element) { foreach (element_children($element['#options']) as $key) { if (!empty($element['#checkbox_descriptions'][$key])) { drupal_set_message('<pre>' . $element['#options'][$key] . ' will get ' . $element['#checkbox_descriptions'][$key] .'<br></pre>'); $element['#options'][$key]['#description'] = $element['#checkbox_descriptions'][$key]; } }
    – John R
    May 16, 2014 at 13:55
1

Got it!

function MYMODULE_process_checkbox_descriptions($element) {
foreach (element_children($element['#options']) as $key) {
    if (!empty($element['#checkbox_descriptions'][$key])) {
        $element[$key]= array(
            '#type' => 'checkbox',
            '#title' => $element['#options'][$key],
            '#description' => $element['#checkbox_descriptions'][$key]
            ) ;
    }
}
return $element;
}

And

function MYMODULE_form_alter($form, &$form_state,$form_id) {
  $form['field_options'] = array(
    '#type' => 'checkboxes',
    '#options' => array(
            1 => t('Option #1'),
            2 => t('Option #2')),
    '#checkbox_descriptions' => array(
            1=> t('Description #1'),
            2=> t('Description #2')),
    '#title' => t('Select Options'),
    '#description' => t('Entering a description here will display below the last item in the array'),
    '#default_value' => array(1),
    '#process' => array('MYMODULE_process_checkbox_descriptions'),
  );

  return $form;
}

Worked nicely!! thanks @Clive and @Marcel !

2
  • This works for me! THX! Jan 3, 2015 at 18:37
  • Don't you think this way will not pass the Drupal Form security measurements? At least if you're trying to alter existing fields. Sep 9, 2016 at 13:04
1

I came across this and the Accepted answer doesn't seem correct. It may work, but there is a much simpler approach. To set an individual #description per checkbox, you'll want to add a new item(s) to the $form array, using the array key you want to set the description for.

So given the original example where the array keys are 0 and 1...

$form['field_options'] = array(
  '#type' => 'checkboxes',
  '#options' => array(
    0 => t('Option #1'),
    1 => t('Option #2')),
  '#title' => t('Select Options'),
  '#description' => t('Entering a description here will display below the last item in the array'),
  '#default_value' => array(0),
);

You will want to add the following to your $form array to set a description for each checkbox...

$form['field_options'][0]['#description'] = 'Custom description for option one (array key 0)';
$form['field_options'][1]['#description'] = 'Custom description for option two (array key 1)';
1
  • So much simpler and easier than the accepted solution, thanks! Feb 22, 2021 at 19:38

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.