6 * On behalf implementation of Feeds mapping API for file.module and
9 * Does actually not include mappers for field types defined in fields module
10 * (because there aren't any) but mappers for all fields that contain their
11 * value simply in $entity->fieldname['und'][$i]['value'].
15 * Implements hook_feeds_processor_targets_alter().
17 * @see FeedsNodeProcessor::getMappingTargets().
19 function file_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
20 foreach (field_info_instances($entity_type, $bundle_name) as
$name => $instance) {
21 $info = field_info_field($name);
23 if (in_array($info['type'], array('file', 'image'))) {
24 $targets[$name] = array(
25 'name' => $instance['label'],
26 'callback' => 'file_feeds_set_target',
27 'description' => t('The @label field of the node.', array('@label' => $instance['label'])),
34 * Callback for mapping. Here is where the actual mapping happens.
36 * When the callback is invoked, $target contains the name of the field the
37 * user has decided to map to and $value contains the value of the feed item
38 * element the user has picked as a source.
40 function file_feeds_set_target($entity, $target, $value) {
44 module_load_include('inc', 'file');
46 // Make sure $value is an array of objects of type FeedsEnclosure.
47 if (!is_array($value)) {
48 $value = array($value);
50 foreach ($value as
$k => $v) {
51 if (!($v instanceof FeedsEnclosure
)) {
53 $value[$k] = new
FeedsEnclosure($v, 'application/octet-stream');
64 // Determine file destination.
65 // @todo This needs review and debugging.
66 $info = field_info_field($target);
67 $bundle_name = $entity->entity_type
== 'node' ?
$entity->type
: $entity->entity_type
;
68 $instance_info = field_info_instance($entity->entity_type
, $target, $bundle_name);
69 $account = !empty($entity->uid
) ?
user_load($entity->uid
) : NULL
;
70 $destination = file_field_widget_uri($info, $instance_info, $account);
74 $field = isset($entity->$target) ?
$entity->$target : array();
75 foreach ($value as
$v) {
76 if ($file = $v->getFile($destination)) {
77 $field['und'][$i] = (array)$file;
78 $field['und'][$i]['display'] = 1; // @todo: Figure out how to properly populate this field.
79 if ($info['cardinality'] == 1) {
85 $entity->{$target} = $field;