diff -u b/core/modules/media/media.services.yml b/core/modules/media/media.services.yml --- b/core/modules/media/media.services.yml +++ b/core/modules/media/media.services.yml @@ -5,4 +4,0 @@ - - media.thumbnail_handler: - class: Drupal\media\MediaThumbnailHandler - arguments: ['@entity_type.manager', '@string_translation', '@config.factory'] diff -u b/core/modules/media/src/Entity/Media.php b/core/modules/media/src/Entity/Media.php --- b/core/modules/media/src/Entity/Media.php +++ b/core/modules/media/src/Entity/Media.php @@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\BaseFieldDefinition; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\media\MediaInterface; use Drupal\Core\Entity\EntityChangedTrait; use Drupal\user\UserInterface; @@ -78,6 +79,7 @@ use EntityChangedTrait; use EntityPublishedTrait; + use StringTranslationTrait; /** * {@inheritdoc} @@ -132,6 +134,65 @@ } /** + * {@inheritdoc} + */ + public function updateThumbnail($from_queue = FALSE) { + $file_storage = \Drupal::service('entity_type.manager')->getStorage('file'); + $thumbnail_uri = $this->getThumbnailUri($from_queue); + $existing = $file_storage->getQuery() + ->condition('uri', $thumbnail_uri) + ->execute(); + + if ($existing) { + $this->thumbnail->target_id = reset($existing); + } + else { + /** @var \Drupal\file\FileInterface $file */ + $file = $file_storage->create(['uri' => $thumbnail_uri]); + if ($owner = $this->getOwner()) { + $file->setOwner($owner); + } + $file->setPermanent(); + $file->save(); + $this->thumbnail->target_id = $file->id(); + } + + $this->thumbnail->alt = $this->t('Thumbnail'); + $this->thumbnail->title = $this->label(); + + return $this; + } + + /** + * Gets a file URI for a media item. + * + * If thumbnail fetching should be queued then temporary use default + * thumbnail for new files or temporary keep existing thumbnail for + * updates. + * Immediately fetch a new thumbnail from the media source otherwise. + * + * @param bool $from_queue + * Specifies whether the thumbnail is being fetched from the queue. + * + * @return \Drupal\media\MediaInterface + * The file URI for the thumbnail of the media item. + */ + protected function getThumbnailUri($from_queue) { + if ($this->bundle->entity->thumbnailDownloadsAreQueued() && $this->isNew()) { + $default_thumbnail_filename = $this->getSource()->getPluginDefinition()['default_thumbnail_filename']; + $thumbnail_uri = \Drupal::service('config.factory')->get('media.settings')->get('icon_base_uri') . '/' . $default_thumbnail_filename; + } + elseif ($this->bundle->entity->thumbnailDownloadsAreQueued() && !$from_queue) { + $thumbnail_uri = $this->get('thumbnail')->entity->getFileUri(); + } + else { + $thumbnail_uri = $this->getSource()->getMetadata($this, $this->getSource()->getPluginDefinition()['thumbnail_uri_metadata_attribute']); + } + + return $thumbnail_uri; + } + + /** * Determines if the thumbnail should be updated for a media item. * * @param bool $is_new @@ -180,7 +241,7 @@ // Set thumbnail. if ($this->shouldUpdateThumbnail()) { - \Drupal::service('media.thumbnail_handler')->updateThumbnail($this); + $this->updateThumbnail(); } } diff -u b/core/modules/media/src/MediaInterface.php b/core/modules/media/src/MediaInterface.php --- b/core/modules/media/src/MediaInterface.php +++ b/core/modules/media/src/MediaInterface.php @@ -42,2 +42,13 @@ + /** + * Update the thumbnail for the media item. + * + * @param bool $from_queue + * Specifies whether the thumbnail is being fetched from the queue. + * + * @return \Drupal\media\MediaInterface + * The updated media item. + */ + public function updateThumbnail($from_queue = FALSE); + } reverted: --- b/core/modules/media/src/MediaThumbnailHandler.php +++ /dev/null @@ -1,107 +0,0 @@ -fileStorage = $entity_type_manager->getStorage('file'); - $this->stringTranslation = $translation; - $this->configFactory = $config_factory; - } - - /** - * {@inheritdoc} - */ - public function updateThumbnail(MediaInterface $media, $from_queue = FALSE) { - $thumbnail_uri = $this->getThumbnailUri($media, $from_queue); - $existing = $this->fileStorage->getQuery() - ->condition('uri', $thumbnail_uri) - ->execute(); - - if ($existing) { - $media->thumbnail->target_id = reset($existing); - } - else { - /** @var \Drupal\file\FileInterface $file */ - $file = $this->fileStorage->create(['uri' => $thumbnail_uri]); - if ($owner = $media->getOwner()) { - $file->setOwner($owner); - } - $file->setPermanent(); - $file->save(); - $media->thumbnail->target_id = $file->id(); - } - - $media->thumbnail->alt = $this->t('Thumbnail'); - $media->thumbnail->title = $media->label(); - - return $media; - } - - /** - * Gets a file URI for a media item. - * - * If thumbnail fetching should be queued then temporary use default - * thumbnail for new files or temporary keep existing thumbnail for - * updates. - * Immediately fetch a new thumbnail from the media source otherwise. - * - * @param \Drupal\media\MediaInterface $media - * A media item. - * @param bool $from_queue - * Specifies whether the thumbnail is being fetched from the queue. - * - * @return \Drupal\media\MediaInterface - * The file URI for the thumbnail of the media item. - */ - protected function getThumbnailUri(MediaInterface $media, $from_queue) { - if ($media->bundle->entity->thumbnailDownloadsAreQueued() && $media->isNew()) { - $default_thumbnail_filename = $media->getSource()->getPluginDefinition()['default_thumbnail_filename']; - $thumbnail_uri = $this->configFactory->get('media.settings')->get('icon_base_uri') . '/' . $default_thumbnail_filename; - } - elseif ($media->bundle->entity->thumbnailDownloadsAreQueued() && !$from_queue) { - $thumbnail_uri = $media->get('thumbnail')->entity->getFileUri(); - } - else { - $thumbnail_uri = $media->getSource()->getMetadata($media, $media->getSource()->getPluginDefinition()['thumbnail_uri_metadata_attribute']); - } - - return $thumbnail_uri; - } - -} reverted: --- b/core/modules/media/src/MediaThumbnailHandlerInterface.php +++ /dev/null @@ -1,23 +0,0 @@ -thumbnailHandler = $thumbnail_handler; } /** @@ -51,8 +33,7 @@ return new static( $configuration, $plugin_id, - $plugin_definition, - $container->get('media.thumbnail_handler') + $plugin_definition ); } @@ -60,10 +41,9 @@ * {@inheritdoc} */ public function processItem($data) { - /** @var \Drupal\media\MediaInterface $entity */ - if ($entity = Media::load($data['id'])) { - $this->thumbnailHandler->updateThumbnail($entity, TRUE); - $entity->save(); + if ($media = Media::load($data['id'])) { + $media->updateThumbnail(TRUE); + $media->save(); } } reverted: --- b/core/modules/media/src/Tests/MediaCacheTagsTest.php +++ /dev/null @@ -1,80 +0,0 @@ -grantPermission('view media'); - $user_role->save(); - } - - /** - * {@inheritdoc} - */ - protected function createEntity() { - // Create a media type. - $mediaType = $this->createMediaType(); - - // Create a media item. - $media = Media::create([ - 'bundle' => $mediaType->id(), - 'name' => 'Unnamed', - ]); - $media->save(); - - return $media; - } - - /** - * {@inheritdoc} - */ - protected function getAdditionalCacheContextsForEntity(EntityInterface $media) { - return ['timezone']; - } - - /** - * {@inheritdoc} - * - * Each media item must have an author and a thumbnail. - */ - protected function getAdditionalCacheTagsForEntity(EntityInterface $media) { - return [ - 'user:' . $media->getOwnerId(), - 'config:image.style.thumbnail', - 'file:' . $media->get('thumbnail')->entity->id(), - ]; - } - -} only in patch2: unchanged: --- /dev/null +++ b/core/modules/media/tests/src/Functional/MediaCacheTagsTest.php @@ -0,0 +1,78 @@ +grantPermission('view media'); + $user_role->save(); + } + + /** + * {@inheritdoc} + */ + protected function createEntity() { + // Create a media type. + $mediaType = $this->createMediaType(); + + // Create a media item. + $media = Media::create([ + 'bundle' => $mediaType->id(), + 'name' => 'Unnamed', + ]); + $media->save(); + + return $media; + } + + /** + * {@inheritdoc} + */ + protected function getAdditionalCacheContextsForEntity(EntityInterface $media) { + return ['timezone']; + } + + /** + * {@inheritdoc} + * + * Each media item must have an author and a thumbnail. + */ + protected function getAdditionalCacheTagsForEntity(EntityInterface $media) { + return [ + 'user:' . $media->getOwnerId(), + 'config:image.style.thumbnail', + 'file:' . $media->get('thumbnail')->entity->id(), + ]; + } + +}