You are here

protected function OptionsBase::formatHtmlItems in Webform 6.x

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

Format an element's items as HTML.

Parameters

array $element: An element.

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

array $options: An array of options.

Return value

string|array The element's items as HTML.

Overrides WebformElementBase::formatHtmlItems

File

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

Class

OptionsBase
Provides a base 'options' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

protected function formatHtmlItems(array &$element, WebformSubmissionInterface $webform_submission, array $options = []) {
  $format = $this
    ->getItemsFormat($element);
  if (strpos($format, 'checklist:') === 0) {

    // Get checked/unchecked icons.
    list(, $checked_type) = explode(':', $format);
    switch ($checked_type) {
      case 'crosses':
        $checked = '✖ ';
        $unchecked = '⚬ ';
        break;
      default:
      case 'boxes':
        $checked = Markup::create('<span style="font-size: 1.4em; line-height: 1em">☑</span> ');
        $unchecked = Markup::create('<span style="font-size: 1.4em; line-height: 1em">☐</span> ');
        break;
    }
    $value = (array) $this
      ->getValue($element, $webform_submission, $options);
    $values = array_combine($value, $value);

    // Build list of checked and unchecked options.
    $build = [];
    $options_description = $this
      ->hasProperty('options_description_display');
    $flattened_options = OptGroup::flattenOptions($element['#options']);
    foreach ($flattened_options as $option_value => $option_text) {
      if ($options_description && WebformOptionsHelper::hasOptionDescription($option_text)) {
        list($option_text) = WebformOptionsHelper::splitOption($option_text);
      }
      $build[$option_value] = [
        '#prefix' => isset($values[$option_value]) ? $checked : $unchecked,
        '#markup' => $option_text,
        '#suffix' => '<br/>',
      ];
      unset($values[$option_value]);
    }

    // Append all remaining option values.
    foreach ($values as $value) {
      $build[$value] = [
        '#prefix' => $checked,
        '#markup' => $value,
        '#suffix' => '<br/>',
      ];
    }
    return $build;
  }
  else {
    return parent::formatHtmlItems($element, $webform_submission, $options);
  }
}