1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* On behalf implementation of Feeds mapping API for text.module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_feeds_processor_targets_alter().
|
10
|
*
|
11
|
* @see FeedsProcessor::getMappingTargets()
|
12
|
*/
|
13
|
function text_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
|
14
|
$text_types = array(
|
15
|
'list_text',
|
16
|
'text',
|
17
|
'text_long',
|
18
|
'text_with_summary',
|
19
|
);
|
20
|
foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
|
21
|
$info = field_info_field($name);
|
22
|
|
23
|
if (in_array($info['type'], $text_types)) {
|
24
|
$targets[$name] = array(
|
25
|
'name' => check_plain($instance['label']),
|
26
|
'callback' => 'text_feeds_set_target',
|
27
|
'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
|
28
|
);
|
29
|
}
|
30
|
}
|
31
|
}
|
32
|
|
33
|
/**
|
34
|
* Callback for mapping text fields.
|
35
|
*/
|
36
|
function text_feeds_set_target($source, $entity, $target, $value) {
|
37
|
if (empty($value)) {
|
38
|
return;
|
39
|
}
|
40
|
|
41
|
if (!is_array($value)) {
|
42
|
$value = array($value);
|
43
|
}
|
44
|
|
45
|
if (isset($source->importer->processor->config['input_format'])) {
|
46
|
$format = $source->importer->processor->config['input_format'];
|
47
|
}
|
48
|
|
49
|
$info = field_info_field($target);
|
50
|
|
51
|
// Iterate over all values.
|
52
|
$field = isset($entity->$target) ? $entity->$target : array('und' => array());
|
53
|
|
54
|
// Allow for multiple mappings to the same target.
|
55
|
$delta = count($field['und']);
|
56
|
|
57
|
foreach ($value as $v) {
|
58
|
|
59
|
if ($info['cardinality'] == $delta) {
|
60
|
break;
|
61
|
}
|
62
|
|
63
|
if (is_object($v) && ($v instanceof FeedsElement)) {
|
64
|
$v = $v->getValue();
|
65
|
}
|
66
|
|
67
|
if (is_scalar($v)) {
|
68
|
$field['und'][$delta]['value'] = $v;
|
69
|
|
70
|
if (isset($format)) {
|
71
|
$field['und'][$delta]['format'] = $format;
|
72
|
}
|
73
|
|
74
|
$delta++;
|
75
|
}
|
76
|
}
|
77
|
|
78
|
$entity->$target = $field;
|
79
|
}
|