1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* On behalf implementation of Feeds mapping API for entity_translation.module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_feeds_presave().
|
10
|
*/
|
11
|
function entity_translation_feeds_presave(FeedsSource $source, $entity, $item, $entity_id) {
|
12
|
$entity_type = $entity->feeds_item->entity_type;
|
13
|
|
14
|
// Check that it's a real entity type, and translation is enabled.
|
15
|
if (!entity_get_info($entity_type) || !entity_translation_enabled($entity_type, $entity)) {
|
16
|
return;
|
17
|
}
|
18
|
|
19
|
if (!$handler = entity_translation_get_handler($entity_type, $entity)) {
|
20
|
return;
|
21
|
}
|
22
|
|
23
|
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
|
24
|
|
25
|
$languages_seen = array();
|
26
|
|
27
|
foreach (field_info_instances($entity_type, $bundle) as $instance) {
|
28
|
$field_name = $instance['field_name'];
|
29
|
|
30
|
// No values in this field, skip it.
|
31
|
if (empty($entity->$field_name) || !is_array($entity->$field_name)) {
|
32
|
continue;
|
33
|
}
|
34
|
|
35
|
// Not translatable.
|
36
|
$info = field_info_field($field_name);
|
37
|
if (!$info || !$info['translatable']) {
|
38
|
continue;
|
39
|
}
|
40
|
|
41
|
// Init the translation handler.
|
42
|
if (empty($handler->getTranslations()->original)) {
|
43
|
$handler->initTranslations();
|
44
|
}
|
45
|
|
46
|
// Avoid invalid user configuration. Entity translation does this when
|
47
|
// loading the translation overview page.
|
48
|
if (count($entity->$field_name) === 1 && key($entity->$field_name) === LANGUAGE_NONE && $handler->getLanguage() !== LANGUAGE_NONE) {
|
49
|
$entity->{$field_name}[$handler->getLanguage()] = $entity->{$field_name}[LANGUAGE_NONE];
|
50
|
$entity->{$field_name}[LANGUAGE_NONE] = array();
|
51
|
}
|
52
|
|
53
|
// Look for languages we haven't created a translation for yet.
|
54
|
foreach (array_diff_key($entity->$field_name, $languages_seen) as $language => $v) {
|
55
|
if ($language === LANGUAGE_NONE) {
|
56
|
continue;
|
57
|
}
|
58
|
|
59
|
$languages_seen[$language] = TRUE;
|
60
|
|
61
|
if ($language === $handler->getLanguage()) {
|
62
|
continue;
|
63
|
}
|
64
|
|
65
|
$translation = array(
|
66
|
'translate' => 0,
|
67
|
'status' => 1,
|
68
|
'language' => $language,
|
69
|
'source' => $handler->getLanguage(),
|
70
|
);
|
71
|
|
72
|
$handler->setTranslation($translation, $entity);
|
73
|
}
|
74
|
}
|
75
|
|
76
|
// Loop through every language for the site, and remove translations for the
|
77
|
// ones that don't have any values.
|
78
|
foreach (language_list() as $language) {
|
79
|
if (!isset($languages_seen[$language->language])) {
|
80
|
$handler->removeTranslation($language->language);
|
81
|
}
|
82
|
}
|
83
|
}
|