3 namespace Drupal\media\Entity;
5 use Drupal\Core\Entity\EditorialContentEntityBase;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Field\BaseFieldDefinition;
9 use Drupal\Core\StringTranslation\StringTranslationTrait;
10 use Drupal\media\MediaInterface;
11 use Drupal\media\MediaSourceEntityConstraintsInterface;
12 use Drupal\media\MediaSourceFieldConstraintsInterface;
13 use Drupal\user\UserInterface;
16 * Defines the media entity class.
18 * @todo Remove default/fallback entity form operation when #2006348 is done.
19 * @see https://www.drupal.org/node/2006348.
23 * label = @Translation("Media"),
24 * label_singular = @Translation("media item"),
25 * label_plural = @Translation("media items"),
26 * label_count = @PluralTranslation(
27 * singular = "@count media item",
28 * plural = "@count media items"
30 * bundle_label = @Translation("Media type"),
32 * "storage" = "Drupal\media\MediaStorage",
33 * "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
34 * "list_builder" = "Drupal\media\MediaListBuilder",
35 * "access" = "Drupal\media\MediaAccessControlHandler",
37 * "default" = "Drupal\media\MediaForm",
38 * "add" = "Drupal\media\MediaForm",
39 * "edit" = "Drupal\media\MediaForm",
40 * "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
41 * "delete-multiple-confirm" = "Drupal\Core\Entity\Form\DeleteMultipleForm",
43 * "translation" = "Drupal\content_translation\ContentTranslationHandler",
44 * "views_data" = "Drupal\media\MediaViewsData",
45 * "route_provider" = {
46 * "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
49 * base_table = "media",
50 * data_table = "media_field_data",
51 * revision_table = "media_revision",
52 * revision_data_table = "media_field_revision",
53 * translatable = TRUE,
54 * show_revision_ui = TRUE,
58 * "bundle" = "bundle",
60 * "langcode" = "langcode",
62 * "published" = "status",
64 * revision_metadata_keys = {
65 * "revision_user" = "revision_user",
66 * "revision_created" = "revision_created",
67 * "revision_log_message" = "revision_log_message",
69 * bundle_entity_type = "media_type",
70 * permission_granularity = "bundle",
71 * admin_permission = "administer media",
72 * field_ui_base_route = "entity.media_type.edit_form",
73 * common_reference_target = TRUE,
75 * "add-page" = "/media/add",
76 * "add-form" = "/media/add/{media_type}",
77 * "canonical" = "/media/{media}",
78 * "collection" = "/admin/content/media",
79 * "delete-form" = "/media/{media}/delete",
80 * "delete-multiple-form" = "/media/delete",
81 * "edit-form" = "/media/{media}/edit",
82 * "revision" = "/media/{media}/revisions/{media_revision}/view",
86 class Media extends EditorialContentEntityBase implements MediaInterface {
88 use StringTranslationTrait;
93 public function getName() {
94 $name = $this->getEntityKey('label');
97 $media_source = $this->getSource();
98 return $media_source->getMetadata($this, $media_source->getPluginDefinition()['default_name_metadata_attribute']);
107 public function label() {
108 return $this->getName();
114 public function setName($name) {
115 return $this->set('name', $name);
121 public function getCreatedTime() {
122 return $this->get('created')->value;
128 public function setCreatedTime($timestamp) {
129 return $this->set('created', $timestamp);
135 public function getOwner() {
136 return $this->get('uid')->entity;
142 public function setOwner(UserInterface $account) {
143 return $this->set('uid', $account->id());
149 public function getOwnerId() {
150 return $this->get('uid')->target_id;
156 public function setOwnerId($uid) {
157 return $this->set('uid', $uid);
163 public function getSource() {
164 return $this->bundle->entity->getSource();
168 * Update the thumbnail for the media item.
170 * @param bool $from_queue
171 * Specifies whether the thumbnail update is triggered from the queue.
173 * @return \Drupal\media\MediaInterface
174 * The updated media item.
178 * @todo There has been some disagreement about how to handle updates to
179 * thumbnails. We need to decide on what the API will be for this.
180 * https://www.drupal.org/node/2878119
182 protected function updateThumbnail($from_queue = FALSE) {
183 $this->thumbnail->target_id = $this->loadThumbnail($this->getThumbnailUri($from_queue))->id();
185 // Set the thumbnail alt.
186 $media_source = $this->getSource();
187 $plugin_definition = $media_source->getPluginDefinition();
188 if (!empty($plugin_definition['thumbnail_alt_metadata_attribute'])) {
189 $this->thumbnail->alt = $media_source->getMetadata($this, $plugin_definition['thumbnail_alt_metadata_attribute']);
192 $this->thumbnail->alt = $this->t('Thumbnail', [], ['langcode' => $this->langcode->value]);
195 // Set the thumbnail title.
196 if (!empty($plugin_definition['thumbnail_title_metadata_attribute'])) {
197 $this->thumbnail->title = $media_source->getMetadata($this, $plugin_definition['thumbnail_title_metadata_attribute']);
200 $this->thumbnail->title = $this->label();
207 * Loads the file entity for the thumbnail.
209 * If the file entity does not exist, it will be created.
211 * @param string $thumbnail_uri
212 * (optional) The URI of the thumbnail, used to load or create the file
213 * entity. If omitted, the default thumbnail URI will be used.
215 * @return \Drupal\file\FileInterface
216 * The thumbnail file entity.
218 protected function loadThumbnail($thumbnail_uri = NULL) {
220 'uri' => $thumbnail_uri ?: $this->getDefaultThumbnailUri(),
223 $file_storage = $this->entityTypeManager()->getStorage('file');
225 $existing = $file_storage->loadByProperties($values);
227 $file = reset($existing);
230 /** @var \Drupal\file\FileInterface $file */
231 $file = $file_storage->create($values);
232 if ($owner = $this->getOwner()) {
233 $file->setOwner($owner);
235 $file->setPermanent();
242 * Returns the URI of the default thumbnail.
245 * The default thumbnail URI.
247 protected function getDefaultThumbnailUri() {
248 $default_thumbnail_filename = $this->getSource()->getPluginDefinition()['default_thumbnail_filename'];
249 return \Drupal::config('media.settings')->get('icon_base_uri') . '/' . $default_thumbnail_filename;
253 * Updates the queued thumbnail for the media item.
255 * @return \Drupal\media\MediaInterface
256 * The updated media item.
260 * @todo If the need arises in contrib, consider making this a public API,
261 * by adding an interface that extends MediaInterface.
263 public function updateQueuedThumbnail() {
264 $this->updateThumbnail(TRUE);
269 * Gets the URI for the thumbnail of a media item.
271 * If thumbnail fetching is queued, new media items will use the default
272 * thumbnail, and existing media items will use the current thumbnail, until
273 * the queue is processed and the updated thumbnail has been fetched.
274 * Otherwise, the new thumbnail will be fetched immediately.
276 * @param bool $from_queue
277 * Specifies whether the thumbnail is being fetched from the queue.
280 * The file URI for the thumbnail of the media item.
284 protected function getThumbnailUri($from_queue) {
285 $thumbnails_queued = $this->bundle->entity->thumbnailDownloadsAreQueued();
286 if ($thumbnails_queued && $this->isNew()) {
287 return $this->getDefaultThumbnailUri();
289 elseif ($thumbnails_queued && !$from_queue) {
290 return $this->get('thumbnail')->entity->getFileUri();
293 $source = $this->getSource();
294 return $source->getMetadata($this, $source->getPluginDefinition()['thumbnail_uri_metadata_attribute']);
298 * Determines if the source field value has changed.
301 * TRUE if the source field value changed, FALSE otherwise.
305 protected function hasSourceFieldChanged() {
306 $source_field_name = $this->getSource()->getConfiguration()['source_field'];
307 $current_items = $this->get($source_field_name);
308 return isset($this->original) && !$current_items->equals($this->original->get($source_field_name));
312 * Determines if the thumbnail should be updated for a media item.
314 * @param bool $is_new
315 * Specifies whether the media item is new.
318 * TRUE if the thumbnail should be updated, FALSE otherwise.
320 protected function shouldUpdateThumbnail($is_new = FALSE) {
321 // Update thumbnail if we don't have a thumbnail yet or when the source
322 // field value changes.
323 return !$this->get('thumbnail')->entity || $is_new || $this->hasSourceFieldChanged();
329 public function preSave(EntityStorageInterface $storage) {
330 parent::preSave($storage);
332 // If no thumbnail has been explicitly set, use the default thumbnail.
333 if ($this->get('thumbnail')->isEmpty()) {
334 $this->thumbnail->target_id = $this->loadThumbnail()->id();
341 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
342 parent::postSave($storage, $update);
344 foreach ($this->translations as $langcode => $data) {
345 if ($this->hasTranslation($langcode)) {
346 $translation = $this->getTranslation($langcode);
347 if ($translation->bundle->entity->thumbnailDownloadsAreQueued() && $translation->shouldUpdateThumbnail($is_new)) {
348 \Drupal::queue('media_entity_thumbnail')->createItem(['id' => $translation->id()]);
357 public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
358 parent::preSaveRevision($storage, $record);
360 $is_new_revision = $this->isNewRevision();
361 if (!$is_new_revision && isset($this->original) && empty($record->revision_log_message)) {
362 // If we are updating an existing media item without adding a
363 // new revision, we need to make sure $entity->revision_log_message is
364 // reset whenever it is empty.
365 // Therefore, this code allows us to avoid clobbering an existing log
366 // entry with an empty one.
367 $record->revision_log_message = $this->original->revision_log_message->value;
370 if ($is_new_revision) {
371 $record->revision_created = self::getRequestTime();
376 * Sets the media entity's field values from the source's metadata.
378 * Fetching the metadata could be slow (e.g., if requesting it from a remote
379 * API), so this is called by \Drupal\media\MediaStorage::save() prior to it
380 * beginning the database transaction, whereas static::preSave() executes
381 * after the transaction has already started.
384 * Expose this as an API in
385 * https://www.drupal.org/project/drupal/issues/2992426.
387 public function prepareSave() {
388 // @todo If the source plugin talks to a remote API (e.g. oEmbed), this code
389 // might be performing a fair number of HTTP requests. This is dangerously
390 // brittle and should probably be handled by a queue, to avoid doing HTTP
391 // operations during entity save. See
392 // https://www.drupal.org/project/drupal/issues/2976875 for more.
394 // In order for metadata to be mapped correctly, $this->original must be
395 // set. However, that is only set once parent::save() is called, so work
396 // around that by setting it here.
397 if (!isset($this->original) && $id = $this->id()) {
398 $this->original = $this->entityTypeManager()
399 ->getStorage('media')
400 ->loadUnchanged($id);
403 $media_source = $this->getSource();
404 foreach ($this->translations as $langcode => $data) {
405 if ($this->hasTranslation($langcode)) {
406 $translation = $this->getTranslation($langcode);
407 // Try to set fields provided by the media source and mapped in
408 // media type config.
409 foreach ($translation->bundle->entity->getFieldMap() as $metadata_attribute_name => $entity_field_name) {
410 // Only save value in entity field if empty. Do not overwrite existing
412 if ($translation->hasField($entity_field_name) && ($translation->get($entity_field_name)->isEmpty() || $translation->hasSourceFieldChanged())) {
413 $translation->set($entity_field_name, $media_source->getMetadata($translation, $metadata_attribute_name));
417 // Try to set a default name for this media item if no name is provided.
418 if ($translation->get('name')->isEmpty()) {
419 $translation->setName($translation->getName());
423 if ($translation->shouldUpdateThumbnail($this->isNew())) {
424 $translation->updateThumbnail();
433 public function validate() {
434 $media_source = $this->getSource();
436 if ($media_source instanceof MediaSourceEntityConstraintsInterface) {
437 $entity_constraints = $media_source->getEntityConstraints();
438 $this->getTypedData()->getDataDefinition()->setConstraints($entity_constraints);
441 if ($media_source instanceof MediaSourceFieldConstraintsInterface) {
442 $source_field_name = $media_source->getConfiguration()['source_field'];
443 $source_field_constraints = $media_source->getSourceFieldConstraints();
444 $this->get($source_field_name)->getDataDefinition()->setConstraints($source_field_constraints);
447 return parent::validate();
453 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
454 $fields = parent::baseFieldDefinitions($entity_type);
456 $fields['name'] = BaseFieldDefinition::create('string')
457 ->setLabel(t('Name'))
459 ->setTranslatable(TRUE)
460 ->setRevisionable(TRUE)
461 ->setDefaultValue('')
462 ->setSetting('max_length', 255)
463 ->setDisplayOptions('form', [
464 'type' => 'string_textfield',
467 ->setDisplayConfigurable('form', TRUE)
468 ->setDisplayConfigurable('view', TRUE);
470 $fields['thumbnail'] = BaseFieldDefinition::create('image')
471 ->setLabel(t('Thumbnail'))
472 ->setDescription(t('The thumbnail of the media item.'))
473 ->setRevisionable(TRUE)
474 ->setTranslatable(TRUE)
475 ->setDisplayOptions('view', [
480 'image_style' => 'thumbnail',
483 ->setDisplayConfigurable('view', TRUE)
486 $fields['uid'] = BaseFieldDefinition::create('entity_reference')
487 ->setLabel(t('Authored by'))
488 ->setDescription(t('The user ID of the author.'))
489 ->setRevisionable(TRUE)
490 ->setDefaultValueCallback(static::class . '::getCurrentUserId')
491 ->setSetting('target_type', 'user')
492 ->setTranslatable(TRUE)
493 ->setDisplayOptions('form', [
494 'type' => 'entity_reference_autocomplete',
497 'match_operator' => 'CONTAINS',
499 'autocomplete_type' => 'tags',
503 ->setDisplayConfigurable('form', TRUE)
504 ->setDisplayOptions('view', [
509 ->setDisplayConfigurable('view', TRUE);
512 ->setDisplayOptions('form', [
513 'type' => 'boolean_checkbox',
515 'display_label' => TRUE,
519 ->setDisplayConfigurable('form', TRUE);
521 $fields['created'] = BaseFieldDefinition::create('created')
522 ->setLabel(t('Authored on'))
523 ->setDescription(t('The time the media item was created.'))
524 ->setTranslatable(TRUE)
525 ->setRevisionable(TRUE)
526 ->setDefaultValueCallback(static::class . '::getRequestTime')
527 ->setDisplayOptions('form', [
528 'type' => 'datetime_timestamp',
531 ->setDisplayConfigurable('form', TRUE)
532 ->setDisplayOptions('view', [
534 'type' => 'timestamp',
537 ->setDisplayConfigurable('view', TRUE);
539 $fields['changed'] = BaseFieldDefinition::create('changed')
540 ->setLabel(t('Changed'))
541 ->setDescription(t('The time the media item was last edited.'))
542 ->setTranslatable(TRUE)
543 ->setRevisionable(TRUE);
549 * Default value callback for 'uid' base field definition.
551 * @see ::baseFieldDefinitions()
554 * An array of default values.
556 public static function getCurrentUserId() {
557 return [\Drupal::currentUser()->id()];
563 public static function getRequestTime() {
564 return \Drupal::time()->getRequestTime();