id()]; if ($op == 'view' && $account->hasPermission('node test view', $account)) { $grants['node_access_test'] = [8888, 8889]; } $no_access_uid = \Drupal::state()->get('node_access_test.no_access_uid') ?: 0; if ($op == 'view' && $account->id() == $no_access_uid) { $grants['node_access_all'] = [0]; } return $grants; } /** * Implements hook_node_access_records(). * * By default, records are written for all nodes. When the * 'node_access_test.private' state variable is set to TRUE, records * are only written for nodes with a "private" property set, which causes the * Node module to write the default global view grant for nodes that are not * marked private. * * @see \Drupal\node\Tests\NodeAccessBaseTableTest::setUp() * @see node_access_test_node_grants() * @see node_access_test.permissions.yml */ function node_access_test_node_access_records(NodeInterface $node) { $grants = []; // For NodeAccessBaseTableTestCase, only set records for private nodes. if (!\Drupal::state()->get('node_access_test.private') || $node->private->value) { // Groups 8888 and 8889 for the node_access_test realm both receive a view // grant for all controlled nodes. See node_access_test_node_grants(). $grants[] = [ 'realm' => 'node_access_test', 'gid' => 8888, 'grant_view' => 1, 'grant_update' => 0, 'grant_delete' => 0, 'priority' => 0, ]; $grants[] = [ 'realm' => 'node_access_test', 'gid' => 8889, 'grant_view' => 1, 'grant_update' => 0, 'grant_delete' => 0, 'priority' => 0, ]; // For the author realm, the group ID is equivalent to a user ID, which // means there are many many groups of just 1 user. $grants[] = [ 'realm' => 'node_access_test_author', 'gid' => $node->getOwnerId(), 'grant_view' => 1, 'grant_update' => 1, 'grant_delete' => 1, 'priority' => 0, ]; } return $grants; } /** * Adds the private field to a node type. * * @param \Drupal\node\NodeTypeInterface $type * A node type entity. */ function node_access_test_add_field(NodeTypeInterface $type) { $field_storage = FieldStorageConfig::create([ 'field_name' => 'private', 'entity_type' => 'node', 'type' => 'integer', ]); $field_storage->save(); $field = FieldConfig::create([ 'field_name' => 'private', 'entity_type' => 'node', 'bundle' => $type->id(), 'label' => 'Private', ]); $field->save(); // Assign widget settings for the 'default' form mode. entity_get_form_display('node', $type->id(), 'default') ->setComponent('private', [ 'type' => 'number', ]) ->save(); } /** * Implements hook_node_access(). */ function node_access_test_node_access(NodeInterface $node, $op, AccountInterface $account) { $secret_catalan = \Drupal::state() ->get('node_access_test_secret_catalan') ?: 0; if ($secret_catalan && $node->language()->getId() == 'ca') { // Make all Catalan content secret. return AccessResult::forbidden()->setCacheMaxAge(0); } // Grant access if a specific user is specified. if (\Drupal::state()->get('node_access_test.allow_uid') === $account->id()) { return AccessResult::allowed(); } // No opinion. return AccessResult::neutral()->setCacheMaxAge(0); }