You are here

protected function OptionsBase::formatHtmlItem in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformElement/OptionsBase.php \Drupal\webform\Plugin\WebformElement\OptionsBase::formatHtmlItem()

Format an element's value as HTML.

Parameters

array $element: An element.

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission.

array $options: An array of options.

Return value

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

Overrides WebformElementBase::formatHtmlItem

1 call to OptionsBase::formatHtmlItem()
WebformImageSelect::formatHtmlItem in modules/webform_image_select/src/Plugin/WebformElement/WebformImageSelect.php
Format an element's value as HTML.
1 method overrides OptionsBase::formatHtmlItem()
WebformImageSelect::formatHtmlItem in modules/webform_image_select/src/Plugin/WebformElement/WebformImageSelect.php
Format an element's value as HTML.

File

src/Plugin/WebformElement/OptionsBase.php, line 321

Class

OptionsBase
Provides a base 'options' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

protected function formatHtmlItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
  $value = $this
    ->getValue($element, $webform_submission, $options);
  $format = $this
    ->getItemFormat($element);
  switch ($format) {
    case 'raw':
      return $value;
    case 'description':
      if (isset($element['#options'])) {
        $options_description = $this
          ->hasProperty('options_description_display');
        if ($options_description) {
          $description = WebformOptionsHelper::getOptionDescription($value, $element['#options'], $options_description);
          return [
            '#markup' => $description,
          ];
        }
      }
      return '';
    case 'value':
    default:
      if (isset($element['#options'])) {
        $options_description = $this
          ->hasProperty('options_description_display');
        $value = WebformOptionsHelper::getOptionText($value, $element['#options'], $options_description);
      }

      // Build a render array that uses #plain_text so that
      // HTML characters are escaped.
      // @see \Drupal\Core\Render\Renderer::ensureMarkupIsSafe
      if ($value === '0') {

        // Issue #2765609: #plain_text doesn't render empty-like values
        // (e.g. 0 and "0").
        // Workaround: Use #markup until this issue is fixed.
        // @todo Remove workaround once only Drupal 8.7.x is supported.
        $build = [
          '#markup' => $value,
        ];
      }
      else {
        $build = [
          '#plain_text' => $value,
        ];
      }
      $options += [
        'prefixing' => TRUE,
      ];
      if ($options['prefixing']) {
        if (isset($element['#field_prefix'])) {
          $build['#prefix'] = $element['#field_prefix'];
        }
        if (isset($element['#field_suffix'])) {
          $build['#suffix'] = $element['#field_suffix'];
        }
      }
      return $build;
  }
}