Projet

Général

Profil

Paste
Télécharger (6,01 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / entityreference / entityreference.feeds.inc @ 59ae487e

1
<?php
2

    
3
/**
4
 * @file
5
 * Feeds mapping implementation for the Entity reference module
6
 */
7

    
8
/**
9
 * Implements hook_feeds_processor_targets_alter().
10
 *
11
 * @see FeedsNodeProcessor::getMappingTargets().
12
 */
13
function entityreference_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
14

    
15
  foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
16
    $info = field_info_field($name);
17
    if ($info['type'] == 'entityreference') {
18
      // We don't use ":guid" in key, not to break existing configurations.
19
      $targets[$name] = array(
20
        'name'        => check_plain($instance['label'] . t(' (Entity reference by Feeds GUID)')),
21
        'callback'    => 'entityreference_feeds_set_target',
22
        'description' => t('The field instance @label of @id matched by Feeds GUID.', array(
23
          '@label' => $instance['label'],
24
          '@id'    => $name,
25
        )),
26
      );
27
      $targets[$name . ':url'] = array(
28
        'name'        => check_plain($instance['label'] . t(' (Entity reference by Feeds URL)')),
29
        'callback'    => 'entityreference_feeds_set_target',
30
        'description' => t('The field instance @label of @id matched by Feeds URL.', array(
31
          '@label' => $instance['label'],
32
          '@id'    => $name,
33
        )),
34
        'real_target' => $name,
35
      );
36
      $targets[$name . ':etid'] = array(
37
        'name'        => check_plain($instance['label'] . t(' (Entity reference by Entity ID)')),
38
        'callback'    => 'entityreference_feeds_set_target',
39
        'description' => t('The field instance @label of @id matched by Entity ID.', array(
40
          '@label' => $instance['label'],
41
          '@id'    => $name,
42
        )),
43
        'real_target' => $name,
44
      );
45
      $targets[$name . ':label'] = array(
46
        'name'        => check_plain($instance['label'] . t(' (Entity reference by Entity label)')),
47
        'callback'    => 'entityreference_feeds_set_target',
48
        'description' => t('The field instance @label of @id matched by Entity label.', array(
49
          '@label' => $instance['label'],
50
          '@id'    => $name,
51
        )),
52
        'real_target' => $name,
53
      );
54
    }
55
  }
56
}
57

    
58
/**
59
 * Entity reference callback for mapping.
60
 *
61
 * When the callback is invoked, $target contains the name of the field the
62
 * user has decided to map to and $value contains the value of the feed item
63
 * element the user has picked as a source.
64
 *
65
 * @param $source
66
 *   A FeedsSource object.
67
 * @param $entity
68
 *   The entity to map to.
69
 * @param $target
70
 *   The target key on $entity to map to.
71
 * @param $value
72
 *   The value to map. MUST be an array.
73
 */
74
function entityreference_feeds_set_target($source, $entity, $target, $value) {
75

    
76
  // Don't do anything if we weren't given any data.
77
  if (empty($value)) {
78
    return;
79
  }
80

    
81
  // Assume that the passed in value could really be any number of values.
82
  if (is_array($value)) {
83
    $values = $value;
84
  }
85
  else {
86
    $values = array($value);
87
  }
88

    
89
  // Determine the field we are matching against.
90
  if (strpos($target, ':') === FALSE) {
91
    $match_key = 'guid';
92
  }
93
  else {
94
    list($target, $match_key) = explode(':', $target, 2);
95
  }
96

    
97
  // Get some useful field information.
98
  $info = field_info_field($target);
99
  if ($match_key == 'label') {
100
    $handler = entityreference_get_selection_handler($info);
101
  }
102

    
103
  // Set the language of the field depending on the mapping.
104
  $language = isset($mapping['language']) ? $mapping['language'] : LANGUAGE_NONE;
105

    
106
  // Iterate over all values.
107
  $iterator = 0;
108
  $field = isset($entity->$target) ? $entity->$target : array();
109
  foreach ($values as $value) {
110

    
111
    // Only process if this value was set for this instance.
112
    if ($value) {
113
      switch ($match_key) {
114
        case 'guid':
115
        case 'url':
116
          // Fetch the entity ID resulting from the mapping table look-up.
117
          $entity_id = db_select('feeds_item', 'fi')
118
            ->fields('fi', array('entity_id'))
119
            ->condition($match_key, $value,'=')
120
            ->execute()
121
            ->fetchField();
122
          break;
123
        case 'etid':
124
          $entity_id = $value;
125
          break;
126
        case 'label':
127
          $options = $handler->getReferencableEntities($value, '=');
128
          if ($options) {
129
            $options = reset($options);
130
            $etids = array_keys($options);
131
            // Use the first matching entity.
132
            $entity_id = reset($etids);
133
          }
134
          else {
135
            $entity_id = NULL;
136
          }
137
          break;
138
      }
139
      /*
140
       * Only add a reference to an existing entity ID if there exists a
141
       * mapping between it and the provided GUID.  In cases where no such
142
       * mapping exists (yet), don't do anything here.  There may be a mapping
143
       * defined later in the CSV file.  If so, and the user re-runs the import
144
       * (as a second pass), we can add this reference then.  (The "Update
145
       * existing nodes" option must be selected during the second pass.)
146
       */
147
      if ($entity_id) {
148

    
149
        // Assign the target ID.
150
        $field[$language][$iterator]['target_id']   = $entity_id;
151
      }
152
      else /* there is no $entity_id, no mapping */ {
153

    
154
        /*
155
         * Feeds stores a hash of every line imported from CSVs in order to
156
         * make the import process more efficient by ignoring lines it's
157
         * already seen.  We need to short-circuit this process in this case
158
         * because users may want to re-import the same line as an update later
159
         * when (and if) a map to a reference exists.  So in order to provide
160
         * this opportunity later, we need to destroy the hash.
161
         */
162
        unset($entity->feeds_item->hash);
163
        $source->log('entityreference', t('No existing entity found for entity @source_id entityreference to source entity @value', array('@source_id' => $entity->feeds_item->entity_id, '@value' => $value)));
164
      }
165
    }
166

    
167
    // Break out of the loop if this field is single-valued.
168
    if ($info['cardinality'] == 1) {
169
      break;
170
    }
171
    $iterator++;
172
  }
173

    
174
  // Add the field to the entity definition.
175
  $entity->{$target} = $field;
176
}