
| Current Path : /var/www/html_old/abm/web/core/modules/image/src/Plugin/Field/FieldFormatter/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : /var/www/html_old/abm/web/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php |
<?php
namespace Drupal\image\Plugin\Field\FieldFormatter;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'image_url' formatter.
*
* @FieldFormatter(
* id = "image_url",
* label = @Translation("URL to image"),
* field_types = {
* "image"
* }
* )
*/
class ImageUrlFormatter extends ImageFormatter {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'image_style' => '',
];
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
unset($element['image_link']);
return $element;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
return [$summary[0]];
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
/** @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $items */
if (empty($images = $this->getEntitiesToView($items, $langcode))) {
// Early opt-out if the field is empty.
return $elements;
}
/** @var \Drupal\image\ImageStyleInterface $image_style */
$image_style = $this->imageStyleStorage->load($this->getSetting('image_style'));
/** @var \Drupal\file\FileInterface[] $images */
foreach ($images as $delta => $image) {
$image_uri = $image->getFileUri();
$url = $image_style ? $image_style->buildUrl($image_uri) : file_create_url($image_uri);
$url = file_url_transform_relative($url);
// Add cacheability metadata from the image and image style.
$cacheability = CacheableMetadata::createFromObject($image);
if ($image_style) {
$cacheability->addCacheableDependency(CacheableMetadata::createFromObject($image_style));
}
$elements[$delta] = ['#markup' => $url];
$cacheability->applyTo($elements[$delta]);
}
return $elements;
}
}