a68b9bae9bcf5ef87098a9a592aae15ddea7302c
[yaffs-website] / web / core / modules / file / src / Entity / File.php
1 <?php
2
3 namespace Drupal\file\Entity;
4
5 use Drupal\Core\Entity\ContentEntityBase;
6 use Drupal\Core\Entity\EntityChangedTrait;
7 use Drupal\Core\Entity\EntityStorageInterface;
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\Core\Field\BaseFieldDefinition;
10 use Drupal\file\FileInterface;
11 use Drupal\user\UserInterface;
12
13 /**
14  * Defines the file entity class.
15  *
16  * @ingroup file
17  *
18  * @ContentEntityType(
19  *   id = "file",
20  *   label = @Translation("File"),
21  *   handlers = {
22  *     "storage" = "Drupal\file\FileStorage",
23  *     "storage_schema" = "Drupal\file\FileStorageSchema",
24  *     "access" = "Drupal\file\FileAccessControlHandler",
25  *     "views_data" = "Drupal\file\FileViewsData",
26  *   },
27  *   base_table = "file_managed",
28  *   entity_keys = {
29  *     "id" = "fid",
30  *     "label" = "filename",
31  *     "langcode" = "langcode",
32  *     "uuid" = "uuid"
33  *   }
34  * )
35  */
36 class File extends ContentEntityBase implements FileInterface {
37
38   use EntityChangedTrait;
39
40   /**
41    * {@inheritdoc}
42    */
43   public function getFilename() {
44     return $this->get('filename')->value;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public function setFilename($filename) {
51     $this->get('filename')->value = $filename;
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function getFileUri() {
58     return $this->get('uri')->value;
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function setFileUri($uri) {
65     $this->get('uri')->value = $uri;
66   }
67
68   /**
69    * {@inheritdoc}
70    *
71    * @see file_url_transform_relative()
72    */
73   public function url($rel = 'canonical', $options = []) {
74     return file_create_url($this->getFileUri());
75   }
76
77   /**
78    * {@inheritdoc}
79    */
80   public function getMimeType() {
81     return $this->get('filemime')->value;
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function setMimeType($mime) {
88     $this->get('filemime')->value = $mime;
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function getSize() {
95     return $this->get('filesize')->value;
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function setSize($size) {
102     $this->get('filesize')->value = $size;
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   public function getCreatedTime() {
109     return $this->get('created')->value;
110   }
111
112   /**
113    * {@inheritdoc}
114    */
115   public function getOwner() {
116     return $this->get('uid')->entity;
117   }
118
119   /**
120    * {@inheritdoc}
121    */
122   public function getOwnerId() {
123     return $this->get('uid')->target_id;
124   }
125
126   /**
127    * {@inheritdoc}
128    */
129   public function setOwnerId($uid) {
130     $this->set('uid', $uid);
131     return $this;
132   }
133
134   /**
135    * {@inheritdoc}
136    */
137   public function setOwner(UserInterface $account) {
138     $this->set('uid', $account->id());
139     return $this;
140   }
141
142   /**
143    * {@inheritdoc}
144    */
145   public function isPermanent() {
146     return $this->get('status')->value == FILE_STATUS_PERMANENT;
147   }
148
149   /**
150    * {@inheritdoc}
151    */
152   public function isTemporary() {
153     return $this->get('status')->value == 0;
154   }
155
156   /**
157    * {@inheritdoc}
158    */
159   public function setPermanent() {
160     $this->get('status')->value = FILE_STATUS_PERMANENT;
161   }
162
163   /**
164    * {@inheritdoc}
165    */
166   public function setTemporary() {
167     $this->get('status')->value = 0;
168   }
169
170   /**
171    * {@inheritdoc}
172    */
173   public static function preCreate(EntityStorageInterface $storage, array &$values) {
174     // Automatically detect filename if not set.
175     if (!isset($values['filename']) && isset($values['uri'])) {
176       $values['filename'] = drupal_basename($values['uri']);
177     }
178
179     // Automatically detect filemime if not set.
180     if (!isset($values['filemime']) && isset($values['uri'])) {
181       $values['filemime'] = \Drupal::service('file.mime_type.guesser')->guess($values['uri']);
182     }
183   }
184
185   /**
186    * {@inheritdoc}
187    */
188   public function preSave(EntityStorageInterface $storage) {
189     parent::preSave($storage);
190
191     // The file itself might not exist or be available right now.
192     $uri = $this->getFileUri();
193     if ($size = @filesize($uri)) {
194       $this->setSize($size);
195     }
196   }
197
198   /**
199    * {@inheritdoc}
200    */
201   public static function preDelete(EntityStorageInterface $storage, array $entities) {
202     parent::preDelete($storage, $entities);
203
204     foreach ($entities as $entity) {
205       // Delete all remaining references to this file.
206       $file_usage = \Drupal::service('file.usage')->listUsage($entity);
207       if (!empty($file_usage)) {
208         foreach ($file_usage as $module => $usage) {
209           \Drupal::service('file.usage')->delete($entity, $module);
210         }
211       }
212       // Delete the actual file. Failures due to invalid files and files that
213       // were already deleted are logged to watchdog but ignored, the
214       // corresponding file entity will be deleted.
215       file_unmanaged_delete($entity->getFileUri());
216     }
217   }
218
219   /**
220    * {@inheritdoc}
221    */
222   public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
223     /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
224     $fields = parent::baseFieldDefinitions($entity_type);
225
226     $fields['fid']->setLabel(t('File ID'))
227       ->setDescription(t('The file ID.'));
228
229     $fields['uuid']->setDescription(t('The file UUID.'));
230
231     $fields['langcode']->setLabel(t('Language code'))
232       ->setDescription(t('The file language code.'));
233
234     $fields['uid'] = BaseFieldDefinition::create('entity_reference')
235       ->setLabel(t('User ID'))
236       ->setDescription(t('The user ID of the file.'))
237       ->setSetting('target_type', 'user');
238
239     $fields['filename'] = BaseFieldDefinition::create('string')
240       ->setLabel(t('Filename'))
241       ->setDescription(t('Name of the file with no path components.'));
242
243     $fields['uri'] = BaseFieldDefinition::create('uri')
244       ->setLabel(t('URI'))
245       ->setDescription(t('The URI to access the file (either local or remote).'))
246       ->setSetting('max_length', 255)
247       ->setSetting('case_sensitive', TRUE)
248       ->addConstraint('FileUriUnique');
249
250     $fields['filemime'] = BaseFieldDefinition::create('string')
251       ->setLabel(t('File MIME type'))
252       ->setSetting('is_ascii', TRUE)
253       ->setDescription(t("The file's MIME type."));
254
255     $fields['filesize'] = BaseFieldDefinition::create('integer')
256       ->setLabel(t('File size'))
257       ->setDescription(t('The size of the file in bytes.'))
258       ->setSetting('unsigned', TRUE)
259       ->setSetting('size', 'big');
260
261     $fields['status'] = BaseFieldDefinition::create('boolean')
262       ->setLabel(t('Status'))
263       ->setDescription(t('The status of the file, temporary (FALSE) and permanent (TRUE).'))
264       ->setDefaultValue(FALSE);
265
266     $fields['created'] = BaseFieldDefinition::create('created')
267       ->setLabel(t('Created'))
268       ->setDescription(t('The timestamp that the file was created.'));
269
270     $fields['changed'] = BaseFieldDefinition::create('changed')
271       ->setLabel(t('Changed'))
272       ->setDescription(t('The timestamp that the file was last changed.'));
273
274     return $fields;
275   }
276
277 }