1 |
41cc1b08
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* On behalf implementation of Feeds mapping API for field.module.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Implements hook_feeds_presave().
|
10 |
|
|
*/
|
11 |
|
|
function field_feeds_presave(FeedsSource $source, $entity, $item, $entity_id) {
|
12 |
|
|
$entity_type = $entity->feeds_item->entity_type;
|
13 |
|
|
|
14 |
|
|
// Not a real entity.
|
15 |
|
|
if (!entity_get_info($entity_type)) {
|
16 |
|
|
return;
|
17 |
|
|
}
|
18 |
|
|
|
19 |
|
|
// Gather the fields that Feeds is mapping to.
|
20 |
|
|
$feeds_fields = array();
|
21 |
|
|
foreach ($source->importer()->processor->getMappings() as $mapping) {
|
22 |
|
|
list($field) = explode(':', $mapping['target']);
|
23 |
|
|
$feeds_fields[$field] = TRUE;
|
24 |
|
|
}
|
25 |
|
|
|
26 |
|
|
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
|
27 |
|
|
|
28 |
|
|
foreach (field_info_instances($entity_type, $bundle) as $instance) {
|
29 |
|
|
$field_name = $instance['field_name'];
|
30 |
|
|
|
31 |
|
|
// Skip fields that Feeds isn't mapping to, and empty fields.
|
32 |
|
|
if (!isset($feeds_fields[$field_name]) || empty($entity->$field_name) || !is_array($entity->$field_name)) {
|
33 |
|
|
continue;
|
34 |
|
|
}
|
35 |
|
|
|
36 |
|
|
$info = field_info_field($field_name);
|
37 |
|
|
|
38 |
|
|
foreach ($entity->$field_name as $language => $values) {
|
39 |
|
|
// Filter out empty values.
|
40 |
|
|
$values = _field_filter_items($info, $values);
|
41 |
|
|
|
42 |
|
|
// Check that the number of values doesn't exceed the field cardinality.
|
43 |
|
|
if ($info['cardinality'] != FIELD_CARDINALITY_UNLIMITED && count($values) > $info['cardinality']) {
|
44 |
|
|
$values = array_slice($values, 0, $info['cardinality']);
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
$entity->{$field_name}[$language] = $values;
|
48 |
|
|
}
|
49 |
|
|
}
|
50 |
|
|
} |