You are here

function _commerce_file_field_aggregate_files in Commerce File 7

Return all commerce_file fields for a given entity, indexed by fid

2 calls to _commerce_file_field_aggregate_files()
CommerceFileLicenseEntity::get_file in includes/commerce_file_license.entity.inc
File
CommerceFileLicenseEntity::get_limits in includes/commerce_file_license.entity.inc
Aggregated Limits

File

includes/commerce_file.field.inc, line 770
Implement an commerce_file field, based on the file module's file field.

Code

function _commerce_file_field_aggregate_files($entity, $entity_type = NULL) {
  $aggregated = array();
  $field_type = COMMERCE_FILE_FIELD_TYPE;
  if (empty($entity_type)) {
    $entity_type = _commerce_file_get_entity_type($entity);
    if (empty($entity_type)) {
      return $aggregated;
    }
  }

  // get some entity info
  $entity_wrapper = entity_metadata_wrapper($entity_type, $entity);
  $entity_id = $entity_wrapper
    ->getIdentifier();
  $entity_bundle = $entity_wrapper
    ->getBundle();

  // get field instances for type and bundle
  $instances = _commerce_file_field_info_instances($entity_type, $entity_bundle, $field_type);
  if (empty($instances)) {
    return $aggregated;
  }
  $file_items = array();
  foreach ($instances as $field_name => $instance) {
    $items = $entity_wrapper->{$field_name}
      ->value();
    if (empty($items)) {
      continue;
    }

    // make an array for single value
    if (isset($items['fid'])) {
      $items = array(
        $items,
      );
    }

    // index items on fid
    foreach ($items as $item) {
      if (isset($item['fid'])) {
        $file_items[$item['fid']][] = _commerce_file_license_resolve_settings($item, $instance);
      }
    }
  }

  // aggregate per file
  foreach ($file_items as $fid => $items) {
    $aggregated[$fid] = call_user_func_array('_commerce_file_field_aggregate_field_items', $items);
  }
  return $aggregated;
}