Checkboxes and radios form elements can both have descriptions added to each item (or indeed other specific form properties). However, this is not documented and not widely known.

It is however covered by our tests, in FormTestCheckboxesRadiosForm:

    // Expand #type checkboxes, setting custom element properties for some but not
    // all options.
    $form['checkboxes'] = array(
      '#type' => 'checkboxes',
      '#title' => 'Checkboxes',
      '#options' => array(
        0 => 'Zero',
        'foo' => 'Foo',
        1 => 'One',
        'bar' => $this->t('<em>Bar - checkboxes</em>'),
        '>' => "<em>Special Char</em><script>alert('checkboxes');</script>",
      ),
    );
    if ($customize) {
      $form['checkboxes'] += array(
        'foo' => array(
          '#description' => 'Enable to foo.',
        ),
        1 => array(
          '#weight' => 10,
        ),
      );
    }

The slightly clearer way to do this is as follows:

    $form['checkboxes'] = array(
      '#type' => 'checkboxes',
      '#title' => 'Checkboxes',
      '#options' => array(
        'foo' => 'Foo',
        'bar' => 'Bar',
     );
   $form['checkboxes']['foo']['#description'] = 'description for the foo checkbox';

Documentation should be added to the Checkboxes and Radios element classes.

Issue fork drupal-2779999

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

joachim created an issue. See original summary.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

shashikant_chauhan’s picture

Assigned: Unassigned » shashikant_chauhan
Status: Active » Needs review
FileSize
1.05 KB

adding patch.

joachim’s picture

Status: Needs review » Needs work

Thanks for the patch!

Good start but needs a bit more work I think:

+++ b/core/lib/Drupal/Core/Render/Element/Checkboxes.php
@@ -21,6 +21,7 @@
+ * $form['high_school']['tests_taken']['SAT']['#description'] = $this->t('description for the SAT option');

The descriptions should be sentences with a capital and a full stop.

I'd also add a code comment within the code example that explains it, eg:

 * );
 * // Element properties may be set on single option items as follows.
shashikant_chauhan’s picture

Status: Needs work » Needs review
FileSize
1.19 KB
1.33 KB

Thanks for the suggestion, adding updated patch.

David_Rothstein’s picture

Version: 8.3.x-dev » 8.2.x-dev
Status: Needs review » Reviewed & tested by the community

The patch looks good. As a small documentation improvement there is no reason this shouldn't be added to 8.2.x also, and the patch applies there as well.

Note that the corresponding issue for documenting this for the Drupal 7 form API reference is #1349432: Mention checkboxes/radios options pre-setting in forms_api_reference.html.

xjm’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/lib/Drupal/Core/Render/Element/Checkboxes.php
@@ -21,6 +21,8 @@
+ * // Element properties may be set on single option items as follows.
+ * $form['high_school']['tests_taken']['SAT']['#description'] = $this->t('Description for the SAT option.');
  * @endcode

Rather than putting a comment within a comment in the same @code/@endcode block, let's start a new one and put the comment above it. So:

* @endcode
*
* Element properties may...
* @code
* $form...
* @endcode

Looks good to me other than that. Thanks!

joachim’s picture

Re #7: taking the comment out of a code comment is a good thing, but:

+ * $form['high_school']['tests_taken']['SAT']['#description'] = $this->t('Description for the SAT option.');

this code in isolation means nothing. It only works if it's following the full radios/checkboxes definition.

David_Rothstein’s picture

Status: Needs work » Needs review

I agree. It is not a new code example, but rather part of the previous one.

That said, I'd be willing to mark a patch that did it either way RTBC :)

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

dpi’s picture

Title: document that checkboxes and radios element can have individual descriptions » Document checkboxes and radios element can have individual descriptions
Version: 8.9.x-dev » 9.1.x-dev
Assigned: shashikant_chauhan » Unassigned

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

guilhermevp’s picture

Assigned: Unassigned » guilhermevp

guilhermevp’s picture

Made a MR based on feedback #7, but #5 still applies. Please review!

guilhermevp’s picture

Assigned: guilhermevp » Unassigned
jonathanshaw’s picture

Status: Needs review » Reviewed & tested by the community

RTBC per #9

quietone’s picture

Status: Reviewed & tested by the community » Needs work

Setting to NW for unresolved comment.

joachim’s picture

Status: Needs work » Reviewed & tested by the community

> I am concerned that both 'high_school' and 'SAT' are culturally specific and may not mean much to anyone outside of countries that have high schools and use the 'SAT' test

> Yep. In addition to all that, I checked and "SAT" is also a registered trademark, which is another reason we should probably avoid referencing it (or any similar tests).

That all seems fair enough, but isn't that all out of scope for this issue? The SAT and high school stuff is already in the example code.

I'd be happy to work on a follow-up, but I don't think the bikeshedding over what to put instead should hold up a simple docs issue which has been hanging around for 6 years.

lauriii’s picture

Issue tags: +Needs follow-up

Can we open a follow-up to address the feedback from the MR?

joachim’s picture

dww made their first commit to this issue’s fork.

dww’s picture

This needed a trivial rebase to be merge-able. Leaving RTBC, since I didn't change any code.

  • lauriii committed 46cc0e0 on 10.0.x
    Issue #2779999 by shashikant_chauhan, guilhermevp, dww, joachim, xjm:...

  • lauriii committed ed80c4d on 9.4.x
    Issue #2779999 by shashikant_chauhan, guilhermevp, dww, joachim, xjm:...

lauriii’s picture

Status: Reviewed & tested by the community » Fixed

Committed 46cc0e0 and pushed to 10.0.x. Also cherry-picked to 9.4.x. Thanks!

  • xjm committed 0bddb14 on 9.3.x authored by lauriii
    Issue #2779999 by shashikant_chauhan, guilhermevp, dww, joachim, xjm:...
xjm’s picture

Version: 9.4.x-dev » 9.3.x-dev

Cherry-picked to 9.3.x as a patch-release-eligible documentation improvement. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.