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",
26 * thumbnail_alt_metadata_attribute = "thumbnail_alt_value"
29 class Image extends File {
32 * Key for "image width" metadata attribute.
36 const METADATA_ATTRIBUTE_WIDTH = 'width';
39 * Key for "image height" metadata attribute.
43 const METADATA_ATTRIBUTE_HEIGHT = 'height';
46 * The image factory service.
48 * @var \Drupal\Core\Image\ImageFactory
50 protected $imageFactory;
53 * The file system service.
55 * @var \Drupal\Core\File\FileSystemInterface
57 protected $fileSystem;
60 * Constructs a new class instance.
62 * @param array $configuration
63 * A configuration array containing information about the plugin instance.
64 * @param string $plugin_id
65 * The plugin_id for the plugin instance.
66 * @param mixed $plugin_definition
67 * The plugin implementation definition.
68 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
69 * Entity type manager service.
70 * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
71 * Entity field manager service.
72 * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
73 * The field type plugin manager service.
74 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
75 * The config factory service.
76 * @param \Drupal\Core\Image\ImageFactory $image_factory
78 * @param \Drupal\Core\File\FileSystemInterface $file_system
79 * The file system service.
81 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) {
82 parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $field_type_manager, $config_factory);
84 $this->imageFactory = $image_factory;
85 $this->fileSystem = $file_system;
91 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
96 $container->get('entity_type.manager'),
97 $container->get('entity_field.manager'),
98 $container->get('plugin.manager.field.field_type'),
99 $container->get('config.factory'),
100 $container->get('image.factory'),
101 $container->get('file_system')
108 public function getMetadataAttributes() {
109 $attributes = parent::getMetadataAttributes();
112 static::METADATA_ATTRIBUTE_WIDTH => $this->t('Width'),
113 static::METADATA_ATTRIBUTE_HEIGHT => $this->t('Height'),
122 public function getMetadata(MediaInterface $media, $name) {
123 // Get the file and image data.
124 /** @var \Drupal\file\FileInterface $file */
125 $file = $media->get($this->configuration['source_field'])->entity;
126 // If the source field is not required, it may be empty.
128 return parent::getMetadata($media, $name);
131 $uri = $file->getFileUri();
132 $image = $this->imageFactory->get($uri);
134 case static::METADATA_ATTRIBUTE_WIDTH:
135 return $image->getWidth() ?: NULL;
137 case static::METADATA_ATTRIBUTE_HEIGHT:
138 return $image->getHeight() ?: NULL;
140 case 'thumbnail_uri':
143 case 'thumbnail_alt_value':
144 return $media->get($this->configuration['source_field'])->alt ?: parent::getMetadata($media, $name);
147 return parent::getMetadata($media, $name);
153 public function createSourceField(MediaTypeInterface $type) {
154 /** @var \Drupal\field\FieldConfigInterface $field */
155 $field = parent::createSourceField($type);
157 // Reset the field to its default settings so that we don't inherit the
158 // settings from the parent class' source field.
159 $settings = $this->fieldTypeManager->getDefaultFieldSettings($field->getType());
161 return $field->set('settings', $settings);