You are here

function field_collection_file_feeds_set_target in Field collection 7

Wrapper function for file_feeds_set_target as it doesn't do the nested stuff.

1 string reference to 'field_collection_file_feeds_set_target'
field_collection_feeds_set_target in ./field_collection.module
Process Field Collection items.

File

./field_collection.module, line 2398
Module implementing field collection field type.

Code

function field_collection_file_feeds_set_target($source, $entity, $target, $value) {
  if (empty($value)) {
    return;
  }
  module_load_include('inc', 'file');

  // Make sure $value is an array of objects of type FeedsEnclosure.
  if (!is_array($value)) {
    $value = array(
      $value,
    );
  }
  foreach ($value as $k => $v) {
    if (!$v instanceof FeedsEnclosure) {
      if (is_string($v)) {
        $value[$k] = new FeedsEnclosure($v, file_get_mimetype($v));
      }
      else {
        unset($value[$k]);
      }
    }
  }
  if (empty($value)) {
    return;
  }
  $entity_type = 'field_collection_item';
  $bundle = $entity->field_name;

  // Determine file destination.
  // @todo This needs review and debugging.
  $instance_info = field_info_instance($entity_type, $target, $bundle);
  $info = field_info_field($target);
  $data = array();
  if (!empty($entity->uid)) {
    $data[$entity_type] = $entity;
  }
  $destination = file_field_widget_uri($info, $instance_info, $data);

  // Populate entity.
  $i = 0;
  $field = isset($entity->{$target}) ? $entity->{$target} : array();
  foreach ($value as $v) {
    $file = FALSE;
    try {
      $v
        ->setAllowedExtensions($instance_info['settings']['file_extensions']);
      $file = $v
        ->getFile($destination);
    } catch (Exception $e) {
      watchdog('feeds', check_plain($e
        ->getMessage()));
    }
    if ($file) {
      $field[LANGUAGE_NONE][$i] = (array) $file;

      // @todo: Figure out how to properly populate this field.
      $field[LANGUAGE_NONE][$i]['display'] = 1;
      if ($info['cardinality'] == 1) {
        break;
      }
      $i++;
    }
  }
  $entity->{$target} = $field;
}