public function Media::automaticallySetThumbnail in Media entity 8
Automatically determines the most appropriate thumbnail and sets "thumbnail" field.
Overrides MediaInterface::automaticallySetThumbnail
1 call to Media::automaticallySetThumbnail()
- Media::preSave in src/
Entity/ Media.php - Acts on an entity before the presave hook is invoked.
File
- src/
Entity/ Media.php, line 209
Class
- Media
- Defines the media entity class.
Namespace
Drupal\media_entity\EntityCode
public function automaticallySetThumbnail() {
/** @var \Drupal\media_entity\MediaBundleInterface $bundle */
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 {
/** @var \Drupal\file\FileInterface $file */
$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();
}
// TODO - We should probably use something smarter (tokens, ...).
$this->thumbnail->alt = t('Thumbnail');
$this->thumbnail->title = $this
->label();
}