1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* On behalf implementation of Feeds mapping API for file.module and
|
6
|
* image.module.
|
7
|
*
|
8
|
* Does actually not include mappers for field types defined in fields module
|
9
|
* (because there aren't any) but mappers for all fields that contain their
|
10
|
* value simply in $entity->fieldname['und'][$i]['value'].
|
11
|
*/
|
12
|
|
13
|
/**
|
14
|
* Implements hook_feeds_processor_targets_alter().
|
15
|
*
|
16
|
* @see FeedsNodeProcessor::getMappingTargets().
|
17
|
*/
|
18
|
function file_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
|
19
|
foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
|
20
|
$info = field_info_field($name);
|
21
|
|
22
|
if (in_array($info['type'], array('file', 'image'))) {
|
23
|
$targets[$name] = array(
|
24
|
'name' => check_plain($instance['label']),
|
25
|
'callback' => 'file_feeds_set_target',
|
26
|
'description' => t('The @label field of the node.', array('@label' => $instance['label'])),
|
27
|
);
|
28
|
}
|
29
|
}
|
30
|
}
|
31
|
|
32
|
/**
|
33
|
* Callback for mapping. Here is where the actual mapping happens.
|
34
|
*
|
35
|
* When the callback is invoked, $target contains the name of the field the
|
36
|
* user has decided to map to and $value contains the value of the feed item
|
37
|
* element the user has picked as a source.
|
38
|
*/
|
39
|
function file_feeds_set_target($source, $entity, $target, $value) {
|
40
|
if (empty($value)) {
|
41
|
return;
|
42
|
}
|
43
|
module_load_include('inc', 'file');
|
44
|
|
45
|
// Make sure $value is an array of objects of type FeedsEnclosure.
|
46
|
if (!is_array($value)) {
|
47
|
$value = array($value);
|
48
|
}
|
49
|
foreach ($value as $k => $v) {
|
50
|
if (!($v instanceof FeedsEnclosure)) {
|
51
|
if (is_string($v)) {
|
52
|
$value[$k] = new FeedsEnclosure($v, file_get_mimetype($v));
|
53
|
}
|
54
|
else {
|
55
|
unset($value[$k]);
|
56
|
}
|
57
|
}
|
58
|
}
|
59
|
if (empty($value)) {
|
60
|
return;
|
61
|
}
|
62
|
|
63
|
$entity_type = $source->importer->processor->entityType();
|
64
|
|
65
|
// Determine file destination.
|
66
|
// @todo This needs review and debugging.
|
67
|
$instance_info = field_info_instance($entity_type, $target, $source->importer->processor->bundle());
|
68
|
$info = field_info_field($target);
|
69
|
$data = array();
|
70
|
if (!empty($entity->uid)) {
|
71
|
$data[$entity_type] = $entity;
|
72
|
}
|
73
|
$destination = file_field_widget_uri($info, $instance_info, $data);
|
74
|
|
75
|
// Populate entity.
|
76
|
$i = 0;
|
77
|
$field = isset($entity->$target) ? $entity->$target : array();
|
78
|
foreach ($value as $v) {
|
79
|
try {
|
80
|
$file = $v->getFile($destination);
|
81
|
}
|
82
|
catch (Exception $e) {
|
83
|
watchdog_exception('Feeds', $e, nl2br(check_plain($e)));
|
84
|
}
|
85
|
if ($file) {
|
86
|
$field['und'][$i] = (array)$file;
|
87
|
$field['und'][$i]['display'] = 1; // @todo: Figure out how to properly populate this field.
|
88
|
if ($info['cardinality'] == 1) {
|
89
|
break;
|
90
|
}
|
91
|
$i++;
|
92
|
}
|
93
|
}
|
94
|
$entity->{$target} = $field;
|
95
|
}
|