You are here

public function NodeAddAccessCheck::access in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/node/src/Access/NodeAddAccessCheck.php \Drupal\node\Access\NodeAddAccessCheck::access()

Checks access to the node add page for the node type.

Parameters

\Drupal\Core\Session\AccountInterface $account: The currently logged in account.

\Drupal\node\NodeTypeInterface $node_type: (optional) The node type. If not specified, access is allowed if there exists at least one node type for which the user may create a node.

Return value

string A \Drupal\Core\Access\AccessInterface constant value.

File

core/modules/node/src/Access/NodeAddAccessCheck.php, line 52
Contains \Drupal\node\Access\NodeAddAccessCheck.

Class

NodeAddAccessCheck
Determines access to for node add pages.

Namespace

Drupal\node\Access

Code

public function access(AccountInterface $account, NodeTypeInterface $node_type = NULL) {
  $access_control_handler = $this->entityManager
    ->getAccessControlHandler('node');

  // If checking whether a node of a particular type may be created.
  if ($account
    ->hasPermission('administer content types')) {
    return AccessResult::allowed()
      ->cachePerPermissions();
  }
  if ($node_type) {
    return $access_control_handler
      ->createAccess($node_type
      ->id(), $account, [], TRUE);
  }

  // If checking whether a node of any type may be created.
  foreach ($this->entityManager
    ->getStorage('node_type')
    ->loadMultiple() as $node_type) {
    if (($access = $access_control_handler
      ->createAccess($node_type
      ->id(), $account, [], TRUE)) && $access
      ->isAllowed()) {
      return $access;
    }
  }

  // No opinion.
  return AccessResult::neutral();
}