0f7631ec2771516af230a81591608e766a66dd5a
[yaffs-website] / web / core / modules / media / src / Plugin / media / Source / File.php
1 <?php
2
3 namespace Drupal\media\Plugin\media\Source;
4
5 use Drupal\file\FileInterface;
6 use Drupal\media\MediaInterface;
7 use Drupal\media\MediaTypeInterface;
8 use Drupal\media\MediaSourceBase;
9
10 /**
11  * File entity media source.
12  *
13  * @see \Drupal\file\FileInterface
14  *
15  * @MediaSource(
16  *   id = "file",
17  *   label = @Translation("File"),
18  *   description = @Translation("Use local files for reusable media."),
19  *   allowed_field_types = {"file"},
20  *   default_thumbnail_filename = "generic.png"
21  * )
22  */
23 class File extends MediaSourceBase {
24
25   /**
26    * Key for "MIME type" metadata attribute.
27    *
28    * @var string
29    */
30   const METADATA_ATTRIBUTE_MIME = 'mimetype';
31
32   /**
33    * Key for "File size" metadata attribute.
34    *
35    * @var string
36    */
37   const METADATA_ATTRIBUTE_SIZE = 'filesize';
38
39
40   /**
41    * {@inheritdoc}
42    */
43   public function getMetadataAttributes() {
44     return [
45       static::METADATA_ATTRIBUTE_MIME => $this->t('MIME type'),
46       static::METADATA_ATTRIBUTE_SIZE => $this->t('File size'),
47     ];
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public function getMetadata(MediaInterface $media, $attribute_name) {
54     /** @var \Drupal\file\FileInterface $file */
55     $file = $media->get($this->configuration['source_field'])->entity;
56     // If the source field is not required, it may be empty.
57     if (!$file) {
58       return parent::getMetadata($media, $attribute_name);
59     }
60     switch ($attribute_name) {
61       case 'mimetype':
62         return $file->getMimeType();
63
64       case 'filesize':
65         return $file->getSize();
66
67       case 'default_name':
68         return $file->getFilename();
69
70       case 'thumbnail_uri':
71         return $this->getThumbnail($file) ?: parent::getMetadata($media, $attribute_name);
72
73       default:
74         return parent::getMetadata($media, $attribute_name);
75     }
76   }
77
78   /**
79    * Gets the thumbnail image URI based on a file entity.
80    *
81    * @param \Drupal\file\FileInterface $file
82    *   A file entity.
83    *
84    * @return string
85    *   File URI of the thumbnail image or NULL if there is no specific icon.
86    */
87   protected function getThumbnail(FileInterface $file) {
88     $icon_base = $this->configFactory->get('media.settings')->get('icon_base_uri');
89
90     // We try to automatically use the most specific icon present in the
91     // $icon_base directory, based on the MIME type. For instance, if an
92     // icon file named "pdf.png" is present, it will be used if the file
93     // matches this MIME type.
94     $mimetype = $file->getMimeType();
95     $mimetype = explode('/', $mimetype);
96
97     $icon_names = [
98       $mimetype[0] . '--' . $mimetype[1],
99       $mimetype[1],
100       $mimetype[0],
101     ];
102     foreach ($icon_names as $icon_name) {
103       $thumbnail = $icon_base . '/' . $icon_name . '.png';
104       if (is_file($thumbnail)) {
105         return $thumbnail;
106       }
107     }
108
109     return NULL;
110   }
111
112   /**
113    * {@inheritdoc}
114    */
115   public function createSourceField(MediaTypeInterface $type) {
116     return parent::createSourceField($type)->set('settings', ['file_extensions' => 'txt doc docx pdf']);
117   }
118
119 }