function delete_all_content_batch_delete in Delete all 8
Same name and namespace in other branches
- 2.x includes/delete_all.content.batch.inc \delete_all_content_batch_delete()
Function to delete content using Batch API.
Parameters
array $nodes: Array of nodes to delete.
array &$context: Sandbox context array.
1 string reference to 'delete_all_content_batch_delete'
- ContentDeleteController::getContentDeleteBatch in src/
Controller/ ContentDeleteController.php
File
- includes/
delete_all.content.batch.inc, line 13
Code
function delete_all_content_batch_delete($nodes = FALSE, &$context) {
$db = \Drupal::database();
// Initialize sandbox.
if ($nodes === FALSE) {
if (!isset($context['sandbox']['progress'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['current_nid'] = 0;
$context['sandbox']['max'] = $db
->select('node', 'n')
->fields('n')
->countQuery()
->execute()
->fetchField();
// Collect results to process in the finished callback.
$context['results']['node_count'] = $context['sandbox']['max'];
}
$nodes_to_delete = $db
->select('node', 'n')
->fields('n', [
'nid',
])
->condition('nid', $context['sandbox']['current_nid'], '>')
->range(0, 100)
->execute()
->fetchCol();
}
else {
if (!isset($context['sandbox']['progress'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['max'] = count($nodes);
// Collect results to process in the finished callback.
$context['results']['node_count'] = $context['sandbox']['max'];
}
// Get a batch of 100 nodes to delete.
$nodes_to_delete = array_slice($nodes, $context['sandbox']['progress'], 100);
}
if ($context['sandbox']['max'] + 1 > 0) {
if (!empty($nodes_to_delete)) {
foreach ($nodes_to_delete as $nid) {
// Delete node.
$node = Node::load($nid);
$node
->delete();
$context['message'] = t('Deleting node with nid %nid', [
'%nid' => $nid,
]);
$context['sandbox']['current_nid'] = $nid;
$context['sandbox']['progress']++;
}
}
// Inform the batch engine that we are not finished,
// and provide an estimation of the completion level we reached.
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
}