You are here

class Image in Media entity image 8

Provides media type plugin for Image.

Plugin annotation


@MediaType(
  id = "image",
  label = @Translation("Image"),
  description = @Translation("Provides business logic and metadata for local images.")
)

Hierarchy

Expanded class hierarchy of Image

File

src/Plugin/MediaEntity/Type/Image.php, line 24

Namespace

Drupal\media_entity_image\Plugin\MediaEntity\Type
View source
class Image extends MediaTypeBase {

  /**
   * The image factory service..
   *
   * @var \Drupal\Core\Image\ImageFactory
   */
  protected $imageFactory;

  /**
   * The exif data.
   *
   * @var array
   */
  protected $exif;

  /**
   * Constructs a new class instance.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Entity type manager service.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   Entity field manager service.
   * @param \Drupal\Core\Image\ImageFactory $image_factory
   *   The image factory.
   * @param \Drupal\Core\Config\Config $config
   *   Media entity config object.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, ImageFactory $image_factory, Config $config) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $config);
    $this->imageFactory = $image_factory;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager'), $container
      ->get('entity_field.manager'), $container
      ->get('image.factory'), $container
      ->get('config.factory')
      ->get('media_entity.settings'));
  }

  /**
   * {@inheritdoc}
   */
  public function providedFields() {
    $fields = array(
      'mime' => $this
        ->t('File MIME'),
      'width' => $this
        ->t('Width'),
      'height' => $this
        ->t('Height'),
    );
    if (!empty($this->configuration['gather_exif'])) {
      $fields += array(
        'model' => $this
          ->t('Camera model'),
        'created' => $this
          ->t('Image creation datetime'),
        'iso' => $this
          ->t('Iso'),
        'exposure' => $this
          ->t('Exposure time'),
        'aperture' => $this
          ->t('Aperture value'),
        'focal_length' => $this
          ->t('Focal length'),
      );
    }
    return $fields;
  }

  /**
   * {@inheritdoc}
   */
  public function getField(MediaInterface $media, $name) {
    $source_field = $this->configuration['source_field'];

    // Get the file, image and exif data.

    /** @var \Drupal\file\FileInterface $file */
    $file = $media->{$source_field}->entity;
    $image = $this->imageFactory
      ->get($file
      ->getFileUri());
    $uri = $file
      ->getFileUri();

    // Return the field.
    switch ($name) {
      case 'mime':
        return !$file->filemime
          ->isEmpty() ? $file
          ->getMimeType() : FALSE;
      case 'width':
        $width = $image
          ->getWidth();
        return $width ? $width : FALSE;
      case 'height':
        $height = $image
          ->getHeight();
        return $height ? $height : FALSE;
      case 'size':
        $size = $file
          ->getSize();
        return $size ? $size : FALSE;
    }
    if (!empty($this->configuration['gather_exif']) && function_exists('exif_read_data')) {
      switch ($name) {
        case 'model':
          return $this
            ->getExifField($uri, 'Model');
        case 'created':
          $date = new DrupalDateTime($this
            ->getExifField($uri, 'DateTimeOriginal'));
          return $date
            ->getTimestamp();
        case 'iso':
          return $this
            ->getExifField($uri, 'ISOSpeedRatings');
        case 'exposure':
          return $this
            ->getExifField($uri, 'ExposureTime');
        case 'aperture':
          return $this
            ->getExifField($uri, 'FNumber');
        case 'focal_length':
          return $this
            ->getExifField($uri, 'FocalLength');
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {

    /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
    $bundle = $form_state
      ->getFormObject()
      ->getEntity();
    $options = [];
    $allowed_field_types = [
      'file',
      'image',
    ];
    foreach ($this->entityFieldManager
      ->getFieldDefinitions('media', $bundle
      ->id()) as $field_name => $field) {
      if (in_array($field
        ->getType(), $allowed_field_types) && !$field
        ->getFieldStorageDefinition()
        ->isBaseField()) {
        $options[$field_name] = $field
          ->getLabel();
      }
    }
    $form['source_field'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Field with source information'),
      '#description' => $this
        ->t('Field on media entity that stores Image file. You can create a bundle without selecting a value for this dropdown initially. This dropdown can be populated after adding fields to the bundle.'),
      '#default_value' => empty($this->configuration['source_field']) ? NULL : $this->configuration['source_field'],
      '#options' => $options,
    ];
    $form['gather_exif'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Whether to gather exif data.'),
      '#description' => $this
        ->t('Gather exif data using exif_read_data().'),
      '#default_value' => empty($this->configuration['gather_exif']) || !function_exists('exif_read_data') ? 0 : $this->configuration['gather_exif'],
      '#options' => [
        0 => $this
          ->t('No'),
        1 => $this
          ->t('Yes'),
      ],
      '#ajax' => [
        'callback' => '::ajaxTypeProviderData',
      ],
      '#disabled' => function_exists('exif_read_data') ? FALSE : TRUE,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultThumbnail() {
    return $this->config
      ->get('icon_base') . '/image.png';
  }

  /**
   * {@inheritdoc}
   */
  public function thumbnail(MediaInterface $media) {
    $source_field = $this->configuration['source_field'];

    /** @var \Drupal\file\FileInterface $file */
    if ($file = $media->{$source_field}->entity) {
      return $file
        ->getFileUri();
    }
    return $this
      ->getDefaultThumbnail();
  }

  /**
   * Get exif field value.
   *
   * @param string $uri
   *   The uri for the file that we are getting the Exif.
   * @param string $field
   *   The name of the exif field.
   *
   * @return string|bool
   *   The value for the requested field or FALSE if is not set.
   */
  protected function getExifField($uri, $field) {
    if (empty($this->exif)) {
      $this->exif = $this
        ->getExif($uri);
    }
    return !empty($this->exif[$field]) ? $this->exif[$field] : FALSE;
  }

  /**
   * Read EXIF.
   *
   * @param string $uri
   *   The uri for the file that we are getting the Exif.
   *
   * @return array|bool
   *   An associative array where the array indexes are the header names and
   *   the array values are the values associated with those headers or FALSE
   *   if the data can't be read.
   */
  protected function getExif($uri) {
    return exif_read_data($uri, 'EXIF');
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultName(MediaInterface $media) {

    // The default name will be the filename of the source_field, if present,
    // or the parent's defaultName implementation if it was not possible to
    // retrieve the filename.
    $source_field = $this->configuration['source_field'];

    /** @var \Drupal\file\FileInterface $file */
    if (!empty($source_field) && ($file = $media->{$source_field}->entity)) {
      return $file
        ->getFilename();
    }
    return parent::getDefaultName($media);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Image::$exif protected property The exif data.
Image::$imageFactory protected property The image factory service..
Image::buildConfigurationForm public function Form constructor. Overrides MediaTypeBase::buildConfigurationForm
Image::create public static function Creates an instance of the plugin. Overrides MediaTypeBase::create
Image::getDefaultName public function Provide a default name for the media. Overrides MediaTypeBase::getDefaultName
Image::getDefaultThumbnail public function Gets the default thumbnail image. Overrides MediaTypeBase::getDefaultThumbnail
Image::getExif protected function Read EXIF.
Image::getExifField protected function Get exif field value.
Image::getField public function Gets a media-related field/value. Overrides MediaTypeInterface::getField
Image::providedFields public function Gets list of fields provided by this plugin. Overrides MediaTypeInterface::providedFields
Image::thumbnail public function Gets thumbnail image. Overrides MediaTypeInterface::thumbnail
Image::__construct public function Constructs a new class instance. Overrides MediaTypeBase::__construct
MediaTypeBase::$config protected property Media entity image config object.
MediaTypeBase::$entityFieldManager protected property The entity field manager service.
MediaTypeBase::$entityTypeManager protected property The entity type manager service.
MediaTypeBase::$label protected property Plugin label.
MediaTypeBase::attachConstraints public function Attaches type-specific constraints to media. Overrides MediaTypeInterface::attachConstraints
MediaTypeBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
MediaTypeBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurablePluginInterface::defaultConfiguration 1
MediaTypeBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurablePluginInterface::getConfiguration
MediaTypeBase::label public function Returns the display label. Overrides MediaTypeInterface::label
MediaTypeBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurablePluginInterface::setConfiguration
MediaTypeBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
MediaTypeBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.