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().
|
10
|
*/
|
11
|
function text_feeds_processor_targets($entity_type, $bundle_name) {
|
12
|
$targets = array();
|
13
|
|
14
|
$text_types = array(
|
15
|
'text',
|
16
|
'text_long',
|
17
|
'text_with_summary',
|
18
|
);
|
19
|
foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
|
20
|
$info = field_info_field($name);
|
21
|
|
22
|
if (in_array($info['type'], $text_types)) {
|
23
|
$targets[$name] = array(
|
24
|
'name' => check_plain($instance['label']),
|
25
|
'callback' => 'text_feeds_set_target',
|
26
|
'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
|
27
|
);
|
28
|
if ($info['type'] == 'text_with_summary') {
|
29
|
// Allow mapping to summary.
|
30
|
$targets[$name . ':summary'] = array(
|
31
|
'name' => t('@name: Summary', array('@name' => $instance['label'])),
|
32
|
'callback' => 'text_feeds_set_target',
|
33
|
'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
|
34
|
'real_target' => $name,
|
35
|
);
|
36
|
}
|
37
|
}
|
38
|
|
39
|
if (!empty($instance['settings']['text_processing'])) {
|
40
|
$targets[$name]['summary_callbacks'] = array('text_feeds_summary_callback');
|
41
|
$targets[$name]['form_callbacks'] = array('text_feeds_form_callback');
|
42
|
}
|
43
|
}
|
44
|
|
45
|
return $targets;
|
46
|
}
|
47
|
|
48
|
/**
|
49
|
* Callback for mapping text fields.
|
50
|
*/
|
51
|
function text_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
|
52
|
$language = $mapping['language'];
|
53
|
|
54
|
list($field_name, $column) = explode(':', $target . ':value');
|
55
|
|
56
|
if ($column === 'value' && isset($source->importer->processor->config['input_format'])) {
|
57
|
$format = $source->importer->processor->config['input_format'];
|
58
|
// Add in default values.
|
59
|
$mapping += array(
|
60
|
'format' => $format,
|
61
|
);
|
62
|
}
|
63
|
|
64
|
$field = isset($entity->$field_name) ? $entity->$field_name : array($language => array());
|
65
|
|
66
|
// Iterate over all values.
|
67
|
$delta = 0;
|
68
|
foreach ($values as $value) {
|
69
|
|
70
|
if (is_object($value) && $value instanceof FeedsElement) {
|
71
|
$value = $value->getValue();
|
72
|
}
|
73
|
|
74
|
if (is_scalar($value) && strlen($value)) {
|
75
|
|
76
|
$field[$language][$delta][$column] = (string) $value;
|
77
|
|
78
|
if (isset($mapping['format'])) {
|
79
|
$field[$language][$delta]['format'] = $mapping['format'];
|
80
|
}
|
81
|
}
|
82
|
|
83
|
$delta++;
|
84
|
}
|
85
|
|
86
|
$entity->$field_name = $field;
|
87
|
}
|
88
|
|
89
|
/**
|
90
|
* Summary callback for text field targets.
|
91
|
*
|
92
|
* Displays which text format will be used for the text field target.
|
93
|
*
|
94
|
* @see text_feeds_processor_targets()
|
95
|
* @see text_feeds_form_callback()
|
96
|
*/
|
97
|
function text_feeds_summary_callback(array $mapping, $target, array $form, array $form_state) {
|
98
|
global $user;
|
99
|
$formats = filter_formats($user);
|
100
|
|
101
|
// Processor-wide input format setting.
|
102
|
$importer = feeds_importer($form['#importer']);
|
103
|
$default_format = !empty($importer->processor->config['input_format']) ? $importer->processor->config['input_format'] : filter_fallback_format();
|
104
|
$mapping += array(
|
105
|
'format' => $default_format,
|
106
|
);
|
107
|
|
108
|
return t('Text format: %format', array('%format' => $formats[$mapping['format']]->name));
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* Form callback for text field targets.
|
113
|
*
|
114
|
* Allows to select a text format for the text field target.
|
115
|
*
|
116
|
* @see text_feeds_processor_targets()
|
117
|
* @see text_feeds_summary_callback()
|
118
|
*/
|
119
|
function text_feeds_form_callback(array $mapping, $target, array $form, array $form_state) {
|
120
|
global $user;
|
121
|
$formats_options = array();
|
122
|
$formats = filter_formats($user);
|
123
|
foreach ($formats as $id => $format) {
|
124
|
$formats_options[$id] = $format->name;
|
125
|
}
|
126
|
|
127
|
// Processor-wide text format setting.
|
128
|
$importer = feeds_importer($form['#importer']);
|
129
|
$default_format = !empty($importer->processor->config['input_format']) ? $importer->processor->config['input_format'] : filter_fallback_format();
|
130
|
$mapping += array(
|
131
|
'format' => $default_format,
|
132
|
);
|
133
|
|
134
|
return array(
|
135
|
'format' => array(
|
136
|
'#type' => 'select',
|
137
|
'#title' => t('Text format'),
|
138
|
'#options' => $formats_options,
|
139
|
'#default_value' => $mapping['format'],
|
140
|
),
|
141
|
);
|
142
|
}
|