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