function taxonomy_machine_name_update_all_terms in Taxonomy Machine Name 8
Update machine names for existing terms, usable both in batch and update.
Parameters
array $context: The $context parameter in updates, called $context in Batch API.
1 call to taxonomy_machine_name_update_all_terms()
- taxonomy_machine_name_update_8001 in ./
taxonomy_machine_name.install - Update machine names for existing terms.
1 string reference to 'taxonomy_machine_name_update_all_terms'
- taxonomy_machine_name_install in ./
taxonomy_machine_name.install - Implements hook_install().
File
- ./
taxonomy_machine_name.install, line 49 - Install, update functions for the Taxonomy Machine Name module.
Code
function taxonomy_machine_name_update_all_terms(&$context) {
$sandbox =& $context['sandbox'];
if (empty($sandbox['tids'])) {
// Size of the batch to process.
$batch_size = 10;
$tids = \Drupal::entityQuery('taxonomy_term')
->notExists('machine_name')
->execute();
$sandbox['total'] = count($tids);
$sandbox['tids'] = array_chunk($tids, $batch_size);
$sandbox['succeeded'] = $sandbox['errored'] = $sandbox['processed_chunks'] = 0;
}
// Nothing to do.
if (!$sandbox['total']) {
$context['message'] = t('No terms updated');
return;
}
// Process all terms in this chunk.
$current_chunk = $sandbox['tids'][$sandbox['processed_chunks']];
$terms = Term::loadMultiple($current_chunk);
foreach ($terms as $term) {
$success = taxonomy_machine_name_update_term($term);
$success ? $sandbox['succeeded']++ : $sandbox['errored']++;
}
// Increment the number of processed chunks to determine when we've finished.
$sandbox['processed_chunks']++;
// When we have processed all of the chunks $context['finished'] will be 1.
// Then the batch / update runner will consider the job finished.
$context['finished'] = $sandbox['processed_chunks'] / count($sandbox['tids']);
$context['message'] = t('@succeeded terms were updated correctly. @errored terms failed.', [
'@succeeded' => $sandbox['succeeded'],
'@errored' => $sandbox['errored'],
]);
}