protected function WebformImageSelect::formatHtmlItem in Webform 6.x
Same name and namespace in other branches
- 8.5 modules/webform_image_select/src/Plugin/WebformElement/WebformImageSelect.php \Drupal\webform_image_select\Plugin\WebformElement\WebformImageSelect::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 OptionsBase::formatHtmlItem
File
- modules/
webform_image_select/ src/ Plugin/ WebformElement/ WebformImageSelect.php, line 111
Class
- WebformImageSelect
- Provides a 'image_select' element.
Namespace
Drupal\webform_image_select\Plugin\WebformElementCode
protected function formatHtmlItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
$value = $this
->getValue($element, $webform_submission, $options);
$format = $this
->getItemFormat($element);
if ($format === 'image') {
if (isset($element['#images'][$value]) && isset($element['#images'][$value]['src'])) {
$src = $element['#images'][$value]['src'];
// Always use absolute URLs for the src so that it will load via e-mail.
if (strpos($src, '/') === 0) {
$src = $this->request
->getSchemeAndHttpHost() . $src;
}
$image = [
'#theme' => 'image',
// ISSUE:
// Image src must be an absolute URL so that it can be sent
// via e-mail but template_preprocess_image() converts the #uri to
// a root-relative URL.
// @see template_preprocess_image()
//
// SOLUTION:
// Using 'src' attributes to prevent the #uri from being converted to
// a root-relative paths.
'#attributes' => [
'src' => $src,
],
'#title' => $element['#images'][$value]['text'],
'#alt' => $element['#images'][$value]['text'],
];
// Suppress all image size errors.
if ($image_size = @getimagesize($element['#images'][$value]['src'])) {
$image['#width'] = $image_size[0];
$image['#height'] = $image_size[1];
}
// For the Results table always just return the image with tooltip.
if (strpos($this->routeMatch
->getRouteName(), 'webform.results_submissions') !== FALSE) {
$image['#attached']['library'][] = 'webform/webform.tooltip';
$image['#attributes']['class'] = [
'js-webform-tooltip-link',
];
return $image;
}
$build = [
'#prefix' => new FormattableMarkup('<figure style="display: inline-block; margin: 0 6px 6px 0; padding: 6px; border: 1px solid #ddd;' . (isset($image['#width']) ? 'width: ' . $image['#width'] . 'px' : '') . '">', []),
'#suffix' => '</figure>',
'image' => $image,
];
if (!empty($element['#show_label'])) {
$build['caption'] = [
'#markup' => $element['#images'][$value]['text'],
'#prefix' => '<figcaption>',
'#suffix' => '</figcaption>',
];
}
return $build;
}
else {
return $value;
}
}
else {
return parent::formatHtmlItem($element, $webform_submission, $options);
}
}