1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* On behalf implementation of Feeds mapping API for locale.module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_feeds_processor_targets_alter().
|
10
|
*/
|
11
|
function locale_feeds_processor_targets_alter(array &$targets, $entity_type, $bundle_name) {
|
12
|
foreach (array_keys($targets) as $target) {
|
13
|
$targets[$target]['preprocess_callbacks'][] = 'locale_feeds_preprocess_callback';
|
14
|
$targets[$target]['summary_callbacks'][] = 'locale_feeds_summary_callback';
|
15
|
$targets[$target]['form_callbacks'][] = 'locale_feeds_form_callback';
|
16
|
}
|
17
|
}
|
18
|
|
19
|
/**
|
20
|
* Preprocess callback that sets the configured mapping language.
|
21
|
*/
|
22
|
function locale_feeds_preprocess_callback(array $target, array &$mapping) {
|
23
|
if (empty($mapping['field_language'])) {
|
24
|
return;
|
25
|
}
|
26
|
|
27
|
$mapping['language'] = $mapping['field_language'];
|
28
|
}
|
29
|
|
30
|
/**
|
31
|
* Summary callback.
|
32
|
*/
|
33
|
function locale_feeds_summary_callback(array $mapping, array $target, array $form, array $form_state) {
|
34
|
$entity_type = $form_state['build_info']['args'][0]->processor->entityType();
|
35
|
$translatable = _locale_feeds_target_is_translatable($entity_type, $mapping['target']);
|
36
|
|
37
|
$mapping += array('field_language' => LANGUAGE_NONE);
|
38
|
|
39
|
$language_options = array(LANGUAGE_NONE => t('Language neutral')) + locale_language_list('name');
|
40
|
|
41
|
$error = NULL;
|
42
|
if ($mapping['field_language'] !== LANGUAGE_NONE && !$translatable) {
|
43
|
// This is an invalid configuration that can come from disabling
|
44
|
// entity_translation.
|
45
|
$error = t('Field not translatable');
|
46
|
}
|
47
|
if (!isset($language_options[$mapping['field_language']])) {
|
48
|
// This is an invalid configuration that can be caused by disabling or
|
49
|
// removing the language in question.
|
50
|
$error = t('Language \'@lang\' not available', array('@lang' => $mapping['field_language']));
|
51
|
}
|
52
|
|
53
|
// Nothing to see here.
|
54
|
if (!$error && !$translatable) {
|
55
|
return;
|
56
|
}
|
57
|
|
58
|
if ($error) {
|
59
|
return t('Language: <strong>Error: @error</strong>', array('@error' => $error));
|
60
|
}
|
61
|
|
62
|
return t('Language: %lang', array('%lang' => $language_options[$mapping['field_language']]));
|
63
|
}
|
64
|
|
65
|
/**
|
66
|
* Form callback.
|
67
|
*/
|
68
|
function locale_feeds_form_callback(array $mapping, array $target, array $form, array $form_state) {
|
69
|
$form = array();
|
70
|
|
71
|
$entity_type = $form_state['build_info']['args'][0]->processor->entityType();
|
72
|
|
73
|
$translatable = _locale_feeds_target_is_translatable($entity_type, $mapping['target']);
|
74
|
$mapping += array('field_language' => LANGUAGE_NONE);
|
75
|
|
76
|
// This is an invalid configuration that can come from disabling
|
77
|
// entity_translation.
|
78
|
$error = $mapping['field_language'] !== LANGUAGE_NONE && !$translatable;
|
79
|
|
80
|
// Nothing to see here.
|
81
|
if (!$error && !$translatable) {
|
82
|
return $form;
|
83
|
}
|
84
|
|
85
|
$language_options = array(LANGUAGE_NONE => t('Language neutral'));
|
86
|
|
87
|
if (!$error) {
|
88
|
$language_options += locale_language_list('name');
|
89
|
}
|
90
|
|
91
|
$form['field_language'] = array(
|
92
|
'#type' => 'select',
|
93
|
'#title' => t('Language'),
|
94
|
'#options' => $language_options,
|
95
|
'#default_value' => $mapping['field_language'],
|
96
|
);
|
97
|
|
98
|
return $form;
|
99
|
}
|
100
|
|
101
|
/**
|
102
|
* Determines if a target is translatable.
|
103
|
*
|
104
|
* @param string $entity_type
|
105
|
* The entity type.
|
106
|
* @param string $target
|
107
|
* The target.
|
108
|
*
|
109
|
* @return bool
|
110
|
* Returns true if the target is translatable, false if not.
|
111
|
*/
|
112
|
function _locale_feeds_target_is_translatable($entity_type, $target) {
|
113
|
list($field_name) = explode(':', $target, 2);
|
114
|
|
115
|
$info = field_info_field($field_name);
|
116
|
|
117
|
return !empty($info) && field_is_translatable($entity_type, $info);
|
118
|
}
|