You are here

public function OptionsBase::formatText in YAML Form 8

Format an element's value as plain text.

Parameters

array $element: An element.

array|mixed $value: A value.

array $options: An array of options.

Return value

string The element's value formatted as plain text or a render array.

Overrides YamlFormElementBase::formatText

File

src/Plugin/YamlFormElement/OptionsBase.php, line 172

Class

OptionsBase
Provides a base 'options' element.

Namespace

Drupal\yamlform\Plugin\YamlFormElement

Code

public function formatText(array &$element, $value, array $options = []) {

  // Return empty value.
  if ($value === '' || $value === NULL || is_array($value) && empty($value)) {
    return '';
  }
  $format = $this
    ->getFormat($element);
  $flattened_options = OptGroup::flattenOptions($element['#options']);

  // If not multiple options array return the simple value.
  if (!is_array($value)) {
    return $format == 'raw' ? $value : YamlFormOptionsHelper::getOptionText($value, $flattened_options);
  }
  $options_text = YamlFormOptionsHelper::getOptionsText($value, $flattened_options);
  switch ($format) {
    case 'ol':
      $list = [];
      $index = 1;
      foreach ($options_text as $option_text) {
        $list[] = $index++ . '. ' . $option_text;
      }
      return implode("\n", $list);
    case 'ul':
      $list = [];
      foreach ($options_text as $index => $option_text) {
        $list[] = '- ' . $option_text;
      }
      return implode("\n", $list);
    case 'and':
      return YamlFormArrayHelper::toString($options_text, t('and'));
    case 'comma':
      return implode(', ', $options_text);
    case 'semicolon':
      return implode('; ', $options_text);
    case 'raw':
      return implode(', ', $value);
    default:
      return implode($format, $options_text);
  }
}