Field classes implement the Drupal\Core\Access\AccessibleInterface
and therefore have to provide an access()
method for access control. \Drupal\Core\Entity\Field\Type\Field
is a base class for fields and provides a sensible default implementation for access()
that invokes access hooks. Contributed field classes are encouraged to re-use this implementation and provide their own access logic in a defaultAccess() method that is invoked from \Drupal\Core\Entity\Field\Type\Field::access()
.
Modules that want to change the access behavior of a field can use hook_entity_field_access()
or hook_entity_field_access_alter()
.
Drupal 7:
function mymodule_field_access($op, $field, $entity_type, $entity, $account) {
if ($field['field_name'] == 'field_of_interest' && $op == 'edit') {
return user_access('edit field of interest', $account);
}
return TRUE;
}
Drupal 8:
function mymodule_entity_field_access($operation, $field, $account) {
if ($field->getName() == 'field_of_interest' && $operation == 'update') {
return user_access('update field of interest', $account);
}
}