entity_translation.inc 2.4 KB

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