I've disabled File (Field) Paths because some people had a conflict when using it. I've chosen only remote file under file sources. I'm using the latest version of remote file source which should have the rudiedirkx fix. Everything works fine when submitting a node form by hand. But when I try to import an xml podcast feed into the content type, the entire mp3 file is downloaded locally rather than just pointing to the remote url.

Comments

webadpro’s picture

I'm not sure which module you are using to do the import but this hasn't been tested.

mttjn’s picture

Issue summary: View changes

same problem. mp3 file is downloaded to local folder. importing with feeds module, standard setup. any ideas?

Andreas Radloff’s picture

Feeds doesn't care about this module, it just goes ahead and downloads the file. Here's how to fix it in your custom module:


/**
 * Implements hook_feeds_processor_targets_alter().
 *
 * @see FeedsNodeProcessor::getMappingTargets()
 */
function yourmodule_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
  foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
    $info = field_info_field($name);

    if (in_array($info['type'], array('image'))) {
      $targets[$name . ':uri'] = array(
        'callback' => 'yourmodule_feeds_set_target',
      );
    }
  }
}

/**
 * Callback for mapping. Here is where the actual mapping happens.
 *
 * When the callback is invoked, $target contains the name of the field the
 * user has decided to map to and $value contains the value of the feed item
 * element the user has picked as a source.
 */
function yourmodule_feeds_set_target($source, $entity, $target, array $values) {
  // Add default of uri for backwards compatibility.
  list($field_name, $sub_field) = explode(':', $target . ':uri');
  $info = field_info_field($field_name);

  if ($sub_field == 'uri') {

    foreach ($values as $k => $v) {
      if (!($v instanceof FeedsEnclosure)) {
        if (is_string($v)) {
          $values[$k] = new FeedsEnclosure($v, file_get_mimetype($v));
        }
        else {
          // Set the value for FALSE rather than remove it to keep our deltas
          // correct.
          $values[$k] = FALSE;
        }
      }
    }

    $entity_type = $source->importer->processor->entityType();
    $bundle = $source->importer->processor->bundle();

    $instance_info = field_info_instance($entity_type, $field_name, $bundle);

    // Determine file destination.
    // @todo This needs review and debugging.
    $data = array();
    if (!empty($entity->uid)) {
      $data[$entity_type] = $entity;
    }

    $destination = file_field_widget_uri($info, $instance_info, $data);
  }

  // Populate entity.
  $field = isset($entity->$field_name) ? $entity->$field_name : array(LANGUAGE_NONE => array());
  $delta = 0;
  foreach ($values as $v) {
    if ($info['cardinality'] == $delta) {
      break;
    }

    if (!isset($field[LANGUAGE_NONE][$delta])) {
      $field[LANGUAGE_NONE][$delta] = array();
    }

    switch ($sub_field) {
      case 'alt':
      case 'title':
        $field[LANGUAGE_NONE][$delta][$sub_field] = $v;
        break;

      case 'uri':
        if ($v) {
          try {
            $file = remote_stream_wrapper_file_load_by_uri($v);
            if (!$file) {
              $file = remote_stream_wrapper_file_create_by_uri($v);
              $file->status = FALSE;
              file_save($file);
            }
            $field[LANGUAGE_NONE][$delta] += (array) $file;
            // @todo: Figure out how to properly populate this field.
            $field[LANGUAGE_NONE][$delta]['display'] = 1;
          }
          catch (Exception $e) {
            watchdog_exception('Feeds', $e, nl2br(check_plain($e)));
          }
        }
        break;
    }

    $delta++;
  }

  $entity->$field_name = $field;
}

t_d_d’s picture

Hi, i have same problem with images in image field - when i create node manualy, images are remote, but when i import them with feeds, they are saved to local folder.

It looks like module above is only for file field. Is there any way to make it work for image fields?