Merge in changes from http://github.com/lxbarth/Feeds/commits/DRUPAL-7--1-0-alpha1.
[project/feeds.git] / mappers / field.inc
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * On behalf implementation of Feeds mapping API for field.module.
7 *
8 * Does actually not include mappers for field types defined in fields module
9 * (because there aren't any) but mappers for all fields that contain their
10 * value simply in $entity->fieldname['und'][$i]['value'].
11 */
12
13 /**
14 * Implements hook_feeds_processor_targets_alter().
15 *
16 * @see FeedsNodeProcessor::getMappingTargets().
17 */
18 function field_feeds_processor_targets_alter(&$targets, $entity_type, $content_type) {
19 foreach (field_info_instances($entity_type, $content_type) as $name => $instance) {
20 $info = field_info_field($name);
21 $allowed_types = array(
22 'number_integer',
23 'number_decimal',
24 'number_float',
25 'text',
26 'text_long',
27 'text_with_summary',
28 );
29 if (in_array($info['type'], $allowed_types)) {
30 $targets[$name] = array(
31 'name' => $instance['label'],
32 'callback' => 'field_feeds_set_target',
33 'description' => t('The @label field of the node.', array('@label' => $instance['label'])),
34 );
35 }
36 }
37 }
38
39 /**
40 * Callback for mapping. Here is where the actual mapping happens.
41 *
42 * When the callback is invoked, $target contains the name of the field the
43 * user has decided to map to and $value contains the value of the feed item
44 * element the user has picked as a source.
45 */
46 function field_feeds_set_target($entity, $target, $value) {
47 if (empty($value)) {
48 return;
49 }
50
51 // Handle non-multiple value fields.
52 if (!is_array($value)) {
53 $value = array($value);
54 }
55
56 $info = field_info_field($target);
57
58 // Iterate over all values.
59 $i = 0;
60 $field = isset($entity->$target) ? $entity->$target : array();
61 foreach ($value as $v) {
62 if (!is_array($v) && !is_object($v)) {
63 $field['und'][$i]['value'] = $v;
64 }
65 if ($info['cardinality'] == 1) {
66 break;
67 }
68 $i++;
69 }
70 $entity->{$target} = $field;
71 }