function taxonomy_permissions_entity_field_access in Taxonomy Permissions 8
Implements hook_entity_field_access().
File
Code
function taxonomy_permissions_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
if ($field_definition
->getType() == 'entity_reference' && $field_definition
->getSetting('target_type') == 'taxonomy_term' && $operation == 'edit') {
$handler_settings = $field_definition
->getSetting('handler_settings');
$target_bundles = $handler_settings['target_bundles'];
// We grant access to the taxonomy reference field if the user has the
// permissions to view at least one vocabulary from the target bundles.
$access = FALSE;
foreach ($target_bundles as $vid => $target_bundle) {
if ($account
->hasPermission('view terms in ' . $vid)) {
$access = TRUE;
}
}
if ($access) {
return AccessResult::allowed();
}
else {
return AccessResult::forbidden();
}
}
return AccessResult::neutral();
}