diff --git a/core/modules/options/lib/Drupal/options/Tests/OptionsArrayFlattenUnitTest.php b/core/modules/options/lib/Drupal/options/Tests/OptionsArrayFlattenUnitTest.php new file mode 100644 index 0000000..259c5d5 --- /dev/null +++ b/core/modules/options/lib/Drupal/options/Tests/OptionsArrayFlattenUnitTest.php @@ -0,0 +1,43 @@ + 'Options flatten array', + 'description' => 'Test multi-dimensional array is flattened.', + 'group' => 'Field types', + ); + } + + public function testArrayFlatten() { + $p = array('v1' => 'l1', 'g1' => array('v2' => 'l2', 'v3' => 'l3')); + $sub = options_array_flatten($p); + $this->assertEqual(array('v1' => 'l1', 'v2' => 'l2', 'v3' => 'l3'), $sub); + + $p = array ( + 'parent1' => array(3 => 'child1', 4 => 'child2', 5 => 'child3'), + 'parent2' => array(8 => 'child1', 9 => 'child2') + ); + $sub = options_array_flatten($p); + $this->assertEqual(array( + 3 => 'child1', 4 => 'child2', 5 => 'child3', + 8 => 'child1', 9 => 'child2'), $sub + ); + } +} diff --git a/core/modules/options/options.module b/core/modules/options/options.module index 3c649c5..e69c541 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,26 @@ function options_options_list(FieldDefinitionInterface $field_definition, Entity return options_allowed_values($field_definition, $entity); } +/** + * Flattens an array of allowed values. + * + * @param array $array + * A single or multidimensional array + * + * @return array + * 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; +}