You are here

public static function LayoutBuilderBlockSanitizerBatch::batchSanitizeAllNodes in Layout Builder Block Sanitizer 8

Load nodes in batch process progressively to sanitize.

File

src/LayoutBuilderBlockSanitizerBatch.php, line 72

Class

LayoutBuilderBlockSanitizerBatch
Class LayoutBuilderBlockSanitizerBatch.

Namespace

Drupal\layout_builder_block_sanitizer

Code

public static function batchSanitizeAllNodes($nids, &$context) {

  // Use the $context['sandbox'] at your convenience to store the
  // information needed to track progression between successive calls.
  if (empty($context['sandbox'])) {

    // Flush caches to avoid false positives looking for block UUID.
    drupal_flush_all_caches();
    $context['sandbox'] = [];
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_node'] = 0;

    // Save node count for the termination message.
    $context['sandbox']['max'] = count($nids);
  }
  $limit = 5;

  // Retrieve the next group.
  $current_node = $context['sandbox']['current_node'];
  $limit_node = $context['sandbox']['current_node'] + $limit;
  $result = range($current_node, $limit_node);
  foreach ($result as $row) {
    \Drupal::service('layout_builder_block_sanitizer.manager')
      ->sanitizeNode($nids[$row]);
    $operation_details = t('Sanitizing NIDs :current to :limit', [
      ':current' => $current_node,
      ':limit' => $limit_node,
    ]);

    // Store some results for post-processing in the 'finished' callback.
    // The contents of 'results' will be available as $results in the
    // 'finished' function (in this example, batch_example_finished()).
    $context['results'][] = $row;

    // Update our progress information.
    $context['sandbox']['progress']++;
    $context['sandbox']['current_node'] = $row;
    $context['message'] = t('@details', [
      '@details' => $operation_details,
    ]);
  }

  // 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'];
  }
}