1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* On behalf implementation of Feeds mapping API for text.module.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
41cc1b08
|
Assos Assos
|
* Implements hook_feeds_processor_targets().
|
10 |
85ad3d82
|
Assos Assos
|
*/
|
11 |
41cc1b08
|
Assos Assos
|
function text_feeds_processor_targets($entity_type, $bundle_name) {
|
12 |
|
|
$targets = array();
|
13 |
|
|
|
14 |
85ad3d82
|
Assos Assos
|
$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 |
41cc1b08
|
Assos Assos
|
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 |
85ad3d82
|
Assos Assos
|
}
|
43 |
|
|
}
|
44 |
41cc1b08
|
Assos Assos
|
|
45 |
|
|
return $targets;
|
46 |
85ad3d82
|
Assos Assos
|
}
|
47 |
|
|
|
48 |
|
|
/**
|
49 |
|
|
* Callback for mapping text fields.
|
50 |
|
|
*/
|
51 |
41cc1b08
|
Assos Assos
|
function text_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
|
52 |
a192dc0b
|
Assos Assos
|
$language = $mapping['language'];
|
53 |
|
|
|
54 |
41cc1b08
|
Assos Assos
|
list($field_name, $column) = explode(':', $target . ':value');
|
55 |
85ad3d82
|
Assos Assos
|
|
56 |
41cc1b08
|
Assos Assos
|
if ($column === 'value' && isset($source->importer->processor->config['input_format'])) {
|
57 |
85ad3d82
|
Assos Assos
|
$format = $source->importer->processor->config['input_format'];
|
58 |
41cc1b08
|
Assos Assos
|
// Add in default values.
|
59 |
|
|
$mapping += array(
|
60 |
|
|
'format' => $format,
|
61 |
|
|
);
|
62 |
85ad3d82
|
Assos Assos
|
}
|
63 |
|
|
|
64 |
a192dc0b
|
Assos Assos
|
$field = isset($entity->$field_name) ? $entity->$field_name : array($language => array());
|
65 |
85ad3d82
|
Assos Assos
|
|
66 |
|
|
// Iterate over all values.
|
67 |
41cc1b08
|
Assos Assos
|
$delta = 0;
|
68 |
|
|
foreach ($values as $value) {
|
69 |
85ad3d82
|
Assos Assos
|
|
70 |
41cc1b08
|
Assos Assos
|
if (is_object($value) && $value instanceof FeedsElement) {
|
71 |
|
|
$value = $value->getValue();
|
72 |
|
|
}
|
73 |
85ad3d82
|
Assos Assos
|
|
74 |
41cc1b08
|
Assos Assos
|
if (is_scalar($value) && strlen($value)) {
|
75 |
85ad3d82
|
Assos Assos
|
|
76 |
a192dc0b
|
Assos Assos
|
$field[$language][$delta][$column] = (string) $value;
|
77 |
85ad3d82
|
Assos Assos
|
|
78 |
41cc1b08
|
Assos Assos
|
if (isset($mapping['format'])) {
|
79 |
a192dc0b
|
Assos Assos
|
$field[$language][$delta]['format'] = $mapping['format'];
|
80 |
41cc1b08
|
Assos Assos
|
}
|
81 |
85ad3d82
|
Assos Assos
|
}
|
82 |
|
|
|
83 |
41cc1b08
|
Assos Assos
|
$delta++;
|
84 |
|
|
}
|
85 |
85ad3d82
|
Assos Assos
|
|
86 |
41cc1b08
|
Assos Assos
|
$entity->$field_name = $field;
|
87 |
|
|
}
|
88 |
85ad3d82
|
Assos Assos
|
|
89 |
41cc1b08
|
Assos Assos
|
/**
|
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 |
85ad3d82
|
Assos Assos
|
}
|
126 |
|
|
|
127 |
41cc1b08
|
Assos Assos
|
// 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 |
85ad3d82
|
Assos Assos
|
} |