-
Notifications
You must be signed in to change notification settings - Fork 1
/
hms_field.feeds.inc
91 lines (78 loc) · 2.4 KB
/
hms_field.feeds.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/**
* @file
* Feeds mapping implementation for the hms_feeds module.
*/
/**
* Implements hook_feeds_processor_targets_alter().
*
* @see FeedsNodeProcessor::getMappingTargets().
*/
function hms_field_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 ($info['type'] == 'hms_field') {
$targets[$name] = array(
'name' => check_plain($instance['label']),
'callback' => 'hms_field_feeds_set_target',
'description' => t('The field instance @label of @id', array(
'@label' => $instance['label'],
'@id' => $name,
)),
);
}
}
}
/**
* HMS field callback for mapping.
*
* 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.
*
* @param $source
* A FeedsSource object.
* @param $entity
* The entity to map to.
* @param $target
* The target key on $entity to map to.
* @param $value
* The value to map. MUST be an array.
* @param $mapping
* Array of mapping settings for current value.
* @param $input_format
* TRUE if an input format should be applied.
*/
function hms_field_feeds_set_target($source, $entity, $target, $value, $mapping, $input_format = FALSE) {
// Don't do anything if we weren't given any data.
if (empty($value)) {
return;
}
// Assume that the passed in value could really be any number of values.
if (is_array($value)) {
$values = $value;
}
else {
$values = array($value);
}
// Get some useful field information.
$info = field_info_field($target);
// Set the language of the field depending on the mapping.
$language = isset($mapping['language']) ? $mapping['language'] : LANGUAGE_NONE;
// Iterate over all values.
$iterator = 0;
$field = isset($entity->$target) ? $entity->$target : array();
foreach ($values as $value) {
// Only process if this value was set for this instance.
if ($value) {
$field[$language][$iterator]['value'] = $value;
}
// Break out of the loop if this field is single-valued.
if ($info['cardinality'] == 1) {
break;
}
$iterator++;
}
// Add the field to the entity definition.
$entity->{$target} = $field;
}