You are here

public function YamlFormLikert::formatHtml in YAML Form 8

Format an element's value as HTML.

Parameters

array $element: An element.

array|mixed $value: A value.

array $options: An array of options.

Return value

array|string The element's value formatted as an HTML string or a render array.

Overrides YamlFormElementBase::formatHtml

1 call to YamlFormLikert::formatHtml()
YamlFormLikert::formatTableColumn in src/Plugin/YamlFormElement/YamlFormLikert.php
Format an element's table column value.

File

src/Plugin/YamlFormElement/YamlFormLikert.php, line 77

Class

YamlFormLikert
Provides a 'likert' element.

Namespace

Drupal\yamlform\Plugin\YamlFormElement

Code

public function formatHtml(array &$element, $value, array $answers = []) {
  $format = $this
    ->getFormat($element);
  switch ($format) {
    case 'raw':
      $items = [];
      foreach ($element['#questions'] as $question_key => $question_label) {
        $answer_value = isset($value[$question_key]) ? $value[$question_key] : NULL;
        $items[$question_key] = [
          '#markup' => "<b>{$question_key}:</b> {$answer_value}",
        ];
      }
      return [
        '#theme' => 'item_list',
        '#items' => $items,
      ];
    case 'table':

      // NOTE: Including inline align attributes to help style the table for
      // HTML emails.
      $header = [];
      $header['likert_question'] = [
        'data' => '',
        'align' => 'left',
        'width' => '40%',
      ];
      foreach ($element['#answers'] as $answer_value => $answer_text) {
        $header[$answer_value] = [
          'data' => $answer_text,
          'align' => 'center',
        ];
      }

      // Calculate answers width.
      $width = number_format(60 / count($element['#answers']), 2, '.', '') . '%';
      $rows = [];
      foreach ($element['#questions'] as $question_key => $question_label) {
        $question_value = isset($value[$question_key]) ? $value[$question_key] : NULL;
        $row = [];
        $row['likert_question'] = [
          'data' => $question_label,
          'align' => 'left',
          'width' => '40%',
        ];
        foreach ($element['#answers'] as $answer_value => $answer_text) {
          $row[$answer_value] = [
            'data' => $question_value == $answer_value ? [
              '#markup' => '&#10007;',
            ] : '',
            'align' => 'center',
            'width' => $width,
          ];
        }
        $rows[$question_key] = $row;
      }
      return [
        '#type' => 'table',
        '#header' => $header,
        '#rows' => $rows,
        '#attributes' => [
          'class' => [
            'yamlform-likert-table',
          ],
        ],
        '#attached' => [
          'library' => [
            'yamlform/yamlform.element.likert',
          ],
        ],
      ];
    default:
    case 'value':
    case 'list':
      $items = [];
      foreach ($element['#questions'] as $question_key => $question_label) {
        $answer_value = isset($value[$question_key]) ? $value[$question_key] : NULL;
        $answer_text = $answer_value ? YamlFormOptionsHelper::getOptionText($answer_value, $element['#answers']) : $this
          ->t('[blank]');
        $items[$question_key] = [
          '#markup' => "<b>{$question_label}:</b> {$answer_text}",
        ];
      }
      return [
        '#theme' => 'item_list',
        '#items' => $items,
      ];
  }
}