View source
<?php
namespace Drupal\media_pdf_thumbnail\Plugin\Field\FieldFormatter;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\image\ImageStyleStorageInterface;
use Drupal\media\MediaInterface;
use Drupal\media\Plugin\Field\FieldFormatter\MediaThumbnailFormatter;
use Drupal\media_pdf_thumbnail\Form\BundlesSettingsForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MediaPdfThumbnailFieldFormatter extends MediaThumbnailFormatter {
protected $config;
protected $entityTypeManager;
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, RendererInterface $renderer, ConfigFactoryInterface $config, EntityTypeManagerInterface $entityTypeManager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $current_user, $image_style_storage, $renderer);
$this->config = $config
->get(BundlesSettingsForm::SETTINGS);
$this->entityTypeManager = $entityTypeManager;
}
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('renderer'), $container
->get('config.factory'), $container
->get('entity_type.manager'));
}
public static function defaultSettings() {
return [] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$element['image_link']['#options']['file'] = $this
->t('File');
return $element;
}
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();
}
elseif ('file') {
$bundle = $media
->bundle();
$data = $this->config
->getRawData();
if (!empty($data[$bundle . '_enable'])) {
$field = !empty($data[$bundle . '_field']) ? $data[$bundle . '_field'] : NULL;
if ($field) {
$fieldValue = $media
->get($field)
->getValue();
$fid = !empty($fieldValue[0]['target_id']) ? $fieldValue[0]['target_id'] : NULL;
$fileEntity = $fid ? $this->entityTypeManager
->getStorage('file')
->load($fid) : NULL;
$url = $fileEntity ? file_create_url($fileEntity
->getFileUri()) : '';
}
}
}
return $url;
}
}