You are here

MediaPdfThumbnailFieldFormatter.php in Media PDF Thumbnail 8.3

File

src/Plugin/Field/FieldFormatter/MediaPdfThumbnailFieldFormatter.php
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;

/**
 * Plugin implementation of the 'media_pdf_thumbnail_field_formatter' formatter.
 *
 * @FieldFormatter(
 *   id = "media_pdf_thumbnail_field_formatter",
 *   label = @Translation("Media pdf thumbnail"),
 *   field_types = {
 *     "entity_reference"
 *   }
 * )
 */
class MediaPdfThumbnailFieldFormatter extends MediaThumbnailFormatter {

  /**
   * @var array|mixed|null
   */
  protected $config;

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * MediaPdfThumbnailFieldFormatter constructor.
   *
   * @param $plugin_id
   * @param $plugin_definition
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
   * @param array $settings
   * @param $label
   * @param $view_mode
   * @param array $third_party_settings
   * @param \Drupal\Core\Session\AccountInterface $current_user
   * @param \Drupal\image\ImageStyleStorageInterface $image_style_storage
   * @param \Drupal\Core\Render\RendererInterface $renderer
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $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'));
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $element = parent::settingsForm($form, $form_state);
    $element['image_link']['#options']['file'] = $this
      ->t('File');
    return $element;
  }

  /**
   * @param \Drupal\media\MediaInterface $media
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *
   * @return \Drupal\Core\Url|string|null
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @throws \Drupal\Core\Entity\EntityMalformedException
   */
  protected function getMediaThumbnailUrl(MediaInterface $media, EntityInterface $entity) {
    $url = NULL;
    $image_link_setting = $this
      ->getSetting('image_link');

    // Check if the formatter involves a 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;
  }

}

Classes

Namesort descending Description
MediaPdfThumbnailFieldFormatter Plugin implementation of the 'media_pdf_thumbnail_field_formatter' formatter.