diff -u b/core/modules/media/src/MediaHandlerBase.php b/core/modules/media/src/MediaHandlerBase.php --- b/core/modules/media/src/MediaHandlerBase.php +++ b/core/modules/media/src/MediaHandlerBase.php @@ -337,11 +337,17 @@ * {@inheritdoc} */ public function getSourceValue(MediaInterface $media) { + $source_field = $this->configuration['source_field']; + + if (empty($source_field)) { + throw new \RuntimeException('Source field for media handler is not defined.'); + } + // Source value is stored as the main property of the source field in 99% of // the cases so return that. 1% can override this function and tweak the // logic. /** @var \Drupal\Core\Field\FieldItemInterface $field_item */ - $field_item = $media->get($this->configuration['source_field'])->first(); + $field_item = $media->get($source_field)->first(); return $field_item->get($field_item->mainPropertyName()); } diff -u b/core/modules/media/src/Plugin/QueueWorker/ThumbnailDownloader.php b/core/modules/media/src/Plugin/QueueWorker/ThumbnailDownloader.php --- b/core/modules/media/src/Plugin/QueueWorker/ThumbnailDownloader.php +++ b/core/modules/media/src/Plugin/QueueWorker/ThumbnailDownloader.php @@ -2,8 +2,11 @@ namespace Drupal\media\Plugin\QueueWorker; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\media\Entity\Media; use Drupal\Core\Queue\QueueWorkerBase; +use Drupal\media\MediaThumbnailHandlerInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Download images. @@ -14,7 +17,44 @@ * cron = {"time" = 60} * ) */ -class ThumbnailDownloader extends QueueWorkerBase { +class ThumbnailDownloader extends QueueWorkerBase implements ContainerFactoryPluginInterface { + + /** + * The thumbnail handler service. + * + * @var \Drupal\media\MediaThumbnailHandlerInterface + */ + protected $thumbnailHandler; + + /** + * Constructs a new SelectionBase object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\media\MediaThumbnailHandlerInterface $thumbnail_handler + * The thumbnail handler service. + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, MediaThumbnailHandlerInterface $thumbnail_handler) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + + $this->thumbnailHandler = $thumbnail_handler; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('media.thumbnail_handler') + ); + } /** * {@inheritdoc} @@ -22,7 +62,7 @@ public function processItem($data) { /** @var \Drupal\media\MediaInterface $entity */ if ($entity = Media::load($data['id'])) { - $entity->automaticallySetThumbnail(); + $this->thumbnailHandler->setThumbnail($entity); $entity->save(); } } diff -u b/core/modules/media/src/SourceFieldInterface.php b/core/modules/media/src/SourceFieldInterface.php --- b/core/modules/media/src/SourceFieldInterface.php +++ b/core/modules/media/src/SourceFieldInterface.php @@ -28,6 +28,9 @@ * * @return \Drupal\Core\TypedData\TypedDataInterface * The source value. + * + * @throws \RuntimeException + * If the source field for the handler is not defined. */ public function getSourceValue(MediaInterface $media);