function lingotek_process_entity_xml in Lingotek Translation 7.6
Same name and namespace in other branches
- 7.7 lingotek.remote.inc \lingotek_process_entity_xml()
- 7.4 lingotek.api.inc \lingotek_process_entity_xml()
- 7.5 lingotek.remote.inc \lingotek_process_entity_xml()
2 calls to lingotek_process_entity_xml()
File
- ./
lingotek.remote.inc, line 113
Code
function lingotek_process_entity_xml($xml, &$entity, $entity_type, $langcode, $node_based_translation = FALSE, $url_alias_translation = 0) {
$language_list = language_list('language');
$source_entity = $entity;
$hook_params = array(
'entity_type' => $entity_type,
'entity' => $entity,
'xml' => $xml,
'langcode' => $langcode,
);
// First allow other modules to manipulate the downloaded content.
drupal_alter('lingotek_entity_download', $hook_params);
// Set $node_based_translation only if the entity type is a node, inherit otherwise.
// (The entity could be a field collection or other entity type, underneath)
if ($entity_type == 'node') {
$node_based_translation = lingotek_uses_node_translation($entity) ? TRUE : FALSE;
if ($node_based_translation) {
$source_entity = lingotek_get_source_node($entity);
}
}
list($id, $vid, $bundle) = lingotek_entity_extract_ids($entity_type, $entity);
$delta = 0;
$last_tag = NULL;
foreach ($xml as $tag => $content) {
// Handle multiple instances of the same field (handled in Drupal using 'delta')
$delta = $tag == $last_tag ? ++$delta : 0;
// URL alias translation (currently available for nodes only)
if ($entity_type == 'node' && $tag == 'url_alias' && $url_alias_translation == 1) {
lingotek_save_url_alias($content, $entity, $langcode);
$last_tag = $tag;
continue;
}
// Handle compound fields (multiple subfields, like 'summary' and 'body')
$field_name = $tag;
$target_key = array(
'value',
);
$subfield_parts = explode('__', $tag);
if (count($subfield_parts) == 2) {
$field_name = $subfield_parts[0];
$target_key = array(
$subfield_parts[1],
);
}
// Get field info or skip if not found
$field_info = field_info_field($field_name);
if (empty($field_info)) {
LingotekLog::warning('Field info downloaded from Lingotek, but field not found: @field_name', array(
'@field_name' => $field_name,
));
$last_tag = $tag;
continue;
}
// Try to get the source field's text format, if available
try {
$source_field_data = field_get_items($entity_type, $source_entity, $field_name);
} catch (EntityMalformedException $e) {
$source_field_data = array();
}
$translatable_field = !empty($field_info['translatable']);
$field_language = $node_based_translation && !$translatable_field ? LANGUAGE_NONE : $langcode;
// Field-Collection Fields: Pull the underlying entities and recurse.
if (module_exists('field_collection') && $field_info['type'] == 'field_collection') {
lingotek_process_field_collection_xml($content, $entity_type, $entity, $field_name, $delta, $langcode, $node_based_translation);
$last_tag = $tag;
continue;
}
// Field-Collection Entities: Set all field-collection fields to translatable,
// if the parent node is not using node-based translation.
if ($entity_type == 'field_collection_item' && !$node_based_translation && $field_info['translatable'] != 1) {
$field_info['translatable'] = 1;
field_update_field($field_info);
}
// Handle (normal?) fields from here down.
$is_link = FALSE;
if (module_exists('link') && $field_info['type'] == 'link_field') {
$is_link = TRUE;
$target_key = array(
'title',
'url',
);
}
$insert_array = array(
'entity_type' => $entity_type,
'bundle' => $bundle,
'entity_id' => $id,
'revision_id' => $vid === NULL || $entity_type == 'comment' ? $id : $vid,
'language' => $field_language,
'delta' => $delta,
'deleted' => '0',
);
$curr_field_data =& $entity->{$field_name};
$index = 0;
foreach ($content as $text) {
$without_title = count($content) == 1;
if (module_exists('link') && $field_info['type'] == 'link_field' && $without_title) {
$array_key = 'url';
}
else {
$array_key = $target_key[$index];
}
$db_field_name = $field_info['field_name'] . '_' . $array_key;
$insert_array[$db_field_name] = lingotek_unfilter_placeholders(decode_entities($text));
if ($db_field_name == 'title_field_value' && strlen($insert_array[$db_field_name]) > 254) {
$insert_array[$db_field_name] = substr($insert_array[$db_field_name], 0, 254);
$langcode = $insert_array['language'];
LingotekLog::info('The @lang (@langcode) title was truncated, since the translation exceeded the maximum of 255 characters.', array(
'@lang' => $language_list[$langcode]->name,
'@langcode' => Lingotek::convertDrupal2Lingotek($langcode),
));
}
lingotek_assign_field_format($insert_array, $source_field_data, $curr_field_data, $field_info['field_name'], $entity->language);
$index++;
}
// Save to both field_data tables and field_revision tables
$field_table_names = array(
'field_revision_' . $field_info['field_name'],
'field_data_' . $field_info['field_name'],
);
foreach ($field_table_names as $fname) {
lingotek_save_field_record($fname, $insert_array);
}
// After every field insert, reset the caches and reload the entity
// TODO: Do we really need to do this every time we save a field, or
// can we just do this once at the end?
cache_clear_all('field:' . $entity_type . ':' . $id, 'cache_field');
entity_get_controller($entity_type)
->resetCache(array(
$id,
));
$entity = lingotek_entity_load_single($entity_type, $id);
//Set URL alias
if ($tag == 'title_field' && $url_alias_translation == 2 && module_exists('pathauto') && $entity->language != LANGUAGE_NONE) {
lingotek_save_pathauto_alias($entity_type, $entity, $langcode);
}
$last_tag = $tag;
}
}