protected function WebformLikert::formatHtmlItem in Webform 8.5
Same name and namespace in other branches
- 6.x src/Plugin/WebformElement/WebformLikert.php \Drupal\webform\Plugin\WebformElement\WebformLikert::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
File
- src/
Plugin/ WebformElement/ WebformLikert.php, line 98
Class
- WebformLikert
- Provides a 'likert' element.
Namespace
Drupal\webform\Plugin\WebformElementCode
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':
$items = [];
foreach ($element['#questions'] as $question_key => $question_text) {
$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' => WebformAccessibilityHelper::buildVisuallyHidden(t('Questions')),
'align' => 'left',
'width' => '40%',
];
foreach ($element['#answers'] as $answer_value => $answer_text) {
list($answer_title) = WebformOptionsHelper::splitOption($answer_text);
$header[$answer_value] = [
'data' => $answer_title,
'align' => 'center',
];
}
// Calculate answers width.
$width = number_format(60 / count($element['#answers']), 2, '.', '') . '%';
$rows = [];
foreach ($element['#questions'] as $question_key => $question_text) {
list($question_title) = WebformOptionsHelper::splitOption($question_text);
$question_value = isset($value[$question_key]) ? $value[$question_key] : NULL;
$row = [];
$row['likert_question'] = [
'data' => $question_title,
'align' => 'left',
'width' => '40%',
];
foreach ($element['#answers'] as $answer_value => $answer_text) {
$row[$answer_value] = [
'data' => $question_value === $answer_value ? [
'#markup' => '✗',
] : '',
'align' => 'center',
'width' => $width,
];
}
$rows[$question_key] = $row;
}
return [
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#sticky' => $this
->getElementProperty($element, 'sticky'),
'#attributes' => [
'class' => [
'webform-likert-table',
],
],
'#attached' => [
'library' => [
'webform/webform.element.likert',
],
],
];
default:
case 'value':
case 'list':
$items = [];
foreach ($element['#questions'] as $question_key => $question_text) {
list($question_title) = WebformOptionsHelper::splitOption($question_text);
$answer_value = isset($value[$question_key]) ? $value[$question_key] : NULL;
$answer_text = $answer_value !== NULL ? WebformOptionsHelper::getOptionText($answer_value, $element['#answers'], TRUE) : $this
->t('[blank]');
$items[$question_key] = [
'question' => [
'#markup' => $question_title,
'#prefix' => '<b>',
'#suffix' => ':</b> ',
],
'answer' => [
'#markup' => $answer_text,
],
];
}
return [
'#theme' => 'item_list',
'#items' => $items,
];
}
}