Change field access per user I am trying to customize the access of user fields. We have an internal list of all users, which all members can access (for things like mail/address/phone number). However, due to privacy, each user should be able to manage these field, and can hide certains fields from showing to particular members or groups.
For this, I created a database table which stores all preferences, and fetch them using dynamic query. For the field access, I use
function MY_MODULE_has_access($operation, $field_machine_name, $uid){
  Code for determining access here
  return if $uid has access
}
function MY_MODULE_entity_field_access($operation, $field_definition, $account, $items = NULL) {
  if ($field_definition-&gt;getName() == &quot;field_address&quot;) {
    $user = \Drupal::currentUser();
    $result = AccessResult::forbiddenIf(!MY_MODULE_has_access($operation, &quot;field_address&quot;, $user-&gt;id()));
    $result-&gt;addCacheableDependency($user);
    return $result;
  }
  return AccessResult::neutral();
}

However, due to caching, this only works one time. If the user changes its preferences, the field access still remains unchanged. How can I fix this?
The variable $user should probably be the parent entity of the field:
$user = $items-&gt;getEntity();

Which then when used as cache dependency should solve the caching issue:
$result-&gt;addCacheableDependency($user);

BTW you find the current user already in $account. If your access check depends on it then don't add it as object but use this predefined method for caching:
$result-&gt;cachePerUser();

But most times access code depends only on the current user roles and the resulting permissions, then you could use instead
$result-&gt;cachePerPermissions();

which is much better for cache performance.