You are here

function filefield_feeds_set_target in Feeds 6

Implementation of hook_feeds_set_target().

@todo: should we support $object->url again?

Parameters

$node: The target node.

$field_name: The name of field on the target node to map to.

$value: The value to be mapped. Should contain a URL or an array of URLs; a FeedsEnclosure or an array of FeedsEnclosures.

1 string reference to 'filefield_feeds_set_target'
filefield_feeds_node_processor_targets_alter in mappers/filefield.inc
Implementation of hook_feeds_node_processor_targets_alter()

File

mappers/filefield.inc, line 44
On behalf implementation of Feeds mapping API for filefield.module (CCK).

Code

function filefield_feeds_set_target($node, $field_name, $value) {

  // Normalize $value, create an array of FeedsEnclosures of it.
  $enclosures = array();
  if (!is_array($value)) {
    $value = array(
      $value,
    );
  }
  foreach ($value as $k => $v) {
    if ($v instanceof FeedsEnclosure) {
      $enclosures[] = $v;
    }
    elseif (valid_url($v)) {
      $enclosures[$k] = new FeedsEnclosure($v, 'application/octet-stream');
    }
  }

  // Map enclosures.
  $items = isset($node->{$field_name}) ? $node->{$field_name} : array();
  foreach ($enclosures as $enclosure) {
    if ($file = $enclosure
      ->getFile()) {
      $field = content_fields($field_name, $node->type);
      $target_dir = filefield_widget_file_path($field, user_load($node->uid));
      $info = field_file_save_file($enclosure
        ->getFile(), array(), $target_dir);
      if ($info) {
        $info['list'] = array();
        $info['data'] = array(
          'description' => '',
        );
        if ($field['list_field']) {
          $info['list'] = $field['list_default'];
        }
        $items[] = $info;
        $error = FALSE;
      }
    }
  }
  $node->{$field_name} = $items;
}