You are here

function administerusersbyrole_entity_field_access in Administer Users by Role 8.3

Same name and namespace in other branches
  1. 8.2 administerusersbyrole.module \administerusersbyrole_entity_field_access()

Implements hook_entity_field_access().

File

./administerusersbyrole.module, line 114
Administer Users by Role main module file.

Code

function administerusersbyrole_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
  if ($field_definition
    ->getTargetEntityTypeId() != 'user') {
    return AccessResult::neutral();
  }
  $fields = [
    'name',
    'status',
    'mail',
  ];
  if ($operation == 'view') {
    array_push($fields, 'roles', 'access');
  }
  if (!in_array($field_definition
    ->getName(), $fields)) {
    return AccessResult::neutral();
  }
  if (is_null($items)) {
    if ($operation == 'view') {

      // No field item list is passed.  This can be used to control whether to hide/show the whole column in views.
      // Hence allow if 'access users overview'.
      return AccessResult::allowedIfHasPermission($account, 'access users overview');
    }
    return AccessResult::neutral();
  }

  // Grant access to read/update extra fields to a sub-admin with permission to update the user.
  return administerusersbyrole_user_access($items
    ->getEntity(), 'update', $account);
}