public function MediaThumbnailManager::deleteThumbnail in Media Thumbnails 8
Delete a media thumbnail.
Parameters
\Drupal\media\MediaInterface $media: The media object.
1 call to MediaThumbnailManager::deleteThumbnail()
- MediaThumbnailManager::updateThumbnail in src/
Plugin/ MediaThumbnailManager.php - Update a media thumbnail.
File
- src/
Plugin/ MediaThumbnailManager.php, line 119
Class
- MediaThumbnailManager
- Provides the Media thumbnail plugin manager.
Namespace
Drupal\media_thumbnails\PluginCode
public function deleteThumbnail(MediaInterface $media) {
// Get the thumbnail file object.
/** @var \Drupal\file\FileInterface $thumbnail */
$thumbnail = $this
->getThumbnail($media);
// Remove the thumbnail from the media entity.
$media
->set('thumbnail', NULL);
// Don't delete thumbnails used in other places.
/** @var \Drupal\file\FileUsage\FileUsageInterface $fileUsage */
$fileUsage = $this->container
->get('file.usage');
$usage = $fileUsage
->listUsage($thumbnail);
$count = 0;
array_walk_recursive($usage, static function () use (&$count) {
$count++;
});
if ($count > 1) {
return;
}
// Don't delete generic default thumbnails.
if ($thumbnail
->getCreatedTime() < $media
->getCreatedTime()) {
return;
}
// Delete the thumbnail file.
try {
$thumbnail
->delete();
} catch (EntityStorageException $e) {
}
}