You are here

function private_content_entity_field_access in Private 8.2

Implements hook_entity_field_access().

File

./private_content.module, line 187
A tremendously simple access control module -- it allows users to mark individual nodes as private; users with 'access private content' perms can read these nodes, while others cannot.

Code

function private_content_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemList $items = NULL) {
  if ($field_definition
    ->getName() != 'private' || $operation != 'edit') {
    return AccessResult::neutral();
  }
  if (private_content_is_locked($items
    ->getEntity())) {
    return AccessResult::forbidden();
  }
  if (!$account
    ->hasPermission('mark content as private')) {
    return AccessResult::forbidden()
      ->cachePerPermissions();
  }
  return AccessResult::neutral();

  // @todo Better code would be to use FieldItemList::defaultAccess
  // Need @FieldType with list_class = 'xxx'
  // That allows other code to grant permissions to selectively mark content as private.
  // return AccessResult::allowedIfHasPermission($account, 'mark content as private');
}