You are here

protected static function OptionsEmailItem::extractAllowedValues in Contact Storage 8

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

src/Plugin/Field/FieldType/OptionsEmailItem.php, line 70

Class

OptionsEmailItem
Plugin to add the Option email item custom field type.

Namespace

Drupal\contact_storage\Plugin\Field\FieldType

Code

protected static function extractAllowedValues($string, $has_data) {
  $values = [];

  // Explode the content of the text area per line.
  $list = explode("\n", $string);
  $list = array_map('trim', $list);
  $list = array_filter($list, 'strlen');
  foreach ($list as $text) {

    // Explode each line around the pipe symbol.
    $elements = explode('|', $text);

    // Expects 3 elements (value, label and emails).
    if (count($elements) == 3) {

      // Sanitize the email address.
      if (\Drupal::service('email.validator')
        ->isValid($elements[2])) {
        $values[$elements[0]] = [
          'value' => $elements[1],
          'emails' => $elements[2],
        ];
        continue;
      }
    }

    // Failed at some point. Returns NULL to display an error.
    return;
  }
  if (empty($values)) {
    return;
  }
  return $values;
}