function NodeAccessRebuild::batchOperation

Same name and namespace in other branches
  1. 11.x core/modules/node/src/NodeAccessRebuild.php \Drupal\node\NodeAccessRebuild::batchOperation()

Implements callback_batch_operation().

Performs batch operation for \Drupal\node\NodeAccessRebuild::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.

File

core/modules/node/src/NodeAccessRebuild.php, line 166

Class

NodeAccessRebuild
Provides methods for checking and rebuilding node access permissions.

Namespace

Drupal\node

Code

public function batchOperation(array &$context) : void {
  $node_storage = $this->entityTypeManager
    ->getStorage('node');
  if (empty($context['sandbox'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_node'] = 0;
    $context['sandbox']['max'] = $node_storage->getQuery()
      ->accessCheck(FALSE)
      ->count()
      ->execute();
  }
  $limit = 20;
  $nids = $node_storage->getQuery()
    ->condition('nid', $context['sandbox']['current_node'], '>')
    ->sort('nid', 'ASC')
    ->accessCheck(FALSE)
    ->range(0, $limit)
    ->execute();
  $node_storage->resetCache($nids);
  $nodes = $node_storage->loadMultiple($nids);
  /** @var \Drupal\node\NodeAccessControlHandlerInterface $access_control_handler */
  $access_control_handler = $this->entityTypeManager
    ->getAccessControlHandler('node');
  foreach ($nids as $nid) {
    $node = $nodes[$nid] ?? NULL;
    // To preserve database integrity, only write grants if the node
    // loads successfully.
    if ($node instanceof NodeInterface) {
      $grants = $access_control_handler->acquireGrants($node);
      $this->grantStorage
        ->write($node, $grants);
    }
    $context['sandbox']['progress']++;
    $context['sandbox']['current_node'] = $nid;
  }
  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.