Pull merge.
[yaffs-website] / web / core / modules / media / src / Plugin / media / Source / Image.php
1 <?php
2
3 namespace Drupal\media\Plugin\media\Source;
4
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;
14
15 /**
16  * Image entity media source.
17  *
18  * @see \Drupal\Core\Image\ImageInterface
19  *
20  * @MediaSource(
21  *   id = "image",
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"
27  * )
28  */
29 class Image extends File {
30
31   /**
32    * Key for "image width" metadata attribute.
33    *
34    * @var string
35    */
36   const METADATA_ATTRIBUTE_WIDTH = 'width';
37
38   /**
39    * Key for "image height" metadata attribute.
40    *
41    * @var string
42    */
43   const METADATA_ATTRIBUTE_HEIGHT = 'height';
44
45   /**
46    * The image factory service.
47    *
48    * @var \Drupal\Core\Image\ImageFactory
49    */
50   protected $imageFactory;
51
52   /**
53    * The file system service.
54    *
55    * @var \Drupal\Core\File\FileSystemInterface
56    */
57   protected $fileSystem;
58
59   /**
60    * Constructs a new class instance.
61    *
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
77    *   The image factory.
78    * @param \Drupal\Core\File\FileSystemInterface $file_system
79    *   The file system service.
80    */
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);
83
84     $this->imageFactory = $image_factory;
85     $this->fileSystem = $file_system;
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
92     return new static(
93       $configuration,
94       $plugin_id,
95       $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')
102     );
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   public function getMetadataAttributes() {
109     $attributes = parent::getMetadataAttributes();
110
111     $attributes += [
112       static::METADATA_ATTRIBUTE_WIDTH => $this->t('Width'),
113       static::METADATA_ATTRIBUTE_HEIGHT => $this->t('Height'),
114     ];
115
116     return $attributes;
117   }
118
119   /**
120    * {@inheritdoc}
121    */
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.
127     if (!$file) {
128       return parent::getMetadata($media, $name);
129     }
130
131     $uri = $file->getFileUri();
132     $image = $this->imageFactory->get($uri);
133     switch ($name) {
134       case static::METADATA_ATTRIBUTE_WIDTH:
135         return $image->getWidth() ?: NULL;
136
137       case static::METADATA_ATTRIBUTE_HEIGHT:
138         return $image->getHeight() ?: NULL;
139
140       case 'thumbnail_uri':
141         return $uri;
142
143       case 'thumbnail_alt_value':
144         return $media->get($this->configuration['source_field'])->alt ?: parent::getMetadata($media, $name);
145     }
146
147     return parent::getMetadata($media, $name);
148   }
149
150   /**
151    * {@inheritdoc}
152    */
153   public function createSourceField(MediaTypeInterface $type) {
154     /** @var \Drupal\field\FieldConfigInterface $field */
155     $field = parent::createSourceField($type);
156
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());
160
161     return $field->set('settings', $settings);
162   }
163
164 }