W3cubDocs

/Drupal 8

function hook_entity_field_access

hook_entity_field_access($operation, \Drupal\Core\Field\FieldDefinitionInterface $field_definition, \Drupal\Core\Session\AccountInterface $account, \Drupal\Core\Field\FieldItemListInterface $items = NULL)

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\Access\AccessibleInterface::access() 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 on which the operation is to be performed.

Return value

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

Related topics

Hooks
Define functions that alter the behavior of Drupal core.

File

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

© 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/8.1.x