You are here

function node_gallery_api_get_item_file in Node Gallery 7

Get item files from NIDs.

Parameters

array $nids: Array of node NIDs.

string $fieldname: File field name

Return value

array Files

File

./node_gallery_api.inc, line 302
Node Gallery API function

Code

function node_gallery_api_get_item_file($nids, $fieldname) {
  $fields =& drupal_static(__FUNCTION__, array());
  static $fields = array();
  if ($fieldname == 'node_gallery_none') {
    return array();
  }
  if (!isset($fields[$fieldname])) {
    $field_info = field_info_field($fieldname);
    $fields[$fieldname]['db_info'] = array_shift(array_values($field_info['storage']['details']['sql'][FIELD_LOAD_CURRENT]));
    $fields[$fieldname]['db_info'] += array(
      'table' => array_shift(array_keys($field_info['storage']['details']['sql'][FIELD_LOAD_CURRENT])),
    );
  }
  $query = db_select($fields[$fieldname]['db_info']['table'], 'field_table');
  $query
    ->fields('field_table', array(
    $fields[$fieldname]['db_info']['fid'],
    'entity_id',
  ));
  $query
    ->condition('field_table.entity_type', 'node', '=');
  if (!is_array($nids)) {
    $query
      ->condition('field_table.entity_id', $nids, '=');
  }
  else {
    $query
      ->condition('field_table.entity_id', $nids, 'IN');
  }
  $results = $query
    ->execute();
  $files = array();
  foreach ($results as $record) {
    if (!empty($record->{$fields[$fieldname]['db_info']['fid']})) {
      $files[$record->entity_id] = file_load($record->{$fields[$fieldname]['db_info']['fid']});
    }
  }

  // TODO: Get rid of this inconsistency of use.
  if (count($nids) == 1) {
    $files = array_shift($files);
  }
  return $files;
}