You are here

protected static function MailWithTypeItem::extractMailTypes in CRM Core 8.2

Extracts the allowed values array from the mail_types element.

Parameters

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

Return value

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

1 call to MailWithTypeItem::extractMailTypes()
MailWithTypeItem::validateMailTypes in modules/crm_core_contact/src/Plugin/Field/FieldType/MailWithTypeItem.php
#element_validate callback for possible mail types.

File

modules/crm_core_contact/src/Plugin/Field/FieldType/MailWithTypeItem.php, line 146

Class

MailWithTypeItem
Defines the 'mail_with_type' field type.

Namespace

Drupal\crm_core_contact\Plugin\Field\FieldType

Code

protected static function extractMailTypes($string) {
  $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 = [];
    if (preg_match('/(.*)\\|(.*)/', $text, $matches)) {

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