You are here

public function PrivateAccess::hasFieldAccess in Field Permissions 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/FieldPermissionType/PrivateAccess.php \Drupal\field_permissions\Plugin\FieldPermissionType\PrivateAccess::hasFieldAccess()

Determine if access to the field is granted for a given account.

Parameters

string $operation: The operation to check. Either 'view' or 'edit'.

\Drupal\Core\Entity\EntityInterface $entity: The entity the field is attached to.

\Drupal\Core\Session\AccountInterface $account: The user to check access for.

Return value

bool The access result.

Overrides FieldPermissionTypeInterface::hasFieldAccess

File

src/Plugin/FieldPermissionType/PrivateAccess.php, line 25

Class

PrivateAccess
Defines a private field type.

Namespace

Drupal\field_permissions\Plugin\FieldPermissionType

Code

public function hasFieldAccess($operation, EntityInterface $entity, AccountInterface $account) {
  if ($account
    ->hasPermission('access private fields')) {
    return TRUE;
  }

  // Users can access the field when creating new entities.
  if ($entity
    ->isNew()) {
    return TRUE;
  }

  // Special handling for 'user' entities.
  if ($entity instanceof UserInterface) {
    return $entity
      ->id() == $account
      ->id();
  }
  elseif ($entity instanceof EntityOwnerInterface) {
    return $entity
      ->getOwnerId() == $account
      ->id();
  }
  return TRUE;
}