4060b773eed13db4ec5bb687f2160033c0551129
[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     $size = @filesize($uri);
194
195     // Set size unless there was an error.
196     if ($size !== FALSE) {
197       $this->setSize($size);
198     }
199   }
200
201   /**
202    * {@inheritdoc}
203    */
204   public static function preDelete(EntityStorageInterface $storage, array $entities) {
205     parent::preDelete($storage, $entities);
206
207     foreach ($entities as $entity) {
208       // Delete all remaining references to this file.
209       $file_usage = \Drupal::service('file.usage')->listUsage($entity);
210       if (!empty($file_usage)) {
211         foreach ($file_usage as $module => $usage) {
212           \Drupal::service('file.usage')->delete($entity, $module);
213         }
214       }
215       // Delete the actual file. Failures due to invalid files and files that
216       // were already deleted are logged to watchdog but ignored, the
217       // corresponding file entity will be deleted.
218       file_unmanaged_delete($entity->getFileUri());
219     }
220   }
221
222   /**
223    * {@inheritdoc}
224    */
225   public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
226     /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
227     $fields = parent::baseFieldDefinitions($entity_type);
228
229     $fields['fid']->setLabel(t('File ID'))
230       ->setDescription(t('The file ID.'));
231
232     $fields['uuid']->setDescription(t('The file UUID.'));
233
234     $fields['langcode']->setLabel(t('Language code'))
235       ->setDescription(t('The file language code.'));
236
237     $fields['uid'] = BaseFieldDefinition::create('entity_reference')
238       ->setLabel(t('User ID'))
239       ->setDescription(t('The user ID of the file.'))
240       ->setSetting('target_type', 'user');
241
242     $fields['filename'] = BaseFieldDefinition::create('string')
243       ->setLabel(t('Filename'))
244       ->setDescription(t('Name of the file with no path components.'));
245
246     $fields['uri'] = BaseFieldDefinition::create('file_uri')
247       ->setLabel(t('URI'))
248       ->setDescription(t('The URI to access the file (either local or remote).'))
249       ->setSetting('max_length', 255)
250       ->setSetting('case_sensitive', TRUE)
251       ->addConstraint('FileUriUnique');
252
253     $fields['filemime'] = BaseFieldDefinition::create('string')
254       ->setLabel(t('File MIME type'))
255       ->setSetting('is_ascii', TRUE)
256       ->setDescription(t("The file's MIME type."));
257
258     $fields['filesize'] = BaseFieldDefinition::create('integer')
259       ->setLabel(t('File size'))
260       ->setDescription(t('The size of the file in bytes.'))
261       ->setSetting('unsigned', TRUE)
262       ->setSetting('size', 'big');
263
264     $fields['status'] = BaseFieldDefinition::create('boolean')
265       ->setLabel(t('Status'))
266       ->setDescription(t('The status of the file, temporary (FALSE) and permanent (TRUE).'))
267       ->setDefaultValue(FALSE);
268
269     $fields['created'] = BaseFieldDefinition::create('created')
270       ->setLabel(t('Created'))
271       ->setDescription(t('The timestamp that the file was created.'));
272
273     $fields['changed'] = BaseFieldDefinition::create('changed')
274       ->setLabel(t('Changed'))
275       ->setDescription(t('The timestamp that the file was last changed.'));
276
277     return $fields;
278   }
279
280 }