1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Integration with the Feeds module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_feeds_processor_targets_alter().
|
10
|
*/
|
11
|
function addressfield_feeds_processor_targets_alter(&$targets, $entity_type, $bundle) {
|
12
|
foreach (field_info_instances($entity_type, $bundle) as $name => $instance) {
|
13
|
$info = field_info_field($name);
|
14
|
if ($info['type'] == 'addressfield') {
|
15
|
foreach ($info['columns'] as $sub_field => $schema_info) {
|
16
|
$name_label = $instance['label'] . ': ' . drupal_ucfirst(str_replace('_', ' ', $sub_field));
|
17
|
$targets[$name . ':' . $sub_field] = array(
|
18
|
'name' => $name_label,
|
19
|
'callback' => 'addressfield_set_target',
|
20
|
'real_target' => $info['field_name'],
|
21
|
'description' => $schema_info['description'],
|
22
|
);
|
23
|
}
|
24
|
}
|
25
|
}
|
26
|
}
|
27
|
|
28
|
/**
|
29
|
* Callback for hook_feeds_processor_targets_alter().
|
30
|
*
|
31
|
* @param $source
|
32
|
* Field mapper source settings.
|
33
|
* @param $entity
|
34
|
* An entity object, for instance a node object.
|
35
|
* @param $target
|
36
|
* A string identifying the target on the node.
|
37
|
* @param $values
|
38
|
* The value to populate the target with.
|
39
|
* @param array $mapping
|
40
|
* Associative array of the mapping settings from the per mapping
|
41
|
* configuration form.
|
42
|
*/
|
43
|
function addressfield_set_target($source, $entity, $target, $values, $mapping) {
|
44
|
$language = $mapping['language'];
|
45
|
list($field_name, $sub_field) = explode(':', $target, 2);
|
46
|
|
47
|
// Field info and instance are required for setting default values.
|
48
|
$entity_type = $source->importer->processor->entityType();
|
49
|
list(, , $bundle_name) = entity_extract_ids($entity_type, $entity);
|
50
|
$info = field_info_field($field_name);
|
51
|
$instance = field_info_instance($entity_type, $field_name, $bundle_name);
|
52
|
|
53
|
// Convert the values into an array if it isn't one already to correspond to
|
54
|
// Drupal's handling of field value arrays.
|
55
|
if (!is_array($values)) {
|
56
|
$values = array($values);
|
57
|
}
|
58
|
|
59
|
// If the field is already set on the given entity, update the existing value
|
60
|
// array. Otherwise start with a fresh field value array.
|
61
|
$field = isset($entity->{$field_name}) ? $entity->{$field_name} : array();
|
62
|
|
63
|
// Loop over the field values array...
|
64
|
foreach ($values as $delta => $value) {
|
65
|
// Set defaults for new values.
|
66
|
if (!isset($field[$language][$delta])) {
|
67
|
$field[$language][$delta] = addressfield_default_values($info, $instance);
|
68
|
}
|
69
|
$field[$language][$delta][$sub_field] = $value;
|
70
|
}
|
71
|
|
72
|
$entity->{$field_name} = $field;
|
73
|
}
|