View source
<?php
namespace Drupal\termcase;
class UpdateTerms {
public static function updateAllTerms(array $terms, $case, array &$context) {
if (!isset($context['sandbox']['progress'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['current_term'] = 0;
$context['sandbox']['max'] = count($terms);
}
$limit = 5;
$terms = array_slice($terms, $context['sandbox']['progress'], $limit);
foreach ($terms as $term) {
$converted_term = _termcase_convert_string_to_case($term->name, $case);
termcase_update_term($converted_term, $term->tid, $term->vid);
$context['results'][] = $term->name;
$context['sandbox']['progress']++;
$context['sandbox']['current_term'] = $term->tid;
$context['message'] = t('Now processing %term', [
'%term' => $term->name,
]);
}
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
public static function updateAllTermsFinishedCallback($success, $results, $operations) {
if ($success) {
$message = \Drupal::translation()
->formatPlural(count($results), 'One post processed.', '@count posts processed.');
}
else {
$message = t('Finished with an error.');
}
\Drupal::messenger()
->addMessage($message);
}
}