File.php 6.74 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 29
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "views_data" = "Drupal\file\FileViewsData",
30
 *   },
31 32 33 34
 *   base_table = "file_managed",
 *   entity_keys = {
 *     "id" = "fid",
 *     "label" = "filename",
35
 *     "langcode" = "langcode",
36 37 38
 *     "uuid" = "uuid"
 *   }
 * )
39
 */
40
class File extends ContentEntityBase implements FileInterface {
41 42

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

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

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

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

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

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

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

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

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

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

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

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

  /**
   * {@inheritdoc}
   */
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
  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;
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 174
  }

  /**
   * {@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;
175 176
  }

177 178 179
  /**
   * {@inheritdoc}
   */
180
  public static function preCreate(EntityStorageInterface $storage, array &$values) {
181 182 183 184 185 186 187 188 189 190 191 192 193 194
    // 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'])) {
      $values['filemime'] = file_get_mimetype($values['uri']);
    }
  }

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

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

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

207 208
    foreach ($entities as $entity) {
      // Delete all remaining references to this file.
209
      $file_usage = \Drupal::service('file.usage')->listUsage($entity);
210 211
      if (!empty($file_usage)) {
        foreach ($file_usage as $module => $usage) {
212
          \Drupal::service('file.usage')->delete($entity, $module);
213 214 215 216 217 218 219 220 221
        }
      }
      // 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());
    }
  }

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

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

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

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

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

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

256
    $fields['filemime'] = BaseFieldDefinition::create('string')
257 258 259
      ->setLabel(t('File MIME type'))
      ->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
      ->setDescription(t('The status of the file, temporary (FALSE) and permanent (TRUE).'));
269

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

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

    return $fields;
279 280
  }

281
}