File.php 6.78 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\file\Entity\File.
6 7
 */

8
namespace Drupal\file\Entity;
9

10
use Drupal\Core\Entity\ContentEntityBase;
11
use Drupal\Core\Entity\EntityStorageInterface;
12
use Drupal\Core\Entity\EntityTypeInterface;
13
use Drupal\Core\Field\BaseFieldDefinition;
14
use Drupal\Core\Language\LanguageInterface;
15
use Drupal\file\FileInterface;
16
use Drupal\user\UserInterface;
17 18 19

/**
 * Defines the file entity class.
20
 *
21
 * @ContentEntityType(
22 23
 *   id = "file",
 *   label = @Translation("File"),
24
 *   handlers = {
25
 *     "storage" = "Drupal\file\FileStorage",
26
 *     "storage_schema" = "Drupal\file\FileStorageSchema",
27
 *     "access" = "Drupal\file\FileAccessControlHandler",
28
 *     "views_data" = "Drupal\file\FileViewsData",
29
 *   },
30 31 32 33
 *   base_table = "file_managed",
 *   entity_keys = {
 *     "id" = "fid",
 *     "label" = "filename",
34
 *     "langcode" = "langcode",
35 36 37
 *     "uuid" = "uuid"
 *   }
 * )
38
 */
39
class File extends ContentEntityBase implements FileInterface {
40 41

  /**
42
   * {@inheritdoc}
43
   */
44 45 46
  public function getFilename() {
    return $this->get('filename')->value;
  }
47 48

  /**
49
   * {@inheritdoc}
50
   */
51 52 53
  public function setFilename($filename) {
    $this->get('filename')->value = $filename;
  }
54 55

  /**
56
   * {@inheritdoc}
57
   */
58 59 60
  public function getFileUri() {
    return $this->get('uri')->value;
  }
61 62

  /**
63
   * {@inheritdoc}
64
   */
65 66 67
  public function setFileUri($uri) {
    $this->get('uri')->value = $uri;
  }
68 69

  /**
70
   * {@inheritdoc}
71 72 73 74 75 76 77
   */
  public function url($rel = 'canonical', $options = array()) {
    return file_create_url($this->getFileUri());
  }

  /**
   * {@inheritdoc}
78
   */
79 80 81
  public function getMimeType() {
    return $this->get('filemime')->value;
  }
82 83

  /**
84
   * {@inheritdoc}
85
   */
86 87 88
  public function setMimeType($mime) {
    $this->get('filemime')->value = $mime;
  }
89 90

  /**
91
   * {@inheritdoc}
92
   */
93 94 95
  public function getSize() {
    return $this->get('filesize')->value;
  }
96 97

  /**
98
   * {@inheritdoc}
99
   */
100 101 102
  public function setSize($size) {
    $this->get('filesize')->value = $size;
  }
103

104 105 106 107 108 109 110
  /**
   * {@inheritdoc}
   */
  public function getCreatedTime() {
    return $this->get('created')->value;
  }

111
  /**
112
   * {@inheritdoc}
113
   */
114
  public function getChangedTime() {
115
    return $this->get('changed')->value;
116 117 118 119 120 121
  }

  /**
   * {@inheritdoc}
   */
  public function getOwner() {
122
    return $this->get('uid')->entity;
123 124 125 126 127
  }

  /**
   * {@inheritdoc}
   */
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  public function getOwnerId() {
    return $this->get('uid')->target_id;
  }

  /**
   * {@inheritdoc}
   */
  public function setOwnerId($uid) {
    $this->set('uid', $uid);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setOwner(UserInterface $account) {
    $this->set('uid', $account->id());
    return $this;
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  }

  /**
   * {@inheritdoc}
   */
  public function isPermanent() {
    return $this->get('status')->value == FILE_STATUS_PERMANENT;
  }

  /**
   * {@inheritdoc}
   */
  public function isTemporary() {
    return $this->get('status')->value == 0;
  }

  /**
   * {@inheritdoc}
   */
  public function setPermanent() {
    $this->get('status')->value = FILE_STATUS_PERMANENT;
  }

  /**
   * {@inheritdoc}
   */
  public function setTemporary() {
    $this->get('status')->value = 0;
174 175
  }

176 177 178
  /**
   * {@inheritdoc}
   */
179
  public static function preCreate(EntityStorageInterface $storage, array &$values) {
180 181 182 183 184 185 186
    // Automatically detect filename if not set.
    if (!isset($values['filename']) && isset($values['uri'])) {
      $values['filename'] = drupal_basename($values['uri']);
    }

    // Automatically detect filemime if not set.
    if (!isset($values['filemime']) && isset($values['uri'])) {
187
      $values['filemime'] = \Drupal::service('file.mime_type.guesser')->guess($values['uri']);
188 189 190 191 192 193
    }
  }

  /**
   * {@inheritdoc}
   */
194 195
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);
196

197 198 199 200 201 202
    $this->setSize(filesize($this->getFileUri()));
  }

  /**
   * {@inheritdoc}
   */
203 204
  public static function preDelete(EntityStorageInterface $storage, array $entities) {
    parent::preDelete($storage, $entities);
205

206 207
    foreach ($entities as $entity) {
      // Delete all remaining references to this file.
208
      $file_usage = \Drupal::service('file.usage')->listUsage($entity);
209 210
      if (!empty($file_usage)) {
        foreach ($file_usage as $module => $usage) {
211
          \Drupal::service('file.usage')->delete($entity, $module);
212 213 214 215 216 217 218 219 220
        }
      }
      // Delete the actual file. Failures due to invalid files and files that
      // were already deleted are logged to watchdog but ignored, the
      // corresponding file entity will be deleted.
      file_unmanaged_delete($entity->getFileUri());
    }
  }

221 222 223
  /**
   * {@inheritdoc}
   */
224
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
225
    $fields['fid'] = BaseFieldDefinition::create('integer')
226 227
      ->setLabel(t('File ID'))
      ->setDescription(t('The file ID.'))
228 229
      ->setReadOnly(TRUE)
      ->setSetting('unsigned', TRUE);
230

231
    $fields['uuid'] = BaseFieldDefinition::create('uuid')
232 233 234 235
      ->setLabel(t('UUID'))
      ->setDescription(t('The file UUID.'))
      ->setReadOnly(TRUE);

236
    $fields['langcode'] = BaseFieldDefinition::create('language')
237 238 239
      ->setLabel(t('Language code'))
      ->setDescription(t('The file language code.'));

240
    $fields['uid'] = BaseFieldDefinition::create('entity_reference')
241 242
      ->setLabel(t('User ID'))
      ->setDescription(t('The user ID of the file.'))
243
      ->setSetting('target_type', 'user');
244

245
    $fields['filename'] = BaseFieldDefinition::create('string')
246 247 248
      ->setLabel(t('Filename'))
      ->setDescription(t('Name of the file with no path components.'));

249
    $fields['uri'] = BaseFieldDefinition::create('uri')
250
      ->setLabel(t('URI'))
251
      ->setDescription(t('The URI to access the file (either local or remote).'))
252 253
      ->setSetting('max_length', 255)
      ->setSetting('case_sensitive', TRUE);
254

255
    $fields['filemime'] = BaseFieldDefinition::create('string')
256
      ->setLabel(t('File MIME type'))
257
      ->setSetting('is_ascii', TRUE)
258 259
      ->setDescription(t("The file's MIME type."));

260
    $fields['filesize'] = BaseFieldDefinition::create('integer')
261
      ->setLabel(t('File size'))
262
      ->setDescription(t('The size of the file in bytes.'))
263 264
      ->setSetting('unsigned', TRUE)
      ->setSetting('size', 'big');
265

266
    $fields['status'] = BaseFieldDefinition::create('boolean')
267
      ->setLabel(t('Status'))
268 269
      ->setDescription(t('The status of the file, temporary (FALSE) and permanent (TRUE).'))
      ->setDefaultValue(FALSE);
270

271
    $fields['created'] = BaseFieldDefinition::create('created')
272
      ->setLabel(t('Created'))
273 274
      ->setDescription(t('The timestamp that the file was created.'));

275
    $fields['changed'] = BaseFieldDefinition::create('changed')
276 277
      ->setLabel(t('Changed'))
      ->setDescription(t('The timestamp that the file was last changed.'));
278 279

    return $fields;
280 281
  }

282
}