1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Feeds mapping implementation for the Entity reference module
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_feeds_processor_targets_alter().
|
10
|
*
|
11
|
* @see FeedsNodeProcessor::getMappingTargets().
|
12
|
*/
|
13
|
function entityreference_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
|
14
|
|
15
|
foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
|
16
|
$info = field_info_field($name);
|
17
|
if ($info['type'] == 'entityreference') {
|
18
|
// We don't use ":guid" in key, not to break existing configurations.
|
19
|
$targets[$name] = array(
|
20
|
'name' => check_plain($instance['label'] . t(' (Entity reference by Feeds GUID)')),
|
21
|
'callback' => 'entityreference_feeds_set_target',
|
22
|
'description' => t('The field instance @label of @id matched by Feeds GUID.', array(
|
23
|
'@label' => $instance['label'],
|
24
|
'@id' => $name,
|
25
|
)),
|
26
|
);
|
27
|
$targets[$name . ':url'] = array(
|
28
|
'name' => check_plain($instance['label'] . t(' (Entity reference by Feeds URL)')),
|
29
|
'callback' => 'entityreference_feeds_set_target',
|
30
|
'description' => t('The field instance @label of @id matched by Feeds URL.', array(
|
31
|
'@label' => $instance['label'],
|
32
|
'@id' => $name,
|
33
|
)),
|
34
|
'real_target' => $name,
|
35
|
);
|
36
|
$targets[$name . ':etid'] = array(
|
37
|
'name' => check_plain($instance['label'] . t(' (Entity reference by Entity ID)')),
|
38
|
'callback' => 'entityreference_feeds_set_target',
|
39
|
'description' => t('The field instance @label of @id matched by Entity ID.', array(
|
40
|
'@label' => $instance['label'],
|
41
|
'@id' => $name,
|
42
|
)),
|
43
|
'real_target' => $name,
|
44
|
);
|
45
|
$targets[$name . ':label'] = array(
|
46
|
'name' => check_plain($instance['label'] . t(' (Entity reference by Entity label)')),
|
47
|
'callback' => 'entityreference_feeds_set_target',
|
48
|
'description' => t('The field instance @label of @id matched by Entity label.', array(
|
49
|
'@label' => $instance['label'],
|
50
|
'@id' => $name,
|
51
|
)),
|
52
|
'real_target' => $name,
|
53
|
);
|
54
|
}
|
55
|
}
|
56
|
}
|
57
|
|
58
|
/**
|
59
|
* Entity reference callback for mapping.
|
60
|
*
|
61
|
* When the callback is invoked, $target contains the name of the field the
|
62
|
* user has decided to map to and $value contains the value of the feed item
|
63
|
* element the user has picked as a source.
|
64
|
*
|
65
|
* @param $source
|
66
|
* A FeedsSource object.
|
67
|
* @param $entity
|
68
|
* The entity to map to.
|
69
|
* @param $target
|
70
|
* The target key on $entity to map to.
|
71
|
* @param $value
|
72
|
* The value to map. MUST be an array.
|
73
|
*/
|
74
|
function entityreference_feeds_set_target($source, $entity, $target, $value) {
|
75
|
|
76
|
// Don't do anything if we weren't given any data.
|
77
|
if (empty($value)) {
|
78
|
return;
|
79
|
}
|
80
|
|
81
|
// Assume that the passed in value could really be any number of values.
|
82
|
if (is_array($value)) {
|
83
|
$values = $value;
|
84
|
}
|
85
|
else {
|
86
|
$values = array($value);
|
87
|
}
|
88
|
|
89
|
// Determine the field we are matching against.
|
90
|
if (strpos($target, ':') === FALSE) {
|
91
|
$match_key = 'guid';
|
92
|
}
|
93
|
else {
|
94
|
list($target, $match_key) = explode(':', $target, 2);
|
95
|
}
|
96
|
|
97
|
// Get some useful field information.
|
98
|
$info = field_info_field($target);
|
99
|
if ($match_key == 'label') {
|
100
|
$handler = entityreference_get_selection_handler($info);
|
101
|
}
|
102
|
|
103
|
// Set the language of the field depending on the mapping.
|
104
|
$language = isset($mapping['language']) ? $mapping['language'] : LANGUAGE_NONE;
|
105
|
|
106
|
// Iterate over all values.
|
107
|
$iterator = 0;
|
108
|
$field = isset($entity->$target) ? $entity->$target : array();
|
109
|
foreach ($values as $value) {
|
110
|
|
111
|
// Only process if this value was set for this instance.
|
112
|
if ($value) {
|
113
|
switch ($match_key) {
|
114
|
case 'guid':
|
115
|
case 'url':
|
116
|
// Fetch the entity ID resulting from the mapping table look-up.
|
117
|
$entity_id = db_select('feeds_item', 'fi')
|
118
|
->fields('fi', array('entity_id'))
|
119
|
->condition($match_key, $value,'=')
|
120
|
->execute()
|
121
|
->fetchField();
|
122
|
break;
|
123
|
case 'etid':
|
124
|
$entity_id = $value;
|
125
|
break;
|
126
|
case 'label':
|
127
|
$options = $handler->getReferencableEntities($value, '=');
|
128
|
$options = reset($options);
|
129
|
$etids = array_keys($options);
|
130
|
// Use the first matching entity.
|
131
|
$entity_id = reset($etids);
|
132
|
break;
|
133
|
}
|
134
|
/*
|
135
|
* Only add a reference to an existing entity ID if there exists a
|
136
|
* mapping between it and the provided GUID. In cases where no such
|
137
|
* mapping exists (yet), don't do anything here. There may be a mapping
|
138
|
* defined later in the CSV file. If so, and the user re-runs the import
|
139
|
* (as a second pass), we can add this reference then. (The "Update
|
140
|
* existing nodes" option must be selected during the second pass.)
|
141
|
*/
|
142
|
if ($entity_id) {
|
143
|
|
144
|
// Assign the target ID.
|
145
|
$field[$language][$iterator]['target_id'] = $entity_id;
|
146
|
}
|
147
|
else /* there is no $entity_id, no mapping */ {
|
148
|
|
149
|
/*
|
150
|
* Feeds stores a hash of every line imported from CSVs in order to
|
151
|
* make the import process more efficient by ignoring lines it's
|
152
|
* already seen. We need to short-circuit this process in this case
|
153
|
* because users may want to re-import the same line as an update later
|
154
|
* when (and if) a map to a reference exists. So in order to provide
|
155
|
* this opportunity later, we need to destroy the hash.
|
156
|
*/
|
157
|
unset($entity->feeds_item->hash);
|
158
|
$source->log('entityreference', t('No existing entity found for entity @source_id entityreference to source entity @value', array('@source_id' => $entity->feeds_item->entity_id, '@value' => $value)));
|
159
|
}
|
160
|
}
|
161
|
|
162
|
// Break out of the loop if this field is single-valued.
|
163
|
if ($info['cardinality'] == 1) {
|
164
|
break;
|
165
|
}
|
166
|
$iterator++;
|
167
|
}
|
168
|
|
169
|
// Add the field to the entity definition.
|
170
|
$entity->{$target} = $field;
|
171
|
}
|