Optgroups on select attribute are not supported
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3545260. -->
Reported by: [valery.suslov](https://www.drupal.org/user/3788140)
Related to !25
>>>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p>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.</p>
<h4 id="summary-steps-reproduce">Steps to reproduce</h4>
<p>Create a link attribute of type select and add options with optgroups:</p>
<pre>attribute:<br> type: select<br> title: Select with optgroups<br> options:<br> Group1:<br> key1: Value1<br> Group2:<br> key2: Value2</pre><h3 id="summary-proposed-resolution">Proposed resolution</h3>
<p>Currently it fails due to the option translation in /src/LinkAttributesManager.php that only handles a single level of options:</p>
<pre> // Translate options.<br> if (!empty($definition['options'])) {<br> foreach ($definition['options'] as $property => $option) {<br> $definition['options'][$property] = new TranslatableMarkup($option);<br> }<br> }</pre><p>Proposal is to allow handling inner arrays, also translating $property when $option is value, e.g.:</p>
<pre> // Translate options.<br> if (!empty($definition['options'])) {<br> $definition['options'] = $this->translateOptions($definition['options']);<br> }<br> }<br><br> /**<br> * Translate options, preserving optgroups.<br> *<br> * @param array<string,mixed> $options<br> * Array of options, possibly grouped.<br> *<br> * @return array<string,mixed><br> * Array with optgroups and option values translated.<br> */<br> private function translateOptions(array $options): array {<br> $translated = [];<br> foreach ($options as $property => $option) {<br> if (is_array($option)) {<br> $translated[(string) new TranslatableMarkup($property)] = $this->translateOptions($option);<br> }<br> else {<br> $translated[$property] = new TranslatableMarkup($option);<br> }<br> }<br> return $translated;<br> }</pre><h3 id="summary-remaining-tasks">Remaining tasks</h3>
<p>-</p>
<h3 id="summary-ui-changes">User interface changes</h3>
<p>-</p>
<h3 id="summary-api-changes">API changes</h3>
<p>-</p>
<h3 id="summary-data-model-changes">Data model changes</h3>
<p>-</p>
issue