function _node_access_write_grants

Writes a list of grants to the database, deleting any previously saved ones.

If a realm is provided, it will only delete grants from that realm, but it will always delete a grant from the 'all' realm. Modules that utilize node_access() can use this function when doing mass updates due to widespread permission changes.

Note: Don't call this function directly from a contributed module. Call node_access_acquire_grants() instead.

Parameters

\Drupal\Core\Entity\EntityInterface $node: The node whose grants are being written.

$grants: A list of grants to write. See hook_node_access_records() for the expected structure of the grants array.

$realm: (optional) If provided, read/write grants for that realm only. Defaults to NULL.

$delete: (optional) If false, does not delete records. This is only for optimization purposes, and assumes the caller has already performed a mass delete of some form. Defaults to TRUE.

See also

node_access_acquire_grants()

Related topics

1 call to _node_access_write_grants()
node_access_acquire_grants in drupal/core/modules/node/node.module
Gets the list of node access grants and writes them to the database.

File

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

Code

function _node_access_write_grants(EntityInterface $node, $grants, $realm = NULL, $delete = TRUE) {
  if ($delete) {
    $query = db_delete('node_access')
      ->condition('nid', $node->nid);
    if ($realm) {
      $query
        ->condition('realm', array(
        $realm,
        'all',
      ), 'IN');
    }
    $query
      ->execute();
  }

  // Only perform work when node_access modules are active.
  if (!empty($grants) && count(module_implements('node_grants'))) {
    $query = db_insert('node_access')
      ->fields(array(
      'nid',
      'langcode',
      'fallback',
      'realm',
      'gid',
      'grant_view',
      'grant_update',
      'grant_delete',
    ));

    // If we have defined a granted langcode, use it. But if not, add a grant
    // for every language this node is translated to.
    foreach ($grants as $grant) {
      if ($realm && $realm != $grant['realm']) {
        continue;
      }
      if (isset($grant['langcode'])) {
        $grant_languages = array(
          $grant['langcode'] => language_load($grant['langcode']),
        );
      }
      else {
        $grant_languages = $node
          ->getTranslationLanguages(TRUE);
      }
      foreach ($grant_languages as $grant_langcode => $grant_language) {

        // Only write grants; denies are implicit.
        if ($grant['grant_view'] || $grant['grant_update'] || $grant['grant_delete']) {
          $grant['nid'] = $node->nid;
          $grant['langcode'] = $grant_langcode;

          // The record with the original langcode is used as the fallback.
          if ($grant['langcode'] == $node->langcode) {
            $grant['fallback'] = 1;
          }
          else {
            $grant['fallback'] = 0;
          }
          $query
            ->values($grant);
        }
      }
    }
    $query
      ->execute();
  }
}