X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fmedia%2Fsrc%2FPlugin%2Fmedia%2FSource%2FImage.php;fp=web%2Fcore%2Fmodules%2Fmedia%2Fsrc%2FPlugin%2Fmedia%2FSource%2FImage.php;h=46f6782c742d0715598731ab283c847ff3410eb6;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hp=0000000000000000000000000000000000000000;hpb=aea91e65e895364e460983b890e295aa5d5540a5;p=yaffs-website diff --git a/web/core/modules/media/src/Plugin/media/Source/Image.php b/web/core/modules/media/src/Plugin/media/Source/Image.php new file mode 100644 index 000000000..46f6782c7 --- /dev/null +++ b/web/core/modules/media/src/Plugin/media/Source/Image.php @@ -0,0 +1,160 @@ +imageFactory = $image_factory; + $this->fileSystem = $file_system; + } + + /** + * {@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('plugin.manager.field.field_type'), + $container->get('config.factory'), + $container->get('image.factory'), + $container->get('file_system') + ); + } + + /** + * {@inheritdoc} + */ + public function getMetadataAttributes() { + $attributes = parent::getMetadataAttributes(); + + $attributes += [ + static::METADATA_ATTRIBUTE_WIDTH => $this->t('Width'), + static::METADATA_ATTRIBUTE_HEIGHT => $this->t('Height'), + ]; + + return $attributes; + } + + /** + * {@inheritdoc} + */ + public function getMetadata(MediaInterface $media, $name) { + // Get the file and image data. + /** @var \Drupal\file\FileInterface $file */ + $file = $media->get($this->configuration['source_field'])->entity; + // If the source field is not required, it may be empty. + if (!$file) { + return parent::getMetadata($media, $name); + } + + $uri = $file->getFileUri(); + $image = $this->imageFactory->get($uri); + switch ($name) { + case static::METADATA_ATTRIBUTE_WIDTH: + return $image->getWidth() ?: NULL; + + case static::METADATA_ATTRIBUTE_HEIGHT: + return $image->getHeight() ?: NULL; + + case 'thumbnail_uri': + return $uri; + } + + return parent::getMetadata($media, $name); + } + + /** + * {@inheritdoc} + */ + public function createSourceField(MediaTypeInterface $type) { + /** @var \Drupal\field\FieldConfigInterface $field */ + $field = parent::createSourceField($type); + + // Reset the field to its default settings so that we don't inherit the + // settings from the parent class' source field. + $settings = $this->fieldTypeManager->getDefaultFieldSettings($field->getType()); + + return $field->set('settings', $settings); + } + +}