1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Integration with the Feeds module.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Implements hook_feeds_node_processor_targets_alter().
|
10 |
|
|
*/
|
11 |
|
|
function addressfield_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
|
12 |
|
|
foreach (field_info_instances($entity_type, $bundle_name) 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 $value
|
38 |
|
|
* The value to populate the target with.
|
39 |
|
|
*/
|
40 |
|
|
function addressfield_set_target($source, $entity, $target, $values) {
|
41 |
|
|
list($field_name, $sub_field) = explode(':', $target, 2);
|
42 |
|
|
|
43 |
|
|
// Convert the values into an array if it isn't one already to correspond to
|
44 |
|
|
// Drupal's handling of field value arrays.
|
45 |
|
|
if (!is_array($values)) {
|
46 |
|
|
$values = array($values);
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
// If the field is already set on the given entity, update the existing value
|
50 |
|
|
// array. Otherwise start with a fresh field value array.
|
51 |
|
|
$field = isset($entity->$field_name) ? $entity->$field_name : array();
|
52 |
|
|
|
53 |
|
|
// Loop over the field values array...
|
54 |
|
|
foreach ($values as $delta => $value) {
|
55 |
|
|
$field[LANGUAGE_NONE][$delta][$sub_field] = $value;
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
$entity->$field_name = $field;
|
59 |
|
|
} |