function NodeAccessTestHooks::nodeAccess

Implements hook_ENTITY_TYPE_access().

Attributes

#[Hook('node_access')]

File

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

Class

NodeAccessTestHooks
Hook implementations for node_access_test.

Namespace

Drupal\node_access_test\Hook

Code

public function nodeAccess(NodeInterface $node, $operation, AccountInterface $account) : AccessResultInterface {
  $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);
}

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