Skip to content

Instantly share code, notes, and snippets.

@onesixromcom
Created July 30, 2023 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onesixromcom/950413ca4be1e8b0633092445c12f48b to your computer and use it in GitHub Desktop.
Save onesixromcom/950413ca4be1e8b0633092445c12f48b to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\my_module\Plugin\Field\FieldFormatter;
use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Cache\Cache;
/**
* Plugin implementation of the 'image_default_fallback' formatter.
*
* @FieldFormatter(
* id = "image_default_fallback",
* label = @Translation("Image (default fallback)"),
* field_types = {
* "image"
* }
* )
*/
class ImageDefaultFallbackFormatter extends ImageFormatter {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$url = NULL;
$image_link_setting = $this->getSetting('image_link');
// Check if the formatter involves a link.
if ($image_link_setting == 'content') {
$entity = $items->getEntity();
if (!$entity->isNew()) {
$url = $entity->toUrl();
}
}
elseif ($image_link_setting == 'file') {
$link_file = TRUE;
}
$image_style_setting = $this->getSetting('image_style');
// Collect cache tags to be added for each item in the field.
$base_cache_tags = [];
if (!empty($image_style_setting)) {
$image_style = $this->imageStyleStorage->load($image_style_setting);
$base_cache_tags = $image_style->getCacheTags();
}
// Display default image if items are empty.
if ($items->isEmpty()) {
$item = new \stdClass();
$item->title = '';
$item->entity = NULL;
$item->width = 1000;
$item->height = 1000;
$item->alt = '';
$item->uri = 'module://my_module/images/default-image.png';
$elements[] = [
'#theme' => 'image_formatter',
'#item' => $item,
'#image_style' => $image_style_setting,
];
return $elements;
}
$files = $this->getEntitiesToView($items, $langcode);
// Early opt-out if the field is empty.
if (empty($files)) {
return $elements;
}
foreach ($files as $delta => $file) {
if (isset($link_file)) {
$image_uri = $file->getFileUri();
$url = $this->fileUrlGenerator->generate($image_uri);
}
$cache_tags = Cache::mergeTags($base_cache_tags, $file->getCacheTags());
// Extract field item attributes for the theme function, and unset them
// from the $item so that the field template does not re-render them.
$item = $file->_referringItem;
$item_attributes = $item->_attributes;
unset($item->_attributes);
$image_loading_settings = $this->getSetting('image_loading');
$item_attributes['loading'] = $image_loading_settings['attribute'];
$elements[$delta] = [
'#theme' => 'image_formatter',
'#item' => $item,
'#item_attributes' => $item_attributes,
'#image_style' => $image_style_setting,
'#url' => $url,
'#cache' => [
'tags' => $cache_tags,
],
];
}
return $elements;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment