Newer
Older
Dries Buytaert
committed
<?php
namespace Drupal\file\Entity;
Dries Buytaert
committed
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
catch
committed
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
Alex Pott
committed
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\file\FileInterface;
use Drupal\user\UserInterface;
Dries Buytaert
committed
/**
* Defines the file entity class.
Angie Byron
committed
*
* @ingroup file
*
Alex Pott
committed
* @ContentEntityType(
Angie Byron
committed
* id = "file",
* label = @Translation("File"),
Alex Pott
committed
* handlers = {
catch
committed
* "storage" = "Drupal\file\FileStorage",
* "storage_schema" = "Drupal\file\FileStorageSchema",
* "access" = "Drupal\file\FileAccessControlHandler",
* "views_data" = "Drupal\file\FileViewsData",
Angie Byron
committed
* base_table = "file_managed",
* entity_keys = {
* "id" = "fid",
* "label" = "filename",
* "langcode" = "langcode",
Angie Byron
committed
* "uuid" = "uuid"
* }
* )
Dries Buytaert
committed
*/
class File extends ContentEntityBase implements FileInterface {
Dries Buytaert
committed
use EntityChangedTrait;
Dries Buytaert
committed
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function getFilename() {
return $this->get('filename')->value;
}
Dries Buytaert
committed
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function setFilename($filename) {
$this->get('filename')->value = $filename;
}
Dries Buytaert
committed
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function getFileUri() {
return $this->get('uri')->value;
}
Dries Buytaert
committed
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function setFileUri($uri) {
$this->get('uri')->value = $uri;
}
Dries Buytaert
committed
/**
* {@inheritdoc}
*
* @see file_url_transform_relative()
*/
public function url($rel = 'canonical', $options = []) {
return file_create_url($this->getFileUri());
}
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function getMimeType() {
return $this->get('filemime')->value;
}
Dries Buytaert
committed
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function setMimeType($mime) {
$this->get('filemime')->value = $mime;
}
Dries Buytaert
committed
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function getSize() {
return $this->get('filesize')->value;
}
Dries Buytaert
committed
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function setSize($size) {
$this->get('filesize')->value = $size;
}
Dries Buytaert
committed
/**
* {@inheritdoc}
*/
public function getCreatedTime() {
return $this->get('created')->value;
}
/**
* {@inheritdoc}
*/
public function getOwner() {
Alex Pott
committed
return $this->get('uid')->entity;
}
/**
* {@inheritdoc}
*/
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;
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
}
/**
* {@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;
Dries Buytaert
committed
}
/**
* {@inheritdoc}
*/
catch
committed
public static function preCreate(EntityStorageInterface $storage, array &$values) {
// 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'] = \Drupal::service('file.mime_type.guesser')->guess($values['uri']);
}
}
/**
* {@inheritdoc}
*/
catch
committed
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
Angie Byron
committed
Alex Pott
committed
// The file itself might not exist or be available right now.
$uri = $this->getFileUri();
$size = @filesize($uri);
// Set size unless there was an error.
if ($size !== FALSE) {
Alex Pott
committed
$this->setSize($size);
}
}
/**
* {@inheritdoc}
*/
catch
committed
public static function preDelete(EntityStorageInterface $storage, array $entities) {
parent::preDelete($storage, $entities);
Angie Byron
committed
foreach ($entities as $entity) {
// Delete all remaining references to this file.
$file_usage = \Drupal::service('file.usage')->listUsage($entity);
if (!empty($file_usage)) {
foreach ($file_usage as $module => $usage) {
\Drupal::service('file.usage')->delete($entity, $module);
}
}
// 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());
}
}
Alex Pott
committed
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
/** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
$fields = parent::baseFieldDefinitions($entity_type);
$fields['fid']->setLabel(t('File ID'))
->setDescription(t('The file ID.'));
$fields['uuid']->setDescription(t('The file UUID.'));
$fields['langcode']->setLabel(t('Language code'))
->setDescription(t('The file language code.'));
Alex Pott
committed
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('User ID'))
->setDescription(t('The user ID of the file.'))
Alex Pott
committed
->setSetting('target_type', 'user');
Alex Pott
committed
$fields['filename'] = BaseFieldDefinition::create('string')
->setLabel(t('Filename'))
->setDescription(t('Name of the file with no path components.'));
Alex Bronstein
committed
$fields['uri'] = BaseFieldDefinition::create('file_uri')
->setLabel(t('URI'))
Alex Pott
committed
->setDescription(t('The URI to access the file (either local or remote).'))
->setSetting('max_length', 255)
->setSetting('case_sensitive', TRUE)
->addConstraint('FileUriUnique');
Alex Pott
committed
$fields['filemime'] = BaseFieldDefinition::create('string')
->setLabel(t('File MIME type'))
->setSetting('is_ascii', TRUE)
->setDescription(t("The file's MIME type."));
Alex Pott
committed
$fields['filesize'] = BaseFieldDefinition::create('integer')
->setLabel(t('File size'))
catch
committed
->setDescription(t('The size of the file in bytes.'))
->setSetting('unsigned', TRUE)
->setSetting('size', 'big');
Alex Pott
committed
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Status'))
->setDescription(t('The status of the file, temporary (FALSE) and permanent (TRUE).'))
->setDefaultValue(FALSE);
Alex Pott
committed
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The timestamp that the file was created.'));
Alex Pott
committed
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The timestamp that the file was last changed.'));
return $fields;
Alex Pott
committed
}