function field_permissions_empty_entity_access in Field Permissions 7
Determines custom field permissions access when the entity is unknown.
When a module calls field_access() without providing an entity (which the API allows it to do), it is doing so in order to check generic access to the field. Therefore, we should only deny access if we know that there is no entity anywhere on the site for which the user has access to the provided field.
For example, Views calls field_access('view') without providing the entity, in order to determine if the field can be included in the query itself. So we only want to return FALSE if we know that there are no entities for which access will be granted. Later on, Views will invoke field_access('view') again, indirectly, when rendering the fields using field_view_field(), and at that point the entity will be passed along so we can do our normal checks on it.
As another example, the FileField Sources module uses field_access('edit') as a menu access callback for the IMCE file browser and does not pass along the entity. So we must return TRUE here if there is any entity for which the user is allowed to edit the field (otherwise the user would not have access to the IMCE file browser interface when editing the fields they do have permission to edit).
Parameters
$op: The operation to be performed ('view' or 'edit').
$field_name: The name of the field whose access is being checked.
$account: The user account whose access is being checked.
Return value
TRUE if access should be allowed, or FALSE if it shouln't.
1 call to field_permissions_empty_entity_access()
- field_permissions_field_access in ./
field_permissions.module - Implementation of hook_field_access().
File
- ./
field_permissions.module, line 186 - This is the main script for the Field Permissions module. It merely contains the implementation of hooks invoked by Drupal core and CCK. All common functions are externalized into several scripts that are included on demand to save memory consumption…
Code
function field_permissions_empty_entity_access($op, $field_name, $account) {
$all_permissions['view'] = array(
'view ' . $field_name,
'view own ' . $field_name,
);
$all_permissions['edit'] = array(
'create ' . $field_name,
'edit ' . $field_name,
'edit own ' . $field_name,
);
// If there's any scenario where the user might have permission to perform
// the operation on the field, return TRUE.
if (isset($all_permissions[$op])) {
foreach ($all_permissions[$op] as $permission) {
if (user_access($permission, $account)) {
return TRUE;
}
}
}
return FALSE;
}