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
Comment #1
hass commentedComment #2
larowlanThis is already supported, have a look at comment module, the field type form. In addition to #options, you can do this:
Comment #3
hass commentedThat 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.
Comment #5
joachim commentedThis 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.