- Issue created by @valery.suslov
I would like to use optgroups on select element. However, due to the current implementation only flat list of options is supported. Attempt to provide an optgroup results in error.
Create a link attribute of type select and add options with optgroups:
attribute:
type: select
title: Select with optgroups
options:
Group1:
key1: Value1
Group2:
key2: Value2
Currently it fails due to the option translation in /src/LinkAttributesManager.php that only handles a single level of options:
// Translate options.
if (!empty($definition['options'])) {
foreach ($definition['options'] as $property => $option) {
$definition['options'][$property] = new TranslatableMarkup($option);
}
}
Proposal is to allow handling inner arrays, also translating $property when $option is value, e.g.:
// Translate options.
if (!empty($definition['options'])) {
$definition['options'] = $this->translateOptions($definition['options']);
}
}
/**
* Translate options, preserving optgroups.
*
* @param array<string,mixed> $options
* Array of options, possibly grouped.
*
* @return array<string,mixed>
* Array with optgroups and option values translated.
*/
private function translateOptions(array $options): array {
$translated = [];
foreach ($options as $property => $option) {
if (is_array($option)) {
$translated[(string) new TranslatableMarkup($property)] = $this->translateOptions($option);
}
else {
$translated[$property] = new TranslatableMarkup($option);
}
}
return $translated;
}
-
-
-
-
Active
2.1
Code