translation.migrate.inc 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * @file
  4. * MigrateTranslationEntityHandler class for entity_translation.
  5. */
  6. /**
  7. * Destination handler implementing translatable fields.
  8. */
  9. class MigrateTranslationEntityHandler extends MigrateDestinationHandler {
  10. /**
  11. * Registers all entites as handled by this class.
  12. */
  13. public function __construct() {
  14. $this->registerTypes(array('entity'));
  15. }
  16. /**
  17. * Handles Entity Translations.
  18. *
  19. * @param stdClass $entity
  20. * @param stdClass $sourceRow
  21. */
  22. public function prepare($entity, stdClass $row) {
  23. $migration = Migration::currentMigration();
  24. $entity_type = $migration->getDestination()->getEntityType();
  25. // Only continue if the entity type + bundle combination is enabled for
  26. // entity_translation.
  27. if (entity_translation_enabled($entity_type, $entity)) {
  28. // Get the entity_translation handler and load any existing translations.
  29. $handler = entity_translation_get_handler($entity_type, $entity);
  30. $handler->loadTranslations();
  31. $original_language = $handler->getLanguage();
  32. // Get the bundle of the entity.
  33. list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  34. // We need to get all of the possible translations to create. So we look
  35. // for any translatable fields.
  36. $translatable_fields = array();
  37. foreach (field_info_instances($entity_type, $bundle) as $instance) {
  38. $field_name = $instance['field_name'];
  39. $field = field_info_field($field_name);
  40. if ($field['translatable']) {
  41. $translatable_fields[] = $field_name;
  42. }
  43. }
  44. // In those fields we look for translated values to build out an array
  45. // of languages we need to set translations for.
  46. $translate_languages = array();
  47. foreach ($translatable_fields as $translatable_field) {
  48. if (!empty($entity->{$translatable_field})) {
  49. $field_languages = array_keys($entity->{$translatable_field});
  50. foreach ($field_languages as $field_language) {
  51. $translate_languages[$field_language] = $field_language;
  52. }
  53. }
  54. }
  55. // Remove the LANGUAGE_NONE results.
  56. unset($translate_languages[LANGUAGE_NONE]);
  57. // Remove the original language.
  58. unset($translate_languages[$original_language]);
  59. // Anything we're left with is a translation that should be set.
  60. foreach ($translate_languages as $translate_language) {
  61. if (!isset($entity->translations->data[$translate_language])) {
  62. // Add the new translation and store it.
  63. $handler->setTranslation(array(
  64. 'translate' => 0,
  65. 'status' => 1,
  66. 'language' => $translate_language,
  67. 'source' => $original_language,
  68. 'uid' => (empty($entity->uid)) ? 0 : $entity->uid,
  69. 'changed' => (empty($entity->changed)) ? REQUEST_TIME : $entity->changed,
  70. 'created' => (empty($entity->created)) ? REQUEST_TIME : $entity->created,
  71. ));
  72. }
  73. }
  74. }
  75. }
  76. }