3 namespace Drupal\media\Plugin\media\Source;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Entity\EntityFieldManagerInterface;
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
8 use Drupal\Core\Field\FieldTypePluginManagerInterface;
9 use Drupal\Core\File\FileSystemInterface;
10 use Drupal\Core\Image\ImageFactory;
11 use Drupal\media\MediaInterface;
12 use Drupal\media\MediaTypeInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Image entity media source.
18 * @see \Drupal\Core\Image\ImageInterface
22 * label = @Translation("Image"),
23 * description = @Translation("Use local images for reusable media."),
24 * allowed_field_types = {"image"},
25 * default_thumbnail_filename = "no-thumbnail.png"
28 class Image extends File {
31 * Key for "image width" metadata attribute.
35 const METADATA_ATTRIBUTE_WIDTH = 'width';
38 * Key for "image height" metadata attribute.
42 const METADATA_ATTRIBUTE_HEIGHT = 'height';
45 * The image factory service.
47 * @var \Drupal\Core\Image\ImageFactory
49 protected $imageFactory;
52 * The file system service.
54 * @var \Drupal\Core\File\FileSystemInterface
56 protected $fileSystem;
59 * Constructs a new class instance.
61 * @param array $configuration
62 * A configuration array containing information about the plugin instance.
63 * @param string $plugin_id
64 * The plugin_id for the plugin instance.
65 * @param mixed $plugin_definition
66 * The plugin implementation definition.
67 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
68 * Entity type manager service.
69 * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
70 * Entity field manager service.
71 * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
72 * The field type plugin manager service.
73 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
74 * The config factory service.
75 * @param \Drupal\Core\Image\ImageFactory $image_factory
77 * @param \Drupal\Core\File\FileSystemInterface $file_system
78 * The file system service.
80 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, ConfigFactoryInterface $config_factory, ImageFactory $image_factory, FileSystemInterface $file_system) {
81 parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $field_type_manager, $config_factory);
83 $this->imageFactory = $image_factory;
84 $this->fileSystem = $file_system;
90 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
95 $container->get('entity_type.manager'),
96 $container->get('entity_field.manager'),
97 $container->get('plugin.manager.field.field_type'),
98 $container->get('config.factory'),
99 $container->get('image.factory'),
100 $container->get('file_system')
107 public function getMetadataAttributes() {
108 $attributes = parent::getMetadataAttributes();
111 static::METADATA_ATTRIBUTE_WIDTH => $this->t('Width'),
112 static::METADATA_ATTRIBUTE_HEIGHT => $this->t('Height'),
121 public function getMetadata(MediaInterface $media, $name) {
122 // Get the file and image data.
123 /** @var \Drupal\file\FileInterface $file */
124 $file = $media->get($this->configuration['source_field'])->entity;
125 // If the source field is not required, it may be empty.
127 return parent::getMetadata($media, $name);
130 $uri = $file->getFileUri();
131 $image = $this->imageFactory->get($uri);
133 case static::METADATA_ATTRIBUTE_WIDTH:
134 return $image->getWidth() ?: NULL;
136 case static::METADATA_ATTRIBUTE_HEIGHT:
137 return $image->getHeight() ?: NULL;
139 case 'thumbnail_uri':
143 return parent::getMetadata($media, $name);
149 public function createSourceField(MediaTypeInterface $type) {
150 /** @var \Drupal\field\FieldConfigInterface $field */
151 $field = parent::createSourceField($type);
153 // Reset the field to its default settings so that we don't inherit the
154 // settings from the parent class' source field.
155 $settings = $this->fieldTypeManager->getDefaultFieldSettings($field->getType());
157 return $field->set('settings', $settings);