function _node_access_rebuild_batch_operation

Same name in other branches
  1. 7.x modules/node/node.module \_node_access_rebuild_batch_operation()
  2. 8.9.x core/modules/node/node.module \_node_access_rebuild_batch_operation()
  3. 10 core/modules/node/node.module \_node_access_rebuild_batch_operation()
  4. 11.x 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

File

core/modules/node/node.module, line 1125

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.