You are here

protected static function BarcodeBase::extractTypes in Barcode 8

Extracts the allowed types array from the types element.

Parameters

string $string: The raw string to extract types/labels from.

Return value

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

See also

\Drupal\barcode\Plugin\Field\FieldType\BarcodeBase::getTypesString()

1 call to BarcodeBase::extractTypes()
BarcodeBase::validateTypes in src/Plugin/Field/FieldType/BarcodeBase.php
#element_validate callback for options field allowed values.

File

src/Plugin/Field/FieldType/BarcodeBase.php, line 202

Class

BarcodeBase

Namespace

Drupal\barcode\Plugin\Field\FieldType

Code

protected static function extractTypes($string) {
  $list = array_map('trim', explode("\n", $string));
  $list = array_filter($list, 'strlen');
  foreach ($list as $text) {
    $matches = [];
    if (preg_match('/(.*)\\|(.*)/', $text, $matches)) {

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