You are here

protected function OptionsBase::formatTextItems in Webform 6.x

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

Format an element's items as text.

Parameters

array $element: An element.

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

array $options: An array of options.

Return value

string The element's items as text.

Overrides WebformElementBase::formatTextItems

File

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

Class

OptionsBase
Provides a base 'options' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

protected function formatTextItems(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 = '☑';
        $unchecked = '☐';
        break;
    }
    $value = (array) $this
      ->getValue($element, $webform_submission, $options);
    $values = array_combine($value, $value);

    // Build list of checked and unchecked options.
    $list = [];
    $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);
      }
      $list[] = (isset($values[$option_value]) ? $checked : $unchecked) . ' ' . $option_text;
      unset($values[$option_value]);
    }

    // Append all remaining option values.
    foreach ($values as $value) {
      $list[] = $checked . ' ' . $value;
    }
    return implode(PHP_EOL, $list);
  }
  else {
    return parent::formatTextItems($element, $webform_submission, $options);
  }
}