diff --git a/entity_translation.module b/entity_translation.module index 947caff..20dde72 100644 --- a/entity_translation.module +++ b/entity_translation.module @@ -2078,3 +2078,60 @@ function entity_translation_entity_save($entity_type, $entity) { field_attach_update($entity_type, $entity); } } + +/** + * Migrate existing field content from language "undefined" to entity language. + * + * This is an API function that can be called from a hook_update_N() + * implementation as follows: + * @code + * function mymodule_update_N(&$sandbox) { + * return entity_translation_migrate_field_content_update($sandbox); + * } + * @endcode + * + * Using this API function is necessary when fields are made translatable via + * some deployment method (like using the features module) as the regular batch + * migration is not triggered in this case. + */ +function entity_translation_migrate_field_content_update(&$sandbox) { + // Number of entities to be processed for each step. + $messages = array(); + if (!isset($sandbox['fields'])) { + // Initialize the array of field to process. + $sandbox['fields'] = array_filter(field_info_fields(), function($field_info) {return $field_info['translatable'];}); + foreach (array_keys($sandbox['fields']) as $field_name) { + $sandbox['fields'][$field_name]['context'] = array('sandbox' => array(), 'finished' => 0); + } + } + // Search for an unfinished field. + $field_names = array_keys($sandbox['fields']); + $field_name = &reset($field_names); + while ($field_name && $sandbox['fields'][$field_name]['context']['finished'] >= 1) { + $field_name = &next($field_names); + } + if ($field_name) { + // Process the field. + module_load_include('inc', 'entity_translation', 'entity_translation.admin'); + entity_translation_translatable_batch(TRUE, $sandbox['fields'][$field_name]['field_name'], TRUE, $sandbox['fields'][$field_name]['context']); + $sandbox['#finished'] = array_reduce($sandbox['fields'], function($carry, $item) { + return $carry + $item['context']['finished']; + }, 0) / count($sandbox['fields']); + $message_args = array( + '@field_name' => $sandbox['fields'][$field_name]['field_name'], + '@progress' => (number_format($sandbox['#finished'] * 100)), + '@field_progress' => (number_format($sandbox['fields'][$field_name]['context']['finished'] * 100)), + ); + if ($sandbox['fields'][$field_name]['context']['finished'] >= 1) { + $messages[] = format_string('@progress - Done with @field_name.', $message_args); + } + else { + $messages[] = format_string('@progress - Processing @field_name (@field_progress)... ', $message_args); + } + } + else { + // No more field to process, we are done. + $sandbox['#finished'] = 1; + } + return implode("\n", $messages); +}