Révision 41cc1b08
Ajouté par Assos Assos il y a presque 9 ans
drupal7/sites/all/modules/feeds/mappers/text.inc | ||
---|---|---|
6 | 6 |
*/ |
7 | 7 |
|
8 | 8 |
/** |
9 |
* Implements hook_feeds_processor_targets_alter(). |
|
10 |
* |
|
11 |
* @see FeedsProcessor::getMappingTargets() |
|
9 |
* Implements hook_feeds_processor_targets(). |
|
12 | 10 |
*/ |
13 |
function text_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) { |
|
11 |
function text_feeds_processor_targets($entity_type, $bundle_name) { |
|
12 |
$targets = array(); |
|
13 |
|
|
14 | 14 |
$text_types = array( |
15 |
'list_text', |
|
16 | 15 |
'text', |
17 | 16 |
'text_long', |
18 | 17 |
'text_with_summary', |
... | ... | |
26 | 25 |
'callback' => 'text_feeds_set_target', |
27 | 26 |
'description' => t('The @label field of the entity.', array('@label' => $instance['label'])), |
28 | 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'); |
|
29 | 42 |
} |
30 | 43 |
} |
44 |
|
|
45 |
return $targets; |
|
31 | 46 |
} |
32 | 47 |
|
33 | 48 |
/** |
34 | 49 |
* Callback for mapping text fields. |
35 | 50 |
*/ |
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 |
} |
|
51 |
function text_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) { |
|
52 |
list($field_name, $column) = explode(':', $target . ':value'); |
|
44 | 53 |
|
45 |
if (isset($source->importer->processor->config['input_format'])) { |
|
54 |
if ($column === 'value' && isset($source->importer->processor->config['input_format'])) {
|
|
46 | 55 |
$format = $source->importer->processor->config['input_format']; |
56 |
// Add in default values. |
|
57 |
$mapping += array( |
|
58 |
'format' => $format, |
|
59 |
); |
|
47 | 60 |
} |
48 | 61 |
|
49 |
$info = field_info_field($target);
|
|
62 |
$field = isset($entity->$field_name) ? $entity->$field_name : array('und' => array());
|
|
50 | 63 |
|
51 | 64 |
// Iterate over all values. |
52 |
$field = isset($entity->$target) ? $entity->$target : array('und' => array()); |
|
65 |
$delta = 0; |
|
66 |
foreach ($values as $value) { |
|
53 | 67 |
|
54 |
// Allow for multiple mappings to the same target. |
|
55 |
$delta = count($field['und']); |
|
68 |
if (is_object($value) && $value instanceof FeedsElement) { |
|
69 |
$value = $value->getValue(); |
|
70 |
} |
|
56 | 71 |
|
57 |
foreach ($value as $v) {
|
|
72 |
if (is_scalar($value) && strlen($value)) {
|
|
58 | 73 |
|
59 |
if ($info['cardinality'] == $delta) { |
|
60 |
break; |
|
61 |
} |
|
74 |
$field['und'][$delta][$column] = (string) $value; |
|
62 | 75 |
|
63 |
if (is_object($v) && ($v instanceof FeedsElement)) { |
|
64 |
$v = $v->getValue(); |
|
76 |
if (isset($mapping['format'])) { |
|
77 |
$field['und'][$delta]['format'] = $mapping['format']; |
|
78 |
} |
|
65 | 79 |
} |
66 | 80 |
|
67 |
if (is_scalar($v)) {
|
|
68 |
$field['und'][$delta]['value'] = $v;
|
|
81 |
$delta++;
|
|
82 |
}
|
|
69 | 83 |
|
70 |
if (isset($format)) { |
|
71 |
$field['und'][$delta]['format'] = $format; |
|
72 |
} |
|
84 |
$entity->$field_name = $field; |
|
85 |
} |
|
73 | 86 |
|
74 |
$delta++; |
|
75 |
} |
|
87 |
/** |
|
88 |
* Summary callback for text field targets. |
|
89 |
* |
|
90 |
* Displays which text format will be used for the text field target. |
|
91 |
* |
|
92 |
* @see text_feeds_processor_targets() |
|
93 |
* @see text_feeds_form_callback() |
|
94 |
*/ |
|
95 |
function text_feeds_summary_callback(array $mapping, $target, array $form, array $form_state) { |
|
96 |
global $user; |
|
97 |
$formats = filter_formats($user); |
|
98 |
|
|
99 |
// Processor-wide input format setting. |
|
100 |
$importer = feeds_importer($form['#importer']); |
|
101 |
$default_format = !empty($importer->processor->config['input_format']) ? $importer->processor->config['input_format'] : filter_fallback_format(); |
|
102 |
$mapping += array( |
|
103 |
'format' => $default_format, |
|
104 |
); |
|
105 |
|
|
106 |
return t('Text format: %format', array('%format' => $formats[$mapping['format']]->name)); |
|
107 |
} |
|
108 |
|
|
109 |
/** |
|
110 |
* Form callback for text field targets. |
|
111 |
* |
|
112 |
* Allows to select a text format for the text field target. |
|
113 |
* |
|
114 |
* @see text_feeds_processor_targets() |
|
115 |
* @see text_feeds_summary_callback() |
|
116 |
*/ |
|
117 |
function text_feeds_form_callback(array $mapping, $target, array $form, array $form_state) { |
|
118 |
global $user; |
|
119 |
$formats_options = array(); |
|
120 |
$formats = filter_formats($user); |
|
121 |
foreach ($formats as $id => $format) { |
|
122 |
$formats_options[$id] = $format->name; |
|
76 | 123 |
} |
77 | 124 |
|
78 |
$entity->$target = $field; |
|
125 |
// Processor-wide text format setting. |
|
126 |
$importer = feeds_importer($form['#importer']); |
|
127 |
$default_format = !empty($importer->processor->config['input_format']) ? $importer->processor->config['input_format'] : filter_fallback_format(); |
|
128 |
$mapping += array( |
|
129 |
'format' => $default_format, |
|
130 |
); |
|
131 |
|
|
132 |
return array( |
|
133 |
'format' => array( |
|
134 |
'#type' => 'select', |
|
135 |
'#title' => t('Text format'), |
|
136 |
'#options' => $formats_options, |
|
137 |
'#default_value' => $mapping['format'], |
|
138 |
), |
|
139 |
); |
|
79 | 140 |
} |
Formats disponibles : Unified diff
Update feeds 7.x-2.0-alpha9 -> 7.x-2.0-beta1
Install lib simplepie 1.3.1