Projet

Général

Profil

Paste
Télécharger (1,73 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / mappers / number.inc @ 76df55b7

1
<?php
2

    
3
/**
4
 * @file
5
 * On behalf implementation of Feeds mapping API for number.module.
6
 */
7

    
8
/**
9
 * Implements hook_feeds_processor_targets_alter().
10
 *
11
 * @see FeedsProcessor::getMappingTargets()
12
 */
13
function number_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
14
  $numeric_types = array(
15
    'list_integer',
16
    'list_float',
17
    'list_boolean',
18
    'number_integer',
19
    'number_decimal',
20
    'number_float',
21
  );
22
  foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
23
    $info = field_info_field($name);
24

    
25
    if (in_array($info['type'], $numeric_types)) {
26
      $targets[$name] = array(
27
        'name' => check_plain($instance['label']),
28
        'callback' => 'number_feeds_set_target',
29
        'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
30
      );
31
    }
32
  }
33
}
34

    
35
/**
36
 * Callback for mapping numerics.
37
 *
38
 * Ensure that $value is a numeric to avoid database errors.
39
 */
40
function number_feeds_set_target($source, $entity, $target, $value) {
41

    
42
  // Do not perform the regular empty() check here. 0 is a valid value. That's
43
  // really just a performance thing anyway.
44

    
45
  if (!is_array($value)) {
46
    $value = array($value);
47
  }
48

    
49
  $info = field_info_field($target);
50

    
51
  // Iterate over all values.
52
  $field = isset($entity->$target) ? $entity->$target : array('und' => array());
53

    
54
  // Allow for multiple mappings to the same target.
55
  $delta = count($field['und']);
56

    
57
  foreach ($value as $v) {
58

    
59
    if ($info['cardinality'] == $delta) {
60
      break;
61
    }
62

    
63
    if (is_object($v) && ($v instanceof FeedsElement)) {
64
      $v = $v->getValue();
65
    }
66

    
67
    if (is_numeric($v)) {
68
      $field['und'][$delta]['value'] = $v;
69
      $delta++;
70
    }
71
  }
72

    
73
  $entity->$target = $field;
74
}