Projet

Général

Profil

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

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

1
<?php
2

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

    
8
/**
9
 * Implements hook_feeds_processor_targets().
10
 */
11
function link_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
    if ($info['type'] == 'link_field') {
17
      if (array_key_exists('url', $info['columns'])) {
18
        $targets[$name . ':url'] = array(
19
          'name' => t('@name: URL', array('@name' => $instance['label'])),
20
          'callback' => 'link_feeds_set_target',
21
          'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
22
          'real_target' => $name,
23
        );
24
      }
25
      if (array_key_exists('title', $info['columns'])) {
26
        $targets[$name . ':title'] = array(
27
          'name' => t('@name: Title', array('@name' => $instance['label'])),
28
          'callback' => 'link_feeds_set_target',
29
          'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
30
          'real_target' => $name,
31
        );
32
      }
33
    }
34
  }
35

    
36
  return $targets;
37
}
38

    
39
/**
40
 * Callback for mapping link fields.
41
 */
42
function link_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
43
  $language = $mapping['language'];
44

    
45
  list($field_name, $column) = explode(':', $target);
46

    
47
  $field = isset($entity->$field_name) ? $entity->$field_name : array($language => array());
48
  $delta = 0;
49

    
50
  foreach ($values as $value) {
51
    if (is_object($value) && ($value instanceof FeedsElement)) {
52
      $value = $value->getValue();
53
    }
54

    
55
    if (is_scalar($value)) {
56
      $field[$language][$delta][$column] = (string) $value;
57
    }
58
    $delta++;
59
  }
60

    
61
  $entity->$field_name = $field;
62
}