View source
<?php
namespace Drupal\media\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\image\ImageStyleStorageInterface;
use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\Render\RendererInterface;
use Drupal\media\MediaInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
class MediaThumbnailFormatter extends ImageFormatter {
protected $renderer;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, ImageStyleStorageInterface $image_style_storage, FileUrlGeneratorInterface $file_url_generator, RendererInterface $renderer) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $current_user, $image_style_storage, $file_url_generator);
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('current_user'), $container
->get('entity_type.manager')
->getStorage('image_style'), $container
->get('file_url_generator'), $container
->get('renderer'));
}
protected function needsEntityLoad(EntityReferenceItem $item) {
return !$item
->hasNewEntity();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$link_types = [
'content' => $this
->t('Content'),
'media' => $this
->t('Media item'),
];
$element['image_link']['#options'] = $link_types;
return $element;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$link_types = [
'content' => $this
->t('Linked to content'),
'media' => $this
->t('Linked to media item'),
];
$image_link_setting = $this
->getSetting('image_link');
if (isset($link_types[$image_link_setting])) {
$summary[] = $link_types[$image_link_setting];
}
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$media_items = $this
->getEntitiesToView($items, $langcode);
if (empty($media_items)) {
return $elements;
}
$image_style_setting = $this
->getSetting('image_style');
foreach ($media_items as $delta => $media) {
$elements[$delta] = [
'#theme' => 'image_formatter',
'#item' => $media
->get('thumbnail')
->first(),
'#item_attributes' => [],
'#image_style' => $this
->getSetting('image_style'),
'#url' => $this
->getMediaThumbnailUrl($media, $items
->getEntity()),
];
$this->renderer
->addCacheableDependency($elements[$delta], $media);
}
if ($this
->getSetting('image_link') && ($image_style = $this->imageStyleStorage
->load($image_style_setting))) {
$this->renderer
->addCacheableDependency($elements, $image_style);
}
return $elements;
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return $field_definition
->getFieldStorageDefinition()
->getSetting('target_type') == 'media';
}
protected function getMediaThumbnailUrl(MediaInterface $media, EntityInterface $entity) {
$url = NULL;
$image_link_setting = $this
->getSetting('image_link');
if ($image_link_setting == 'content') {
if (!$entity
->isNew()) {
$url = $entity
->toUrl();
}
}
elseif ($image_link_setting === 'media') {
$url = $media
->toUrl();
}
return $url;
}
}