Problem/Motivation

There are many situations where it is extremly useful to add a description to a radio of radios or checkbox of checkboxes. In GA it is implemented like below. I also tried something like this with Webforms, but it is not really easy and required crazy form altering.

Currently the code looks like this:

$form['tracking']['domain_tracking']['google_analytics_domain_mode'] = [
  '#type' => 'radios',
  '#title' => t('What are you tracking?'),
  '#options' => [
    0 => Safemarkup::format(t('A single domain (default)') . '<div class="description">' . t('Domain: @domain', ['@domain' => $_SERVER['HTTP_HOST']]) . '</div>'),
    1 => Safemarkup::format(t('One domain with multiple subdomains') . '<div class="description">' . t('Examples: @domains', ['@domains' => implode(', ', $multiple_sub_domains)]) . '</div>'),
    2 => Safemarkup::format(t('Multiple top-level domains') . '<div class="description">' . t('Examples: @domains', ['@domains' => implode(', ', $multiple_toplevel_domains)]) . '</div>'),
  ],
  '#default_value' => $config->get('domain_mode'),
];

But this is something that should be avoided and should look like:

$form['tracking']['domain_tracking']['google_analytics_domain_mode'] = [
  '#type' => 'radios',
  '#title' => t('What are you tracking?'),
  '#options' => [
    0 => [
      '#title' => t('A single domain (default)'),
      '#description' => t('Domain: @domain', ['@domain' => $_SERVER['HTTP_HOST']]),
    ],
    1 => [
      '#title' => t('One domain with multiple subdomains'),
      '#description' => t('Examples: @domains', ['@domains' => implode(', ', $multiple_sub_domains)]),
    ],
    2 => [
      '#title' => t('Multiple top-level domains'),
      '#description' => t('Examples: @domains', ['@domains' => implode(', ', $multiple_toplevel_domains)]),
    ],
  ],
  '#default_value' => $config->get('domain_mode'),
];

Proposed resolution

Allow optional array in option items with #title and #description.

Remaining tasks

Write patch.

User interface changes

None for existing form items.

API changes

Yes.

Data model changes

None.

Comments

hass’s picture

Issue summary: View changes
larowlan’s picture

This is already supported, have a look at comment module, the field type form. In addition to #options, you can do this:

$f['field'] = [
  '#title' => 'blah',
  '#options' => [
    0 => 'yep',
    1 => 'no',
  ],
  0 => [
    '#description' => 'description for yep',
  ],
]
hass’s picture

That is interresting and works, but this is not documented. Since what core version does this work? However it works It is not very intuitive to me.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

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

joachim’s picture

Status: Active » Closed (outdated)

This has been possible since D7 and maybe even before. But not widely known. I've filed #2779999: Document checkboxes and radios element can have individual descriptions.