function NodeGrantDatabaseStorage::write

Same name in other branches
  1. 8.9.x core/modules/node/src/NodeGrantDatabaseStorage.php \Drupal\node\NodeGrantDatabaseStorage::write()
  2. 10 core/modules/node/src/NodeGrantDatabaseStorage.php \Drupal\node\NodeGrantDatabaseStorage::write()
  3. 11.x core/modules/node/src/NodeGrantDatabaseStorage.php \Drupal\node\NodeGrantDatabaseStorage::write()

Overrides NodeGrantDatabaseStorageInterface::write

File

core/modules/node/src/NodeGrantDatabaseStorage.php, line 202

Class

NodeGrantDatabaseStorage
Defines a storage handler class that handles the node grants system.

Namespace

Drupal\node

Code

public function write(NodeInterface $node, array $grants, $realm = NULL, $delete = TRUE) {
    if ($delete) {
        $query = $this->database
            ->delete('node_access')
            ->condition('nid', $node->id());
        if ($realm) {
            $query->condition('realm', [
                $realm,
                'all',
            ], 'IN');
        }
        $query->execute();
    }
    // Only perform work when node_access modules are active.
    if (!empty($grants) && $this->moduleHandler
        ->hasImplementations('node_grants')) {
        $query = $this->database
            ->insert('node_access')
            ->fields([
            '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.
        $fallback_langcode = $node->getUntranslated()
            ->language()
            ->getId();
        foreach ($grants as $grant) {
            if ($realm && $realm != $grant['realm']) {
                continue;
            }
            if (isset($grant['langcode'])) {
                $grant_languages = [
                    $grant['langcode'] => $this->languageManager
                        ->getLanguage($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->id();
                    $grant['langcode'] = $grant_langcode;
                    // The record with the original langcode is used as the fallback.
                    if ($grant['langcode'] == $fallback_langcode) {
                        $grant['fallback'] = 1;
                    }
                    else {
                        $grant['fallback'] = 0;
                    }
                    $query->values($grant);
                }
            }
        }
        $query->execute();
    }
}

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