Problem/Motivation

Form controls of type select will group options in HTML <optgroup> tags if the #options array contains a sub-array (the key for the sub-array will be used as the optgroup label). For example, in D7, the following code will output the control seen in the attached Screen Shot 2014-06-10 at 11.24.01.png:

/**
* Form constructor for the select control with option-groups example.
*
* @see user_login_form_validate()
* @see user_login_form_submit()
*
* @ingroup forms
*/
function form_example_select_optgroup($form, &$form_state) {
  $form['test_select'] = array(
    '#type' => 'select',
    '#title' => t('Selected'),
    '#options' => array(
      '1' => t('One'),
      t('Two') => array(
        '2.1' => t('Two point one'),
        '2.2' => t('Two point two'),
      ),
      '3' => t('Three'),
    ),
  );
}

... however, this behaviour is not documented in the Form API Reference for the select control nor the Form API Reference for the #options property.

Proposed resolution

Add documentation to the select control and the options property in the Form API Reference.

Remaining tasks

  1. Write a patch for 8.x
  2. Review and RTBC
  3. Commit to 8.x
  4. Write a patch for 7.x
  5. Review and RTBC
  6. Commit to 7.x

User interface changes

None.

API changes

None.

CommentFileSizeAuthor
#7 interdiff-5-7.txt894 bytesAnonymous (not verified)
#7 document_how_optgroups-2283703-7.patch1.43 KBAnonymous (not verified)
#5 interdiff-3-5.txt855 bytesAnonymous (not verified)
#5 document_how_optgroups-2283703-5.patch1.45 KBAnonymous (not verified)
#3 document_how_optgroups-2283703-3.patch1.03 KBAnonymous (not verified)
Screen Shot 2014-06-10 at 11.24.01.png12.43 KBmparker17
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhodgdon’s picture

Project: Drupal core » Documentation
Version: 8.x-dev »
Component: documentation » API documentation files
Issue tags: +FAPI reference

Form API reference is in the Documentation project git repository, and is also nearly impossible to edit. Good luck.

jhodgdon’s picture

Title: Document how optgroups are generated in the Form API Reference » Document how optgroups work on 'select' form elements
Project: Documentation » Drupal core
Version: » 8.0.x-dev
Component: API documentation files » documentation
Category: Feature request » Task
Parent issue: » #2486967: [meta] Move/Create Form Element Documentation
Related issues: -#1617948: [policy for now] New standard for documenting form/render elements and properties

For Drupal 8, we need to document this on the Select render element, which is where we are documenting form/render elements now instead of in the Form API reference.

Anonymous’s picture

Status: Active » Needs review
FileSize
1.03 KB

How about this?

jhodgdon’s picture

Status: Needs review » Needs work

Looks good! Can we add a usage example for optgroups too?

Anonymous’s picture

Status: Needs work » Needs review
FileSize
1.45 KB
855 bytes

Yes, we can.

In the example I cast the TranslatableMarkup object so it can be used as a key, and that works, but it's possible this is not the right thing to do here?

jhodgdon’s picture

Status: Needs review » Needs work

Great, thanks! A bit of tweaking...

  1. +++ b/core/lib/Drupal/Core/Render/Element/Select.php
    @@ -14,12 +14,30 @@
    + * Example usage:
    

    In the other Element classes, we used

    Usage example:

    not

    Example usage:

    as the header. Let's stay consistent.

  2. +++ b/core/lib/Drupal/Core/Render/Element/Select.php
    @@ -14,12 +14,30 @@
    + *     (string) t('Two') => [
    

    How about if we just use '2' as the header here, rather than casting the t() output?

  3. I think the indentation of the ] lines at the end of the usage example is a bit off too.
Anonymous’s picture

Status: Needs work » Needs review
FileSize
1.43 KB
894 bytes

Fixed all three items. Thanks for reviewing!

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Looks good, thanks!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed df03a9d and pushed to 8.0.x and 8.1.x. Thanks!

  • alexpott committed aac6a3f on 8.1.x
    Issue #2283703 by pjonckiere, jhodgdon: Document how optgroups work on '...

  • alexpott committed df03a9d on
    Issue #2283703 by pjonckiere, jhodgdon: Document how optgroups work on '...

Status: Fixed » Closed (fixed)

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