Commit dbc572c8 authored by alexpott's avatar alexpott

Issue #2392301 by yched, joelpittet: OptGroups::flattenOptions() should preserve labels

parent 92db0802
......@@ -12,6 +12,7 @@
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\OptGroup;
/**
* Base class for the 'options_*' widgets.
......@@ -155,7 +156,7 @@ protected function getOptions(FieldableEntityInterface $entity) {
// Options might be nested ("optgroups"). If the widget does not support
// nested options, flatten the list.
if (!$this->supportsGroups()) {
$options = $this->flattenOptions($options);
$options = OptGroup::flattenOptions($options);
}
$this->options = $options;
......@@ -176,7 +177,7 @@ protected function getOptions(FieldableEntityInterface $entity) {
*/
protected function getSelectedOptions(FieldItemListInterface $items, $delta = 0) {
// We need to check against a flat list of options.
$flat_options = $this->flattenOptions($this->getOptions($items->getEntity()));
$flat_options = OptGroup::flattenOptions($this->getOptions($items->getEntity()));
$selected_options = array();
foreach ($items as $item) {
......@@ -191,21 +192,6 @@ protected function getSelectedOptions(FieldItemListInterface $items, $delta = 0)
return $selected_options;
}
/**
* Flattens an array of allowed values.
*
* @param array $array
* A single or multidimensional array.
*
* @return array
* The flattened array.
*/
protected function flattenOptions(array $array) {
$result = array();
array_walk_recursive($array, function($a, $b) use (&$result) { $result[$b] = $a; });
return $result;
}
/**
* Indicates whether the widgets support optgroups.
*
......
......@@ -50,7 +50,7 @@ protected static function doFlattenOptions(array $array, array &$options) {
static::doFlattenOptions($value, $options);
}
else {
$options[$key] = 1;
$options[$key] = $value;
}
}
}
......
......@@ -10,6 +10,7 @@
use Drupal\Core\Field\AllowedTagsXssTrait;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\OptGroup;
/**
* Plugin implementation of the 'list_default' formatter.
......@@ -40,7 +41,7 @@ public function viewElements(FieldItemListInterface $items) {
->getFieldStorageDefinition()
->getOptionsProvider('value', $items->getEntity());
// Flatten the possible options, to support opt groups.
$options = $this->flattenOptions($provider->getPossibleOptions());
$options = OptGroup::flattenOptions($provider->getPossibleOptions());
foreach ($items as $delta => $item) {
$value = $item->value;
......@@ -54,21 +55,4 @@ public function viewElements(FieldItemListInterface $items) {
return $elements;
}
/**
* Flattens an array of allowed values.
*
* @param array $array
* A single or multidimensional array.
*
* @return array
* The flattened array.
*
* @todo Remove it once https://www.drupal.org/node/2392301 landed.
*/
protected function flattenOptions(array $array) {
$result = array();
array_walk_recursive($array, function($a, $b) use (&$result) { $result[$b] = $a; });
return $result;
}
}
......@@ -22,7 +22,7 @@ class OptGroupTest extends UnitTestCase {
* @dataProvider providerTestFlattenOptions
*/
public function testFlattenOptions($options) {
$this->assertSame(array('foo' => 1), OptGroup::flattenOptions($options));
$this->assertSame(array('foo' => 'foo'), OptGroup::flattenOptions($options));
}
/**
......@@ -42,7 +42,6 @@ public function providerTestFlattenOptions() {
array(array($object1)),
array(array($object2)),
array(array($object1, $object2)),
array(array('foo' => $object3)),
array(array('foo' => $object3, $object1, array('foo' => 'foo'))),
);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment