1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Integration with the Feeds module.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
fd5a8e62
|
Assos Assos
|
* Implements hook_feeds_processor_targets_alter().
|
10 |
85ad3d82
|
Assos Assos
|
*/
|
11 |
fd5a8e62
|
Assos Assos
|
function addressfield_feeds_processor_targets_alter(&$targets, $entity_type, $bundle) {
|
12 |
|
|
foreach (field_info_instances($entity_type, $bundle) as $name => $instance) {
|
13 |
85ad3d82
|
Assos Assos
|
$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 |
fd5a8e62
|
Assos Assos
|
* @param $values
|
38 |
85ad3d82
|
Assos Assos
|
* The value to populate the target with.
|
39 |
d51f9c7d
|
Assos Assos
|
* @param array $mapping
|
40 |
|
|
* Associative array of the mapping settings from the per mapping
|
41 |
|
|
* configuration form.
|
42 |
85ad3d82
|
Assos Assos
|
*/
|
43 |
d51f9c7d
|
Assos Assos
|
function addressfield_set_target($source, $entity, $target, $values, $mapping) {
|
44 |
|
|
$language = $mapping['language'];
|
45 |
85ad3d82
|
Assos Assos
|
list($field_name, $sub_field) = explode(':', $target, 2);
|
46 |
|
|
|
47 |
d51f9c7d
|
Assos Assos
|
// 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 |
85ad3d82
|
Assos Assos
|
// 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 |
d51f9c7d
|
Assos Assos
|
$field = isset($entity->{$field_name}) ? $entity->{$field_name} : array();
|
62 |
85ad3d82
|
Assos Assos
|
|
63 |
|
|
// Loop over the field values array...
|
64 |
|
|
foreach ($values as $delta => $value) {
|
65 |
d51f9c7d
|
Assos Assos
|
// 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 |
85ad3d82
|
Assos Assos
|
}
|
71 |
|
|
|
72 |
d51f9c7d
|
Assos Assos
|
$entity->{$field_name} = $field;
|
73 |
85ad3d82
|
Assos Assos
|
} |