You are here

function menu_manipulator_prepare_associative_list in Menu Manipulator 3.0.x

Same name and namespace in other branches
  1. 8.2 menu_manipulator.module \menu_manipulator_prepare_associative_list()
  2. 8 menu_manipulator.module \menu_manipulator_prepare_associative_list()
  3. 2.0.x menu_manipulator.module \menu_manipulator_prepare_associative_list()

Helper function to filter the associative key|label configuration.

Parameters

string $string: The original value.

Return value

array An associative array of values.

See also

Drupal\options\Plugin\Field\FieldType\ListItemBase::extractedAllowedValues();

1 call to menu_manipulator_prepare_associative_list()
menu_manipulator_form_menu_link_content_form_alter in ./menu_manipulator.module
Implements hook_form_BASE_FORM_ID_alter() for the Menu Link Content form.

File

./menu_manipulator.module, line 227
Contains menu_manipulator.module.

Code

function menu_manipulator_prepare_associative_list($string) {
  $values = [];
  $list = explode("\n", $string);
  $list = array_map('trim', $list);
  $list = array_filter($list, 'strlen');
  $generated_keys = $explicit_keys = FALSE;
  foreach ($list as $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]);
      $explicit_keys = TRUE;
    }
    $values[$key] = $value;
  }

  // We generate keys only if the list contains no explicit key at all.
  if ($explicit_keys && $generated_keys) {
    return [];
  }
  return $values;
}