function lingotek_process_entity_xml in Lingotek Translation 7.5
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.6 lingotek.remote.inc \lingotek_process_entity_xml()
1 call to lingotek_process_entity_xml()
File
- ./
lingotek.remote.inc, line 91
Code
function lingotek_process_entity_xml($xml, $entity, $entity_type, $drupal_language_code, $node_based_translation = FALSE, $url_alias_translation = 0) {
list($id, $vid, $bundle) = lingotek_entity_extract_ids($entity_type, $entity);
// Set $node_based if the entity type is a node, inherit otherwise.
if ($entity_type == 'node') {
$node_based_translation = lingotek_uses_node_translation($entity) ? TRUE : FALSE;
}
// Must check for entity type and node-based translation, as the entity type
// could be a field collection nested within a node-based node.
$source_entity = $entity_type == 'node' && $node_based_translation ? lingotek_get_source_node($entity) : $entity;
// Add revision_id to be the same as the entity_id if it was not found or if
// it is a comment.
if ($vid === NULL || $entity_type == 'comment') {
$vid = $id;
}
$delta = 0;
$last_tag = NULL;
foreach ($xml as $tag => $content) {
if ($tag == $last_tag) {
$delta++;
}
else {
$delta = 0;
}
if ($tag == 'url_alias' && $url_alias_translation == 1) {
$target = check_plain($content);
//URL Alias related to the page:
$conditions = array(
'source' => 'node/' . $id,
);
if ($entity->language != LANGUAGE_NONE) {
$conditions['language'] = $entity->language;
}
$path = path_load($conditions);
if ($path !== FALSE) {
$conditions['language'] = $drupal_language_code;
if ($path['alias'] != $target || $entity->language != $drupal_language_code) {
$original = path_load($conditions);
$conditions['alias'] = $target;
if ($original === FALSE) {
path_save($conditions);
}
else {
path_delete($original);
path_save($conditions);
}
}
}
}
else {
//this part of the xml is a field
$drupal_field_name = $tag;
$field = field_info_field($drupal_field_name);
$source_field_data = field_get_items($entity_type, $source_entity, $drupal_field_name);
$translatable_field = !empty($field['translatable']);
$field_language = $node_based_translation && !$translatable_field ? LANGUAGE_NONE : $drupal_language_code;
$target_key = 'value';
$subfield_parts = explode('__', $tag);
if (count($subfield_parts) == 2) {
$drupal_field_name = $subfield_parts[0];
$target_key = $subfield_parts[1];
}
if (isset($field)) {
$curr_field_data =& $entity->{$drupal_field_name};
$index = 0;
if (module_exists('link') && $field['type'] == 'link_field') {
$target_key = array(
'title',
'url',
);
}
if (!is_array($target_key)) {
$target_key = array(
$target_key,
);
}
$insert_array = array(
'entity_type' => $entity_type,
'bundle' => $bundle,
'entity_id' => $id,
'revision_id' => $vid,
'language' => $field_language,
'delta' => $delta,
'deleted' => '0',
);
$field_names = array(
'field_revision_' . $field['field_name'],
'field_data_' . $field['field_name'],
);
if (module_exists('field_collection') && $field['type'] == 'field_collection') {
$default = language_default();
$default_language = $default->language;
if (isset($curr_field_data[LANGUAGE_NONE][$delta]['value'])) {
$field_collection_id = $curr_field_data[LANGUAGE_NONE][$delta]['value'];
}
elseif (isset($curr_field_data[$default_language][$delta]['value'])) {
$field_collection_id = $curr_field_data[$language_default][$delta]['value'];
}
else {
if (!$node_based_translation) {
// The field-collection field must be empty.
continue;
}
// If it does not exist and the profile is node-based, create a new FC.
$field_collection_item = entity_create('field_collection_item', array(
'field_name' => $field['field_name'],
));
$field_collection_item
->setHostEntity($entity_type, $entity);
$field_collection_item
->save();
$field_collection_id = $field_collection_item->item_id;
}
$field_collection_item = lingotek_entity_load_single('field_collection_item', $field_collection_id);
if (!$field_collection_item) {
// The field collection was removed, so disregard any info on it
continue;
}
$field_collection_item->type = $field['field_name'];
$field_collection_item->language = $entity->language;
$field_collection_item->nid = $field_collection_item->item_id;
$field_collection_item->vid = $field_collection_item->revision_id;
// RECURSION FOR FIELD COLLECTIONS
lingotek_process_entity_xml($content, $field_collection_item, 'field_collection_item', $drupal_language_code, $node_based_translation);
$last_tag = $tag;
continue;
}
else {
foreach ($content as $text) {
// Set all field-collection fields to translatable, if not node-based.
if ($entity_type == 'field_collection_item' && !$node_based_translation && $field['translatable'] != 1) {
$field['translatable'] = 1;
field_update_field($field);
}
$is_link = in_array('url', $target_key);
$without_title = count($content) == 1;
if ($is_link && $without_title) {
$array_key = $target_key[1];
}
else {
$array_key = $target_key[$index];
}
$db_field_name = $field['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);
$language_list = language_list('language');
$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),
));
}
// Assign the field format.
$field_format = NULL;
if (!empty($source_field_data[0]['format'])) {
$field_format = $source_field_data[0]['format'];
}
elseif (!empty($curr_field_data[$entity->language][0]['format'])) {
$field_format = $curr_field_data[$entity->language][0]['format'];
}
if (!empty($field_format)) {
$format_db_field_name = $field['field_name'] . '_format';
$insert_array[$format_db_field_name] = $field_format;
}
$index++;
}
}
foreach ($field_names as $field_name) {
// using drupal_write_record to avoid node_save - node_save overwrites publications unless called on both revised and published versions of the node (i.e. workbench_moderation)
// UPDATE: This could perhaps be rewritten now that workbench_moderation is supported through the rules module
try {
drupal_write_record($field_name, $insert_array);
} catch (PDOException $e) {
$primary_keys = array(
'entity_type',
'entity_id',
'revision_id',
'deleted',
'delta',
'language',
);
drupal_write_record($field_name, $insert_array, $primary_keys);
}
}
}
cache_clear_all('field:' . $entity_type . ':' . $id, 'cache_field');
//Set URL alias
if ($tag == 'title_field' && $url_alias_translation == 2 && module_exists('pathauto') && $entity->language != LANGUAGE_NONE) {
module_load_include('inc', 'pathauto');
$uri = entity_uri('node', $entity);
$entity_unchanged = entity_load_unchanged('node', $id);
pathauto_create_alias('node', 'update', $uri['path'], array(
'node' => clone $entity_unchanged,
), $entity->type, $drupal_language_code);
}
}
$last_tag = $tag;
}
}