diff --git a/core/modules/options/options.module b/core/modules/options/options.module index 3c649c5..d21c320 100644 --- a/core/modules/options/options.module +++ b/core/modules/options/options.module @@ -414,7 +414,8 @@ function options_field_validate(EntityInterface $entity = NULL, $field, $instanc $entity = _field_create_entity_from_ids($ids); } - $allowed_values = options_allowed_values($instance, $entity); + // Flatten the array before validating to account for optgroups. + $allowed_values = options_array_flatten(options_allowed_values($field, $instance, $entity)); foreach ($items as $delta => $item) { if (!empty($item['value'])) { if (!empty($allowed_values) && !isset($allowed_values[$item['value']])) { @@ -444,3 +445,25 @@ function options_options_list(FieldDefinitionInterface $field_definition, Entity return options_allowed_values($field_definition, $entity); } +/** + * Flattens an array of allowed values. + * + * @param $array + * A single or multidimensional array. + * @return + * A flattened array. + */ +function options_array_flatten($array) { + $result = array(); + if (is_array($array)) { + foreach ($array as $key => $value) { + if (is_array($value)) { + $result += options_array_flatten($value); + } + else { + $result[$key] = $value; + } + } + } + return $result; +}