protected function WebformHeight::formatTextItem in Webform 6.x
Same name and namespace in other branches
- 8.5 src/Plugin/WebformElement/WebformHeight.php \Drupal\webform\Plugin\WebformElement\WebformHeight::formatTextItem()
Format an element's value 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 value formatted as text.
Overrides WebformElementBase::formatTextItem
See also
_webform_token_get_submission_value()
File
- src/
Plugin/ WebformElement/ WebformHeight.php, line 39
Class
- WebformHeight
- Provides a 'height' element.
Namespace
Drupal\webform\Plugin\WebformElementCode
protected function formatTextItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
$value = $this
->getValue($element, $webform_submission, $options);
$format = $this
->getItemFormat($element);
if ($format === 'raw') {
return $value;
}
if (empty($value)) {
return '';
}
$value = (double) $value;
$feet = floor($value / 12);
$inches = $value - $feet * 12;
$height_format = $this
->getElementProperty($element, 'height_format');
switch ($height_format) {
case WebformHeightElement::HEIGHT_SYMBOL:
$feet_plural = '″';
$feet_singular = '″';
$inches_plural = '′';
$inches_singular = '′';
break;
case WebformHeightElement::HEIGHT_ABBREVIATE:
$feet_plural = ' ' . $this
->t('ft', [], [
'context' => 'Imperial height unit abbreviate',
]);
$feet_singular = ' ' . $this
->t('ft', [], [
'context' => 'Imperial height unit abbreviate',
]);
$inches_plural = ' ' . $this
->t('in', [], [
'context' => 'Imperial height unit abbreviate',
]);
$inches_singular = ' ' . $this
->t('in', [], [
'context' => 'Imperial height unit abbreviate',
]);
break;
default:
$feet_plural = ' ' . $this
->t('feet', [], [
'context' => 'Imperial height unit',
]);
$feet_singular = ' ' . $this
->t('foot', [], [
'context' => 'Imperial height unit',
]);
$inches_plural = ' ' . $this
->t('inches', [], [
'context' => 'Imperial height unit',
]);
$inches_singular = ' ' . $this
->t('inch', [], [
'context' => 'Imperial height unit',
]);
break;
}
return ($feet ? $feet . ($feet === 1 ? $feet_singular : $feet_plural) : '') . ($height_format !== WebformHeightElement::HEIGHT_SYMBOL && $feet && $inches ? ' ' : '') . ($inches ? $inches . ($inches === 1 ? $inches_singular : $inches_plural) : '');
}