You are here

function _commerce_file_field_view_access in Commerce File 7

Returns TRUE if a user has view access to commerce_file fields on any field instance

1 call to _commerce_file_field_view_access()
commerce_file_field_formatter_view in ./commerce_file.module
Implements hook_field_formatter_view().

File

./commerce_file.module, line 895
Provides integration of file licenses with Commerce

Code

function _commerce_file_field_view_access($entity_type, $entity, $field, $instance, $langcode, $items, $display, $account = NULL) {
  global $user;
  $access = FALSE;
  $account = isset($account) ? $account : $user;
  if (user_access('administer ' . COMMERCE_FILE_FIELD_TYPE . ' field type', $account)) {

    // provide link to field type admins
    $access = TRUE;
  }
  elseif ($entity_type == COMMERCE_FILE_LICENSE_ENTITY_NAME) {

    // check license view access
    $access = _commerce_file_field_view_access_license($entity, $account);
  }
  else {

    // Extract fid's and query active licenses
    $fids = array();
    foreach ($items as $delta => $item) {
      if (!empty($item['fid'])) {
        $fids[] = $item['fid'];
      }
    }
    if (!empty($fids)) {
      $licenses = commerce_file_license_load_by_property($fids, array(), $account);
      if (!empty($licenses)) {
        foreach ($licenses as $license) {
          if (_commerce_file_field_view_access_license($license, $account)) {
            $access = TRUE;
            break;
          }
        }
      }
    }
  }
  return $access;
}