function NodeAccessTestHooks::nodeGrants

Implements hook_node_grants().

Provides three grant realms:

  • node_access_test_author: Grants users view, update, and delete privileges on nodes they have authored. Users receive a group ID matching their user ID on this realm.
  • node_access_test: Grants users view privileges when they have the 'node test view' permission. Users with this permission receive two group IDs for the realm, 8888 and 8889. Access for both realms is identical; the second group is added so that the interaction of multiple groups on a given grant realm can be tested in NodeAccessPagerTest.
  • node_access_all: Provides grants for the user whose user ID matches the 'node_access_test.no_access_uid' state variable. Access control on this realm is not provided in this module; instead, NodeQueryAlterTest::testNodeQueryAlterOverride() manually writes a node access record defining the access control for this realm.

See also

\Drupal\node\Tests\NodeQueryAlterTest::testNodeQueryAlterOverride()

\Drupal\node\Tests\NodeAccessPagerTest

node_access_test.permissions.yml

node_access_test_node_access_records()

File

core/modules/node/tests/modules/node_access_test/src/Hook/NodeAccessTestHooks.php, line 40

Class

NodeAccessTestHooks
Hook implementations for node_access_test.

Namespace

Drupal\node_access_test\Hook

Code

public function nodeGrants($account, $operation) {
    $grants = [];
    $grants['node_access_test_author'] = [
        $account->id(),
    ];
    if ($operation == 'view' && $account->hasPermission('node test view')) {
        $grants['node_access_test'] = [
            8888,
            8889,
        ];
    }
    $no_access_uid = \Drupal::state()->get('node_access_test.no_access_uid', 0);
    if ($operation == 'view' && $account->id() == $no_access_uid) {
        $grants['node_access_all'] = [
            0,
        ];
    }
    return $grants;
}

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