You are here

protected static function DoubleField::extractAllowedValues in Double Field 4.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/Field/FieldType/DoubleField.php \Drupal\double_field\Plugin\Field\FieldType\DoubleField::extractAllowedValues()

Extracts the allowed values array from the allowed_values element.

Parameters

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

Return value

array The array of extracted key/value pairs.

See also

\Drupal\options\Plugin\Field\FieldType\ListTextItem::extractAllowedValues()

1 call to DoubleField::extractAllowedValues()
DoubleField::validateAllowedValues in src/Plugin/Field/FieldType/DoubleField.php
Element validate callback for subfield allowed values.

File

src/Plugin/Field/FieldType/DoubleField.php, line 528

Class

DoubleField
Plugin implementation of the 'double_field' field type.

Namespace

Drupal\double_field\Plugin\Field\FieldType

Code

protected static function extractAllowedValues(string $string) : array {
  $values = [];
  $list = explode("\n", $string);
  $list = array_map('trim', $list);
  $list = array_filter($list, 'strlen');
  foreach ($list as $text) {

    // Check for an explicit key.
    if (preg_match('/(.*)\\|(.*)/', $text, $matches)) {

      // Trim key and value to avoid unwanted spaces issues.
      $key = trim($matches[1]);
      $value = trim($matches[2]);
    }
    else {
      $key = $value = $text;
    }
    $values[$key] = $value;
  }
  return $values;
}