You are here

protected static function ListFloatItem::extractAllowedValues 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::extractAllowedValues()

Extracts the allowed values array from the allowed_values element.

Parameters

string $string: The raw string to extract values from.

bool $has_data: The current field already has data inserted or not.

Return value

array|null The array of extracted key/value pairs, or NULL if the string is invalid.

Overrides ListItemBase::extractAllowedValues

See also

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

File

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

Class

ListFloatItem
Plugin implementation of the 'list_float' field type.

Namespace

Drupal\options\Plugin\Field\FieldType

Code

protected static function extractAllowedValues($string, $has_data) {
  $values = parent::extractAllowedValues($string, $has_data);
  if ($values) {
    $keys = array_keys($values);
    $labels = array_values($values);
    $keys = array_map(function ($key) {

      // Float keys are represented as strings and need to be disambiguated
      // ('.5' is '0.5').
      return is_numeric($key) ? (string) (double) $key : $key;
    }, $keys);
    return array_combine($keys, $labels);
  }
}