You are here

function content_access_mass_update in Content Access 8

Same name and namespace in other branches
  1. 6 content_access.admin.inc \content_access_mass_update()
  2. 7 content_access.admin.inc \content_access_mass_update()

Mass updates node access records for nodes of the given types.

Parameters

array $types: An array of content type names.

Return value

bool Whether the operation has been processed successfully or postponed.

2 calls to content_access_mass_update()
ContentAccessAdminSettingsForm::submitForm in src/Form/ContentAccessAdminSettingsForm.php
Form submission handler.
content_access_user_admin_perm_submit in ./content_access.module
Submit callback for the user permissions form.

File

./content_access.module, line 763
Content access module file.

Code

function content_access_mass_update(array $types) {
  $query = \Drupal::database()
    ->select('node', 'n')
    ->fields('n', [
    'nid',
  ])
    ->condition('n.type', $types, 'IN');
  $count = $query
    ->countQuery()
    ->execute()
    ->fetchField();
  node_access_needs_rebuild(TRUE);

  // If there not too much nodes affected, try to do it.
  if ($count <= ContentAccessInterface::CONTENT_ACCESS_MASS_UPDATE_THRESHOLD) {
    $records = $query
      ->execute();
    foreach ($records as $node) {
      $node = Node::load($node->nid);
      $grants = \Drupal::entityTypeManager()
        ->getAccessControlHandler('node')
        ->acquireGrants($node);
      \Drupal::service('node.grant_storage')
        ->write($node, $grants);
    }
    foreach (Cache::getBins() as $cache_backend) {
      $cache_backend
        ->deleteAll();
    }
    node_access_needs_rebuild(FALSE);
    return TRUE;
  }
  return FALSE;
}