diff --git a/core/lib/Drupal/Core/Form/FormValidator.php b/core/lib/Drupal/Core/Form/FormValidator.php index ee2da65..51dd3fc 100644 --- a/core/lib/Drupal/Core/Form/FormValidator.php +++ b/core/lib/Drupal/Core/Form/FormValidator.php @@ -316,7 +316,7 @@ protected function performRequiredValidation(&$elements, FormStateInterface &$fo if (isset($elements['#options']) && isset($elements['#value'])) { if ($elements['#type'] == 'select') { - $options = OptGroup::flattenOptions($elements['#options']); + $options = Element\Select::getFlattenedOptions($elements); } else { $options = $elements['#options']; diff --git a/core/lib/Drupal/Core/Render/Element/Optgroup.php b/core/lib/Drupal/Core/Render/Element/Optgroup.php index 04b963c..12290c7 100644 --- a/core/lib/Drupal/Core/Render/Element/Optgroup.php +++ b/core/lib/Drupal/Core/Render/Element/Optgroup.php @@ -109,4 +109,23 @@ public static function preRenderOptgroup($element) { return $element; } + /** + * Returns a list of options for an option group element. + * + * This function assumes that the passed element has been processed by + * Optgroup::processOptgroup() prior to calling this function. + */ + public static function getOptions($element) { + $options = []; + + // @TODO This does not respect weights. + foreach (Element::children($element) as $child) { + if ($element[$child]['#type'] == 'option') { + $options[$child] = $element[$child]['#title']; + } + } + + return $options; + } + } diff --git a/core/lib/Drupal/Core/Render/Element/Select.php b/core/lib/Drupal/Core/Render/Element/Select.php index beae6bc..c4ab21d 100644 --- a/core/lib/Drupal/Core/Render/Element/Select.php +++ b/core/lib/Drupal/Core/Render/Element/Select.php @@ -212,4 +212,25 @@ public static function preRenderSelect($element) { return $element; } + /** + * Returns a list of flattened options for a select element. + * + * This function assumes that the passed element has been processed by + * Select::processSelect() prior to calling this function. + */ + public static function getFlattenedOptions($element) { + $options = []; + + // @TODO This does not respect weights. + foreach (Element::children($element) as $child) { + if ($element[$child]['#type'] == 'optgroup') { + $options += Optgroup::getOptions($element[$child]); + unset($element[$child]); + } + } + $options += Optgroup::getOptions($element); + + return $options; + } + }