W3cubDocs

/Drupal 8

function hook_entity_field_access_alter

hook_entity_field_access_alter(array &$grants, array $context)

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

Hooks
Define functions that alter the behavior of Drupal core.

File

core/lib/Drupal/Core/Entity/entity.api.php, line 1902
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']);
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!entity.api.php/function/hook_entity_field_access_alter/8.1.x