You are here

public static function ListFloatItem::simplifyAllowedValues in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/options/src/Plugin/Field/FieldType/ListFloatItem.php \Drupal\options\Plugin\Field\FieldType\ListFloatItem::simplifyAllowedValues()
  2. 10 core/modules/options/src/Plugin/Field/FieldType/ListFloatItem.php \Drupal\options\Plugin\Field\FieldType\ListFloatItem::simplifyAllowedValues()

Simplifies allowed values to a key-value array from the structured array.

Parameters

array $structured_values: Array of items with a 'value' and 'label' key each for the allowed values.

Return value

array Allowed values were the array key is the 'value' value, the value is the 'label' value.

Overrides ListItemBase::simplifyAllowedValues

See also

\Drupal\options\Plugin\Field\FieldType\ListItemBase::structureAllowedValues()

File

core/modules/options/src/Plugin/Field/FieldType/ListFloatItem.php, line 93

Class

ListFloatItem
Plugin implementation of the 'list_float' field type.

Namespace

Drupal\options\Plugin\Field\FieldType

Code

public static function simplifyAllowedValues(array $structured_values) {
  $values = [];
  foreach ($structured_values as $item) {

    // Nested elements are embedded in the label.
    if (is_array($item['label'])) {
      $item['label'] = static::simplifyAllowedValues($item['label']);
    }

    // Cast the value to a float first so that .5 and 0.5 are the same value
    // and then cast to a string so that values like 0.5 can be used as array
    // keys.
    // @see http://php.net/manual/language.types.array.php
    $values[(string) (double) $item['value']] = $item['label'];
  }
  return $values;
}