1 list.module list_allowed_values_string($values)

Generates a string representation of an array of 'allowed values'.

This string format is suitable for edition in a textarea.

Parameters

$values: An array of values, where array keys are values and array values are labels.

Return value

The string representation of the $values array::

  • Values are separated by a carriage return.
  • Each value is in the format "value|label" or "value".

File

core/modules/field/modules/list/list.module, line 364
Defines list field types that can be used with the Options module.

Code

function list_allowed_values_string($values) {
  $lines = array();
  $previous_key = FALSE;

  foreach ($values as $key => $value) {
    // Convert groups.
    if (is_array($value)) {
      $lines[] = "<$key>";
      foreach ($value as $subkey => $subvalue) {
        $lines[] = "$subkey|$subvalue";
      }
      $previous_key = $key;
    }
    // Typical key|value pairs.
    else {
      // Exit out of any groups.
      if (isset($values[$previous_key]) && is_array($values[$previous_key])) {
        $lines[] = "<>";
      }
      // Skip empty rows.
      if ($values[$key] !== '') {
        $lines[] = "$key|$value";
      }
      $previous_key = $key;
    }
  }
  return implode("\n", $lines);
}