function template_preprocess_image_url_formatter in Image URL Formatter 8
Returns HTML for an image url field formatter.
Parameters
array $variables: An associative array containing:
- item: An array of image data.
- image_style: An optional image style.
- path: An optional array containing the link 'path' and link 'options'.
File
- ./
image_url_formatter.module, line 38 - Add an URL formatter for image field
Code
function template_preprocess_image_url_formatter(&$variables) {
$item = $variables['item'];
$output = $item->entity
->createFileUrl(FALSE);
if ($variables['image_style']) {
$image['style_name'] = $variables['image_style'];
$output = ImageStyle::load($image['style_name'])
->buildUrl($item->entity
->getFileUri());
}
$url_type = isset($variables['url_type']) ? $variables['url_type'] : 0;
if ($url_type == 1) {
$output = str_replace($GLOBALS['base_url'], '', $output);
}
elseif ($url_type == 2) {
$output = str_replace($GLOBALS['base_url'] . '/', '', $output);
}
// The link path and link options are both optional, but for the options to be
// processed, the link path must at least be an empty string.
if (isset($variables['path']['path'])) {
$path = $variables['path']['path'];
$options = isset($variables['path']['options']) ? $variables['path']['options'] : [];
// When displaying an image inside a link, the html option must be TRUE.
$options['html'] = TRUE;
$output = Link::fromTextAndUrl($output, $path, $options)
->toString();
}
$variables['image'] = $output;
}