Chris@0: 'entity.manager']; Chris@18: Chris@18: /** Chris@18: * The entity type manager. Chris@0: * Chris@18: * @var \Drupal\Core\Entity\EntityTypeManagerInterface Chris@0: */ Chris@18: protected $entityTypeManager; Chris@0: Chris@0: /** Chris@0: * Constructs a EntityCreateAccessCheck object. Chris@0: * Chris@18: * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager Chris@18: * The entity type manager. Chris@0: */ Chris@18: public function __construct(EntityTypeManagerInterface $entity_type_manager) { Chris@18: $this->entityTypeManager = $entity_type_manager; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks access to the node add page for the node type. Chris@0: * Chris@0: * @param \Drupal\Core\Session\AccountInterface $account Chris@0: * The currently logged in account. Chris@0: * @param \Drupal\node\NodeTypeInterface $node_type Chris@0: * (optional) The node type. If not specified, access is allowed if there Chris@0: * exists at least one node type for which the user may create a node. Chris@0: * Chris@0: * @return string Chris@0: * A \Drupal\Core\Access\AccessInterface constant value. Chris@0: */ Chris@0: public function access(AccountInterface $account, NodeTypeInterface $node_type = NULL) { Chris@18: $access_control_handler = $this->entityTypeManager->getAccessControlHandler('node'); Chris@0: // If checking whether a node of a particular type may be created. Chris@0: if ($account->hasPermission('administer content types')) { Chris@0: return AccessResult::allowed()->cachePerPermissions(); Chris@0: } Chris@0: if ($node_type) { Chris@0: return $access_control_handler->createAccess($node_type->id(), $account, [], TRUE); Chris@0: } Chris@0: // If checking whether a node of any type may be created. Chris@18: foreach ($this->entityTypeManager->getStorage('node_type')->loadMultiple() as $node_type) { Chris@0: if (($access = $access_control_handler->createAccess($node_type->id(), $account, [], TRUE)) && $access->isAllowed()) { Chris@0: return $access; Chris@0: } Chris@0: } Chris@0: Chris@0: // No opinion. Chris@0: return AccessResult::neutral(); Chris@0: } Chris@0: Chris@0: }