Projet

Général

Profil

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

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

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_alter().
10
 *
11
 * @see FeedsProcessor::getMappingTargets()
12
 */
13
function link_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
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

    
37
/**
38
 * Callback for mapping. Here is where the actual mapping happens.
39
 *
40
 * When the callback is invoked, $target contains the name of the field the
41
 * user has decided to map to and $value contains the value of the feed item
42
 * element the user has picked as a source.
43
 */
44
function link_feeds_set_target($source, $entity, $target, $value) {
45
  if (empty($value)) {
46
    return;
47
  }
48

    
49
  // Handle non-multiple value fields.
50
  if (!is_array($value)) {
51
    $value = array($value);
52
  }
53

    
54
  // Iterate over all values.
55
  list($field_name, $column) = explode(':', $target);
56
  $info = field_info_field($field_name);
57

    
58
  $field = isset($entity->$field_name) ? $entity->$field_name : array();
59
  $delta = 0;
60

    
61
  foreach ($value as $v) {
62
    if ($info['cardinality'] == $delta) {
63
      break;
64
    }
65

    
66
    if (is_object($v) && ($v instanceof FeedsElement)) {
67
      $v = $v->getValue();
68
    }
69

    
70
    if (is_scalar($v)) {
71
      $field['und'][$delta][$column] = $v;
72
      $delta++;
73
    }
74
  }
75
  $entity->$field_name = $field;
76
}