RefreshBatch.php in Media Thumbnails 8
File
src/Batch/RefreshBatch.php
View source
<?php
namespace Drupal\media_thumbnails\Batch;
use Drupal\Core\Entity\EntityStorageException;
use Drupal;
use Exception;
class RefreshBatch {
public static function createBatch() : array {
$ids = \Drupal::entityQuery('media')
->execute();
return [
'operations' => [
[
'\\Drupal\\media_thumbnails\\Batch\\RefreshBatch::process',
[
array_values($ids),
],
],
],
'finished' => '\\Drupal\\media_thumbnails\\Batch\\RefreshBatch::finished',
'title' => t('Refreshing media entity thumbnails'),
'init_message' => t('Thumbnail refresh batch is starting.'),
'progress_message' => t('Please wait...'),
'error_message' => t('Thumbnail refresh batch has encountered an error.'),
];
}
public static function count() : int {
$entity_type_manager = Drupal::service('entity_type.manager');
try {
$storage = $entity_type_manager
->getStorage('media');
} catch (Exception $e) {
return 0;
}
$query = $storage
->getAggregateQuery();
$query
->count();
return (int) $query
->execute();
}
public static function process($ids, &$context) {
if (!isset($context['results']['processed'])) {
$context['results']['processed'] = 0;
$context['results']['updated'] = 0;
$context['sandbox']['count'] = self::count();
}
$entity_type_manager = Drupal::service('entity_type.manager');
$storage = $entity_type_manager
->getStorage('media');
$media = $storage
->load($ids[$context['results']['processed']]);
if ($media) {
try {
$media
->save();
} catch (EntityStorageException $e) {
}
}
$context['results']['processed']++;
$context['finished'] = $context['sandbox']['count'] ? $context['results']['processed'] / $context['sandbox']['count'] : 1;
}
public static function finished($success, $results, $operations) {
$variables = [
'@processed' => $results['processed'],
];
if ($success) {
Drupal::messenger()
->addMessage(t('Processed @processed media entities.', $variables));
}
else {
Drupal::messenger()
->addWarning(t('An error occurred after processing @processed media entities.', $variables));
}
}
}