Projet

Général

Profil

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

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

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) {
43
  list($field_name, $column) = explode(':', $target);
44

    
45
  $field = isset($entity->$field_name) ? $entity->$field_name : array('und' => array());
46
  $delta = 0;
47

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

    
53
    if (is_scalar($value)) {
54
      $field['und'][$delta][$column] = (string) $value;
55
    }
56
    $delta++;
57
  }
58

    
59
  $entity->$field_name = $field;
60
}