View source
<?php
namespace Drupal\media_entity\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\media_entity\MediaInterface;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\user\UserInterface;
class Media extends ContentEntityBase implements MediaInterface {
use EntityChangedTrait;
const PUBLISHED = 1;
const NOT_PUBLISHED = 0;
protected $queued_thumbnail_download = FALSE;
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
public function getChangedTime() {
return $this
->get('changed')->value;
}
public function isPublished() {
return (bool) $this
->get('status')->value;
}
public function setPublished($published) {
$this
->set('status', $published ? Media::PUBLISHED : Media::NOT_PUBLISHED);
return $this;
}
public function getPublisher() {
return $this
->get('uid')->entity;
}
public function setQueuedThumbnailDownload() {
$this->queued_thumbnail_download = TRUE;
}
public function getPublisherId() {
return $this
->get('uid')->target_id;
}
public function setPublisherId($uid) {
$this
->set('uid', $uid);
return $this;
}
public function getType() {
return $this->bundle->entity
->getType();
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
if (!$this
->get('revision_uid')->entity) {
$this
->set('revision_uid', $this
->getPublisherId());
}
if (!$this
->get('thumbnail')->entity || !empty($this->queued_thumbnail_download)) {
$this
->automaticallySetThumbnail();
}
foreach ($this->bundle->entity->field_map as $source_field => $destination_field) {
if ($this
->hasField($destination_field) && $this->{$destination_field}
->isEmpty() && ($value = $this
->getType()
->getField($this, $source_field))) {
$this
->set($destination_field, $value);
}
}
if (empty($this
->label())) {
$this
->set('name', $this
->getType()
->getDefaultName($this));
}
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
if (!$update && $this->bundle->entity
->getQueueThumbnailDownloads()) {
$queue = \Drupal::queue('media_entity_thumbnail');
$queue
->createItem([
'id' => $this
->id(),
]);
}
}
public function automaticallySetThumbnail() {
if ($this->bundle->entity
->getQueueThumbnailDownloads() && $this
->isNew()) {
$thumbnail_uri = $this
->getType()
->getDefaultThumbnail();
}
else {
$thumbnail_uri = $this
->getType()
->thumbnail($this);
}
$existing = \Drupal::entityQuery('file')
->condition('uri', $thumbnail_uri)
->execute();
if ($existing) {
$this->thumbnail->target_id = reset($existing);
}
else {
$file = $this
->entityTypeManager()
->getStorage('file')
->create([
'uri' => $thumbnail_uri,
]);
if ($publisher = $this
->getPublisher()) {
$file
->setOwner($publisher);
}
$file
->setPermanent();
$file
->save();
$this->thumbnail->target_id = $file
->id();
}
$this->thumbnail->alt = t('Thumbnail');
$this->thumbnail->title = $this
->label();
}
public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
parent::preSaveRevision($storage, $record);
if (!$this
->isNewRevision() && isset($this->original) && (!isset($record->revision_log) || $record->revision_log === '')) {
$record->revision_log = $this->original->revision_log->value;
}
}
public function validate() {
$this
->getType()
->attachConstraints($this);
return parent::validate();
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['mid'] = BaseFieldDefinition::create('integer')
->setLabel(t('Media ID'))
->setDescription(t('The media ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The media UUID.'))
->setReadOnly(TRUE);
$fields['vid'] = BaseFieldDefinition::create('integer')
->setLabel(t('Revision ID'))
->setDescription(t('The media revision ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['bundle'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Bundle'))
->setDescription(t('The media bundle.'))
->setSetting('target_type', 'media_bundle')
->setReadOnly(TRUE);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The media language code.'))
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setDisplayOptions('view', [
'region' => 'hidden',
])
->setDisplayOptions('form', [
'type' => 'language_select',
'weight' => 2,
]);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Media name'))
->setDescription(t('The name of this media.'))
->setRequired(TRUE)
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setDefaultValue('')
->setSetting('max_length', 255)
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
])
->setDisplayConfigurable('view', TRUE);
$fields['thumbnail'] = BaseFieldDefinition::create('image')
->setLabel(t('Thumbnail'))
->setDescription(t('The thumbnail of the media.'))
->setRevisionable(TRUE)
->setDisplayOptions('view', [
'type' => 'image',
'weight' => 1,
'label' => 'hidden',
'settings' => [
'image_style' => 'thumbnail',
],
])
->setDisplayConfigurable('view', TRUE)
->setReadOnly(TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Publisher ID'))
->setDescription(t('The user ID of the media publisher.'))
->setRevisionable(TRUE)
->setDefaultValueCallback('Drupal\\media_entity\\Entity\\Media::getCurrentUserId')
->setSetting('target_type', 'user')
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Publishing status'))
->setDescription(t('A boolean indicating whether the media is published.'))
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setDefaultValue(TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the media was created.'))
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'timestamp',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('form', [
'type' => 'datetime_timestamp',
'weight' => 10,
])
->setDisplayConfigurable('form', TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the media was last edited.'))
->setTranslatable(TRUE)
->setRevisionable(TRUE);
$fields['revision_timestamp'] = BaseFieldDefinition::create('created')
->setLabel(t('Revision timestamp'))
->setDescription(t('The time that the current revision was created.'))
->setCustomStorage(FALSE)
->setRevisionable(TRUE);
$fields['revision_uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Revision publisher ID'))
->setDescription(t('The user ID of the publisher of the current revision.'))
->setSetting('target_type', 'user')
->setCustomStorage(FALSE)
->setRevisionable(TRUE);
$fields['revision_log'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Revision Log'))
->setDescription(t('The log entry explaining the changes in this revision.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE);
return $fields;
}
public static function getCurrentUserId() {
return [
\Drupal::currentUser()
->id(),
];
}
public function getRevisionCreationTime() {
return $this->revision_timestamp->value;
}
public function setRevisionCreationTime($timestamp) {
$this->revision_timestamp->value = $timestamp;
return $this;
}
public function getRevisionUser() {
return $this->revision_uid->entity;
}
public function setRevisionUser(UserInterface $account) {
$this->revision_uid->entity = $account;
return $this;
}
public function getRevisionUserId() {
return $this->revision_uid->target_id;
}
public function setRevisionUserId($user_id) {
$this->revision_uid->target_id = $user_id;
return $this;
}
public function getRevisionLogMessage() {
return $this->revision_log->value;
}
public function setRevisionLogMessage($revision_log_message) {
$this->revision_log->value = $revision_log_message;
return $this;
}
}