You are here

public function VarbaseBootstrapParagraphsSettingsForm::optionsExtractAllowedListTextValues in Varbase Bootstrap Paragraphs 8.5

Same name and namespace in other branches
  1. 8.7 src/Form/VarbaseBootstrapParagraphsSettingsForm.php \Drupal\varbase_bootstrap_paragraphs\Form\VarbaseBootstrapParagraphsSettingsForm::optionsExtractAllowedListTextValues()
  2. 8.4 src/Form/VarbaseBootstrapParagraphsSettingsForm.php \Drupal\varbase_bootstrap_paragraphs\Form\VarbaseBootstrapParagraphsSettingsForm::optionsExtractAllowedListTextValues()
  3. 8.6 src/Form/VarbaseBootstrapParagraphsSettingsForm.php \Drupal\varbase_bootstrap_paragraphs\Form\VarbaseBootstrapParagraphsSettingsForm::optionsExtractAllowedListTextValues()
  4. 9.0.x src/Form/VarbaseBootstrapParagraphsSettingsForm.php \Drupal\varbase_bootstrap_paragraphs\Form\VarbaseBootstrapParagraphsSettingsForm::optionsExtractAllowedListTextValues()

Parses a string of 'allowed values' into an array.

Parameters

string $string: The list of allowed values in string format described in optionsExtractAllowedValues().

Return value

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

See also

optionsExtractAllowedListTextValues()

2 calls to VarbaseBootstrapParagraphsSettingsForm::optionsExtractAllowedListTextValues()
VarbaseBootstrapParagraphsSettingsForm::submitForm in src/Form/VarbaseBootstrapParagraphsSettingsForm.php
Submit Form.
VarbaseBootstrapParagraphsSettingsForm::validateForm in src/Form/VarbaseBootstrapParagraphsSettingsForm.php
Validate Form.

File

src/Form/VarbaseBootstrapParagraphsSettingsForm.php, line 128

Class

VarbaseBootstrapParagraphsSettingsForm
Provides form for managing module settings.

Namespace

Drupal\varbase_bootstrap_paragraphs\Form

Code

public function optionsExtractAllowedListTextValues($string) {
  $values = [];
  $list = explode("\n", $string);
  $list = array_map('trim', $list);
  $list = array_filter($list, 'strlen');
  foreach ($list as $position => $text) {
    $value = $key = FALSE;

    // 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]);
      $values[$key] = $value;
    }
    else {
      return;
    }
  }
  return $values;
}