image_url_formatter.module in Image URL Formatter 8
Same filename and directory in other branches
Add an URL formatter for image field
File
image_url_formatter.moduleView source
<?php
/**
* @file
* Add an URL formatter for image field
*/
use Drupal\Core\Link;
use Drupal\image\Entity\ImageStyle;
/**
* Implements hook_theme().
*/
function image_url_formatter_theme() {
return [
'image_url_formatter' => [
'variables' => [
'item' => NULL,
'url' => NULL,
'image_style' => NULL,
'url_type' => NULL,
],
],
];
}
/**
* Returns HTML for an image url field formatter.
*
* @param 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'.
*
* @ingroup themeable
*/
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;
}
Functions
Name | Description |
---|---|
image_url_formatter_theme | Implements hook_theme(). |
template_preprocess_image_url_formatter | Returns HTML for an image url field formatter. |