function 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

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.