Commit | Line | Data |
---|---|---|
7f254ad8 AE |
1 | <?php |
2 | /** | |
3 | * @file | |
4 | * Integration with the Feeds module. | |
5 | */ | |
6 | ||
7 | /** | |
8 | * Implements hook_feeds_processor_targets_alter(). | |
9 | * | |
10 | * @see FeedsNodeProcessor::getMappingTargets(). | |
11 | */ | |
12 | function civicrm_contact_ref_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) { | |
13 | foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) { | |
14 | $info = field_info_field($name); | |
15 | if (in_array($info['type'], array('civicrm_contact_ref_contact'))) { | |
16 | $targets[$name] = array( | |
17 | 'name' => $instance['label'], | |
18 | 'callback' => 'civicrm_contact_ref_feeds_set_target', | |
19 | 'description' => t('The @label field of the node.', array('@label' => $instance['label'])), | |
20 | 'optional_unique' => TRUE, | |
21 | ); | |
22 | } | |
23 | } | |
24 | } | |
25 | ||
26 | /** | |
27 | * Callback function for mapping CiviCRM Contact Ref field. | |
28 | * | |
29 | * This function is invoked via hook_feeds_processor_targets_alter(). | |
30 | * Here is where the actual mapping happens. | |
31 | * | |
32 | * @param $target | |
33 | * the name of the field the user has decided to map to. | |
34 | * @param $value | |
35 | * the value of the feed item element the user has picked as a source. | |
36 | */ | |
37 | function civicrm_contact_ref_feeds_set_target($source, $entity, $target, $value) { | |
38 | $value = is_array($value) ? $value : array($value); | |
39 | ||
40 | $info = field_info_field($target); | |
41 | ||
42 | // Iterate over all values. | |
43 | ||
44 | $field = isset($entity->$target) ? $entity->$target : array(); | |
45 | // Allow for multiple mappings to the same target. | |
46 | $delta = 0; | |
47 | foreach ($value as $v) { | |
48 | if ($info['cardinality'] == $delta) { | |
49 | break; | |
50 | } | |
51 | ||
52 | if (is_object($v) && ($v instanceof FeedsElement)) { | |
53 | $v = $v->getValue(); | |
54 | } | |
55 | ||
56 | if (is_numeric($v)) { | |
57 | $field['und'][$delta]['contact_id'] = $v; | |
58 | $delta++; | |
59 | } | |
60 | } | |
61 | ||
62 | $entity->{$target} = $field; | |
63 | } |