You are here

function og_field_access_field_access in Organic groups 7.2

Same name and namespace in other branches
  1. 7 og_field_access/og_field_access.module \og_field_access_field_access()

Implements hook_field_access().

File

og_field_access/og_field_access.module, line 11
Provide field access based on group.

Code

function og_field_access_field_access($op, $field, $entity_type, $entity, $account) {
  if (empty($entity)) {

    // $entity might be NULL, so return early.
    // @see field_access().
    return;
  }
  if (user_access('administer group')) {
    return TRUE;
  }
  $perm = $op == 'view' ? 'view ' . $field['field_name'] . ' field' : 'update ' . $field['field_name'] . ' field';
  list($id, , $bundle) = entity_extract_ids($entity_type, $entity);

  // Show fields in user registration form.
  if ($entity_type == 'user' && !$id) {
    return;
  }
  if (!$id && $op == 'edit' && (og_is_group($entity_type, $entity) || og_is_group_content_type($entity_type, $bundle))) {

    // This is create form of a non-saved entity, so we check
    // permissions to access the field, for all the groups the user is a
    // member.
    foreach (og_get_entity_groups() as $group_type => $gids) {
      foreach ($gids as $gid) {
        if (og_user_access($group_type, $gid, $perm)) {
          return TRUE;
        }
      }
    }
    return FALSE;
  }

  // Return result only if the entity is related to OG.
  $access = og_user_access_entity($perm, $entity_type, $entity, $account);
  if (!is_null($access)) {
    return $access;
  }
}