Allow Nested Optgroups

Created on 3 January 2019, over 6 years ago
Updated 17 March 2025, 23 days ago

API page: https://api.drupal.org/api/drupal/core%21modules%21system%21templates%21...

The select.html.twig allows only one level of optgroups. HTML standards also allow the same. But when incorporating plugins like select2 or Chosen, allowing nested optgroups would help build features like "Deepest level taxonomy term select".

Altering the code in the following way would be of much help.

{% import _self as select %}
{% spaceless %}
  <select{{ attributes }}>
    {{ select.options(options) }}    
  </select>
{% endspaceless %}

{% macro options(options) %}
  {% import _self as select %}
  {% for option in options %}
    {% if option.type == 'optgroup' %}
      <optgroup label="{{ option.label }}">
        {{ select.options(option.options) }}
      </optgroup>
    {% elseif option.type == 'option' %}
      <option value="{{ option.value }}"{{ option.selected ? ' selected="selected"' }}>{{ option.label }}</option>
    {% endif %}
  {% endfor %}
{% endmacro %}

Will try and create a patch soon.

Feature request
Status

Active

Version

11.0 🔥

Component

documentation

Created by

🇮🇳India Ashok Gj

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024