You are here

function _node_access_rebuild_batch_operation in Drupal 8

Same name and namespace in other branches
  1. 6 modules/node/node.module \_node_access_rebuild_batch_operation()
  2. 7 modules/node/node.module \_node_access_rebuild_batch_operation()
  3. 9 core/modules/node/node.module \_node_access_rebuild_batch_operation()
  4. 10 core/modules/node/node.module \_node_access_rebuild_batch_operation()

Implements callback_batch_operation().

Performs batch operation for node_access_rebuild().

This is a multistep operation: we go through all nodes by packs of 20. The batch processing engine interrupts processing and sends progress feedback after 1 second execution time.

Parameters

array $context: An array of contextual key/value information for rebuild batch process.

Related topics

1 string reference to '_node_access_rebuild_batch_operation'
node_access_rebuild in core/modules/node/node.module
Rebuilds the node access database.

File

core/modules/node/node.module, line 1310
The core module that allows content to be submitted to the site.

Code

function _node_access_rebuild_batch_operation(&$context) {
  $node_storage = \Drupal::entityTypeManager()
    ->getStorage('node');
  if (empty($context['sandbox'])) {

    // Initiate multistep processing.
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_node'] = 0;
    $context['sandbox']['max'] = \Drupal::entityQuery('node')
      ->accessCheck(FALSE)
      ->count()
      ->execute();
  }

  // Process the next 20 nodes.
  $limit = 20;
  $nids = \Drupal::entityQuery('node')
    ->condition('nid', $context['sandbox']['current_node'], '>')
    ->sort('nid', 'ASC')
    ->accessCheck(FALSE)
    ->range(0, $limit)
    ->execute();
  $node_storage
    ->resetCache($nids);
  $nodes = Node::loadMultiple($nids);
  foreach ($nodes as $nid => $node) {

    // To preserve database integrity, only write grants if the node
    // loads successfully.
    if (!empty($node)) {

      /** @var \Drupal\node\NodeAccessControlHandlerInterface $access_control_handler */
      $access_control_handler = \Drupal::entityTypeManager()
        ->getAccessControlHandler('node');
      $grants = $access_control_handler
        ->acquireGrants($node);
      \Drupal::service('node.grant_storage')
        ->write($node, $grants);
    }
    $context['sandbox']['progress']++;
    $context['sandbox']['current_node'] = $nid;
  }

  // Multistep processing : report progress.
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}