You are here

public function YamlFormLikert::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/YamlFormLikert.php, line 158

Class

YamlFormLikert
Provides a 'likert' element.

Namespace

Drupal\yamlform\Plugin\YamlFormElement

Code

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

  // Return empty value.
  if ($value === '' || $value === NULL || is_array($value) && empty($value)) {
    return '';
  }
  $format = $this
    ->getFormat($element);
  switch ($format) {
    case 'raw':
      $list = [];
      foreach ($element['#questions'] as $question_key => $question_label) {
        $answer_value = isset($value[$question_key]) ? $value[$question_key] : NULL;
        $list[] = "{$question_key}: {$answer_value}";
      }
      return implode("\n", $list);
    default:
    case 'value':
    case 'table':
    case 'list':
      $list = [];
      foreach ($element['#questions'] as $question_key => $question_label) {
        $answer_value = isset($value[$question_key]) ? $value[$question_key] : NULL;
        $answer_text = YamlFormOptionsHelper::getOptionText($answer_value, $element['#answers']);
        $list[] = "{$question_label}: {$answer_text}";
      }
      return implode("\n", $list);
  }
}