You are here

protected static function Dropdown::extractAllowedValues in Open Social 8.9

Same name and namespace in other branches
  1. 8 modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::extractAllowedValues()
  2. 8.2 modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::extractAllowedValues()
  3. 8.3 modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::extractAllowedValues()
  4. 8.4 modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::extractAllowedValues()
  5. 8.5 modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::extractAllowedValues()
  6. 8.6 modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::extractAllowedValues()
  7. 8.7 modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::extractAllowedValues()
  8. 8.8 modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::extractAllowedValues()
  9. 10.3.x modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::extractAllowedValues()
  10. 10.0.x modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::extractAllowedValues()
  11. 10.1.x modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::extractAllowedValues()
  12. 10.2.x modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::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.

See also

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

1 call to Dropdown::extractAllowedValues()
Dropdown::validateAllowedValues in modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php
Element_validate callback for options field allowed values.

File

modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php, line 170

Class

Dropdown
Plugin implementation of the 'dropdown' field type.

Namespace

Drupal\dropdown\Plugin\Field\FieldType

Code

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

    // Check for an explicit key.
    $matches = [];

    // @TODO Explicit key is necessary !
    if (preg_match('/(.*)\\|(.*)\\|(.*)/', $text, $matches)) {

      // Trim key and value to avoid unwanted spaces issues.
      $value = trim($matches[1]);
      $label = trim($matches[2]);
      $description = trim($matches[3]);
      $values[$value] = [
        'value' => $value,
        'label' => $label,
        'description' => $description,
      ];
    }
    elseif (preg_match('/(.*)\\|(.*)/', $text, $matches)) {

      // Trim key and value to avoid unwanted spaces issues.
      $value = trim($matches[1]);
      $label = trim($matches[2]);
      $values[$value] = [
        'value' => $value,
        'label' => $label,
      ];
    }
  }
  return $values;
}