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

Alter the default access behavior for a given field.

Use this hook to override access grants from another module. Note that the original default access flag is masked under the ':default' key.

Parameters

\Drupal\Core\Access\AccessResultInterface[] $grants: An array of grants gathered by hook_entity_field_access(). The array is keyed by the module that defines the field's access control; the values are grant responses for each module (\Drupal\Core\Access\AccessResult).

array $context: Context array on the performed operation with the following keys:

Related topics

2 functions implement hook_entity_field_access_alter()

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

entity_serialization_test_entity_field_access_alter in core/modules/serialization/tests/modules/entity_serialization_test/entity_serialization_test.module
Implements hook_entity_field_access_alter().
entity_test_entity_field_access_alter in core/modules/system/tests/modules/entity_test/entity_test.module
Implements hook_entity_field_access_alter().

File

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

Code

function hook_entity_field_access_alter(array &$grants, array $context) {

  /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
  $field_definition = $context['field_definition'];
  if ($field_definition
    ->getName() == 'field_of_interest' && $grants['node']
    ->isForbidden()) {

    // Override node module's restriction to no opinion (neither allowed nor
    // forbidden). We don't want to provide our own access hook, we only want to
    // take out node module's part in the access handling of this field. We also
    // don't want to switch node module's grant to
    // AccessResultInterface::isAllowed() , because the grants of other modules
    // should still decide on their own if this field is accessible or not
    $grants['node'] = AccessResult::neutral()
      ->inheritCacheability($grants['node']);
  }
}