You are here

function hook_entity_field_access in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_field_access()
  2. 9 core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_field_access()

Control access to fields.

This hook is invoked from \Drupal\Core\Entity\EntityAccessControlHandler::fieldAccess() to let modules grant or deny operations on fields.

Parameters

string $operation: The operation to be performed. See \Drupal\Core\Entity\EntityAccessControlHandlerInterface::fieldAccess() for possible values.

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The field definition.

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

\Drupal\Core\Field\FieldItemListInterface $items: (optional) The entity field object for which to check access, or NULL if access is checked for the field definition, without any specific value available. Defaults to NULL.

Return value

\Drupal\Core\Access\AccessResultInterface The access result.

See also

\Drupal\Core\Entity\EntityAccessControlHandlerInterface::fieldAccess()

Related topics

12 functions implement hook_entity_field_access()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

content_moderation_entity_field_access in core/modules/content_moderation/content_moderation.module
Implements hook_entity_field_access().
entity_test_entity_field_access in core/modules/system/tests/modules/entity_test/entity_test.module
Implements hook_entity_field_access().
field_test_boolean_access_denied_entity_field_access in core/modules/field/tests/modules/field_test_boolean_access_denied/field_test_boolean_access_denied.module
Implements hook_entity_field_access().
field_test_entity_field_access in core/modules/field/tests/modules/field_test/field_test.field.inc
Implements hook_entity_field_access().
image_access_test_hidden_entity_field_access in core/modules/image/tests/modules/image_access_test_hidden/image_access_test_hidden.module
Implements hook_entity_field_access().

... See full list

File

core/lib/Drupal/Core/Entity/entity.api.php, line 2111
Hooks and documentation related to entities.

Code

function hook_entity_field_access($operation, \Drupal\Core\Field\FieldDefinitionInterface $field_definition, \Drupal\Core\Session\AccountInterface $account, \Drupal\Core\Field\FieldItemListInterface $items = NULL) {
  if ($field_definition
    ->getName() == 'field_of_interest' && $operation == 'edit') {
    return AccessResult::allowedIfHasPermission($account, 'update field of interest');
  }
  return AccessResult::neutral();
}