Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[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  * )
27  */
28 class Image extends File {
29
30   /**
31    * Key for "image width" metadata attribute.
32    *
33    * @var string
34    */
35   const METADATA_ATTRIBUTE_WIDTH = 'width';
36
37   /**
38    * Key for "image height" metadata attribute.
39    *
40    * @var string
41    */
42   const METADATA_ATTRIBUTE_HEIGHT = 'height';
43
44   /**
45    * The image factory service.
46    *
47    * @var \Drupal\Core\Image\ImageFactory
48    */
49   protected $imageFactory;
50
51   /**
52    * The file system service.
53    *
54    * @var \Drupal\Core\File\FileSystemInterface
55    */
56   protected $fileSystem;
57
58   /**
59    * Constructs a new class instance.
60    *
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
76    *   The image factory.
77    * @param \Drupal\Core\File\FileSystemInterface $file_system
78    *   The file system service.
79    */
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);
82
83     $this->imageFactory = $image_factory;
84     $this->fileSystem = $file_system;
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
91     return new static(
92       $configuration,
93       $plugin_id,
94       $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')
101     );
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   public function getMetadataAttributes() {
108     $attributes = parent::getMetadataAttributes();
109
110     $attributes += [
111       static::METADATA_ATTRIBUTE_WIDTH => $this->t('Width'),
112       static::METADATA_ATTRIBUTE_HEIGHT => $this->t('Height'),
113     ];
114
115     return $attributes;
116   }
117
118   /**
119    * {@inheritdoc}
120    */
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.
126     if (!$file) {
127       return parent::getMetadata($media, $name);
128     }
129
130     $uri = $file->getFileUri();
131     $image = $this->imageFactory->get($uri);
132     switch ($name) {
133       case static::METADATA_ATTRIBUTE_WIDTH:
134         return $image->getWidth() ?: NULL;
135
136       case static::METADATA_ATTRIBUTE_HEIGHT:
137         return $image->getHeight() ?: NULL;
138
139       case 'thumbnail_uri':
140         return $uri;
141     }
142
143     return parent::getMetadata($media, $name);
144   }
145
146   /**
147    * {@inheritdoc}
148    */
149   public function createSourceField(MediaTypeInterface $type) {
150     /** @var \Drupal\field\FieldConfigInterface $field */
151     $field = parent::createSourceField($type);
152
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());
156
157     return $field->set('settings', $settings);
158   }
159
160 }