Projet

Général

Profil

Paste
Télécharger (2,04 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / mappers / list.inc @ a192dc0b

1
<?php
2

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

    
8
/**
9
 * Implements hook_feeds_processor_targets().
10
 */
11
function list_feeds_processor_targets($entity_type, $bundle_name) {
12
  $targets = array();
13

    
14
  foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
15
    $info = field_info_field($name);
16

    
17
    switch ($info['type']) {
18

    
19
      case 'list_integer':
20
      case 'list_float':
21
        $targets[$name] = array(
22
          'name' => check_plain($instance['label']),
23
          'callback' => 'number_feeds_set_target',
24
          'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
25
        );
26
        break;
27

    
28
      case 'list_boolean':
29
        $targets[$name] = array(
30
          'name' => check_plain($instance['label']),
31
          'callback' => 'list_feeds_set_boolean_target',
32
          'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
33
        );
34
        break;
35

    
36
      case 'list_text':
37
        $targets[$name] = array(
38
          'name' => check_plain($instance['label']),
39
          'callback' => 'text_feeds_set_target',
40
          'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
41
        );
42
        break;
43
    }
44
  }
45

    
46
  return $targets;
47
}
48

    
49
/**
50
 * Callback for setting list_boolean fields.
51
 */
52
function list_feeds_set_boolean_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
53
  $language = $mapping['language'];
54

    
55
  $field = isset($entity->$target) ? $entity->$target : array($language => array());
56

    
57
  foreach ($values as $value) {
58

    
59
    if (is_object($value) && ($value instanceof FeedsElement)) {
60
      $value = $value->getValue();
61
    }
62

    
63
    if (is_string($value) && strlen($value) == 0) {
64
      // Don't convert an empty string to a boolean.
65
      continue;
66
    }
67
    if (is_null($value)) {
68
      // Don't convert a NULL value to a boolean.
69
      continue;
70
    }
71

    
72
    $field[$language][] = array('value' => (int) (bool) $value);
73
  }
74

    
75
  $entity->$target = $field;
76
}