public static function PriceListItemImportForm::batchDeleteExisting in Commerce Pricelist 8.2
Batch operation to delete existing items from the price list.
Parameters
int $price_list_id: The price list ID.
array $context: The batch context.
File
- src/
Form/ PriceListItemImportForm.php, line 263
Class
Namespace
Drupal\commerce_pricelist\FormCode
public static function batchDeleteExisting($price_list_id, array &$context) {
$entity_type_manager = \Drupal::entityTypeManager();
$price_list_storage = $entity_type_manager
->getStorage('commerce_pricelist');
$price_list_item_storage = $entity_type_manager
->getStorage('commerce_pricelist_item');
/** @var \Drupal\commerce_pricelist\Entity\PriceListInterface $price_list */
$price_list = $price_list_storage
->load($price_list_id);
$price_list_item_ids = $price_list
->getItemIds();
if (empty($context['sandbox'])) {
$context['sandbox']['delete_total'] = count($price_list_item_ids);
$context['sandbox']['delete_count'] = 0;
$context['results']['delete_count'] = 0;
}
$total_items = $context['sandbox']['delete_total'];
$deleted =& $context['sandbox']['delete_count'];
$remaining = $total_items - $deleted;
$limit = (int) ($remaining < self::BATCH_SIZE) ? $remaining : self::BATCH_SIZE;
if ($total_items == 0 || empty($price_list_item_ids)) {
$context['finished'] = 1;
}
else {
$price_list_item_ids = array_slice($price_list_item_ids, 0, $limit);
$price_list_items = $price_list_item_storage
->loadMultiple($price_list_item_ids);
$price_list_item_storage
->delete($price_list_items);
$deleted = $deleted + $limit;
$context['message'] = t('Deleting price @deleted of @total_items', [
'@deleted' => $deleted,
'@total_items' => $total_items,
]);
$context['finished'] = $deleted / $total_items;
}
// Update the results for finishBatch().
$context['results']['delete_count'] = $deleted;
}