Site icon Blog ARC Optimizer

Drupal 9.4 Queue Worker avec traitement par lots

Drupal 9.4 Queue Worker avec traitement par lots


/**
* {@inheritdoc}
*/
public function processItem($data) {
// Implement your custom processing logic here.
// This method will be called for each item in the queue.

// Example: Updating an entity field value.
$entity = $this->entityTypeManager->getStorage('node')->load($data['nid']);
$entity->set('field_status', 'processed');
$entity->save();

// Batch processing.
$batch = [
'operations' => [
[[get_class($this), 'processItem'], [$data]],
],
'finished' => [get_class($this), 'finishedCallback'],
'title' => t('Processing queue items...'),
'init_message' => t('Starting processing...'),
'progress_message' => t('Processed @current out of @total.'),
];

batch_set($batch);
}

/**
* Batch processing finished callback.
*/
public static function finishedCallback($success, $results, $operations) {
if ($success) {
// Batch processing completed successfully.
// Perform any additional actions here.
}
else {
// Batch processing failed.
// Log or handle errors here.
}
}




Source link
Quitter la version mobile