View source
<?php
function globallink_entity_send_for_translations($nids, $pd4, $submission_name, $due_date, $project_code, $source_locale, $target_locale_arr, $submission_details, $submission_priority) {
module_load_include('inc', 'globallink', 'gl_ws/gl_ws_send_translations');
module_load_include('inc', 'globallink', 'globallink');
$node_check = variable_get('globallink_implementation_type', 0);
$submitter = $submission_details['submitter'];
$drupal_locale_code = globallink_get_drupal_locale_code($source_locale);
$globallink_arr = array();
foreach ($nids as $nid) {
list($nid, $vid) = explode('-', $nid, 2);
$rows = globallink_entity_get_sent_rows_by_nid($nid);
$target_arr = $target_locale_arr;
foreach ($rows as $row) {
if (array_search($row->target_lang_code, $target_locale_arr)) {
unset($target_arr[$row->target_lang_code]);
watchdog(GLOBALLINK_MODULE, 'Skipping Node Id - %id for locales %locale', array(
'%id' => $nid,
'%locale' => $row->target_lang_code,
), WATCHDOG_DEBUG);
}
}
if (empty($target_arr)) {
continue;
}
$node = node_load($nid, $vid);
if ($node_check == 1) {
foreach ($target_arr as $key => $target_locale) {
if (!globallink_translate_node_for_language($node, globallink_get_drupal_locale_code($target_locale))) {
unset($target_arr[$key]);
}
}
}
if (empty($target_arr)) {
watchdog(GLOBALLINK_MODULE, 'No target languages. Skipping nid - %nid', array(
'%nid' => $nid,
), WATCHDOG_CRITICAL);
continue;
}
$drupal_target_arr = array();
foreach ($target_arr as $target_locale) {
array_push($drupal_target_arr, globallink_get_drupal_locale_code($target_locale));
}
$tnid = NULL;
$tvid = NULL;
$name = '.xml';
$xml_targets = globallink_entity_get_xml($node, $drupal_target_arr, $tnid, $tvid, $name, FALSE, $drupal_locale_code);
$xml = $xml_targets[0];
$eck_entity = $xml_targets[1];
if (!$xml) {
watchdog(GLOBALLINK_MODULE, 'Cannot create XML. Skipping nid - %nid', array(
'%nid' => $nid,
), WATCHDOG_CRITICAL);
continue;
}
$globallink = new GlobalLink();
$globallink->nid = $node->nid;
$globallink->vid = $node->vid;
$globallink->title = $node->title;
$globallink->type = $node->type;
$globallink->metadata = GLOBALLINK_ENTITY_TYPE_ENTITY;
$globallink->sourceLocale = $source_locale;
$globallink->targetLocale = $target_arr;
$globallink->sourceXML = $xml;
$globallink->sourceFileName = $name;
$globallink->submissionName = $submission_name;
$globallink->submissionPriority = $submission_priority;
$globallink->dueDate = $due_date;
$globallink->submissionInstructions = $submission_details['instructions'] . "\nSubmitter: " . $submitter;
$globallink_arr[GLOBALLINK_ENTITY_TYPE_ENTITY][] = $globallink;
if (count($eck_entity) > 0) {
$globallink_arr = globallink_send_entity_ref_for_translations($eck_entity, $pd4, $submission_name, $due_date, $project_code, $source_locale, $target_locale_arr, $submission_details, $submission_priority, $globallink_arr);
}
}
return $globallink_arr;
}
function globallink_entity_get_sent_rows_by_nid($nid) {
$result = db_select('globallink_document', 'gd')
->fields('gd')
->condition('entity_type', GLOBALLINK_ENTITY_TYPE_ENTITY, '=')
->condition('object_id', $nid, '=')
->condition('target_status', array(
GLOBALLINK_STATUS_TRANSLATION_SENT,
GLOBALLINK_STATUS_TRANSLATION_COMPLETED,
GLOBALLINK_STATUS_TRANSLATION_ERROR,
), 'IN')
->execute();
$rows = array();
foreach ($result as $row) {
$rows[] = $row;
}
return $rows;
}
function globallink_entity_update_entity($globallink, $t_arr = NULL) {
module_load_include('inc', 'globallink', 'globallink');
$target_nid = '';
$target_vid = '';
try {
if (isset($globallink->targetLocale)) {
$target_lang = globallink_get_drupal_locale_code($globallink->targetLocale);
}
if ($t_arr != NULL) {
$translated_arr = $t_arr;
}
else {
$translated_arr = globallink_entity_get_translated_array($globallink->targetXML, $target_lang);
}
if (isset($translated_arr['nid'])) {
$target_nid = $translated_arr['nid'];
}
if (isset($translated_arr['vid'])) {
$target_vid = $translated_arr['vid'];
}
$globallink->nid = $target_nid;
$globallink->vid = $target_vid;
$node = node_load($target_nid, $target_vid);
if (isset($translated_arr['title'])) {
$node->title = $translated_arr['title'];
}
if (isset($node->title_original) && $node->title != $node->title_original) {
$node->title = $node->title_original;
}
if (!$node || is_null($node) || !is_object($node)) {
$globallink->status = GLOBALLINK_STATUS_TRANSLATION_SOURCE_DELETED;
return;
}
$nodepath = drupal_lookup_path('alias', 'node/' . $target_nid);
if ($nodepath) {
if (!empty($translated_arr['path'])) {
$path = array(
'alias' => $translated_arr['path'],
'source' => 'node/' . $target_nid,
'language' => $target_lang,
);
}
else {
$path = array(
'alias' => $nodepath,
'source' => 'node/' . $target_nid,
'language' => $target_lang,
);
}
if (module_exists('pathauto')) {
$path['pathauto'] = 0;
$node->path['pathauto'] = 0;
}
path_save($path);
}
$node->revision = 1;
if (module_exists('revisioning')) {
$node->is_pending = FALSE;
$node->revision_moderation = FALSE;
}
$node->tpt_skip = TRUE;
if (module_exists('metatag')) {
if (isset($translated_arr['metatag'])) {
$target_metatag_arr = $translated_arr['metatag'];
if (!empty($node->metatags[$node->language])) {
$metatags_names = array_keys($node->metatags[$node->language]);
$n_metatag =& $node->metatags;
foreach ($metatags_names as $name) {
$n_metatag[$target_lang][$name] = $node->metatags[$node->language][$name];
if (isset($target_metatag_arr[$name]) && isset($target_metatag_arr[$name][0])) {
$gl_obj = $target_metatag_arr[$name]['0'];
if (is_object($gl_obj)) {
$translated_content = $gl_obj->translatedContent;
}
else {
$translated_content = $gl_obj;
}
$n_metatag[$target_lang][$name] = array(
'value' => $translated_content,
);
}
}
}
}
}
$success = globallink_entity_save_translated_entity_with_fields($node, $translated_arr, $target_lang);
if ($success) {
$globallink->status = GLOBALLINK_STATUS_TRANSLATION_IMPORTED;
}
else {
$globallink->status = GLOBALLINK_STATUS_TRANSLATION_ERROR;
}
} catch (Exception $e) {
$globallink->status = GLOBALLINK_STATUS_TRANSLATION_ERROR;
watchdog(GLOBALLINK_MODULE, 'Exception - %function - File[%file], Line[%line], Code[%code], Message[%message]', array(
'%function' => __FUNCTION__,
'%file' => $e
->getFile(),
'%line' => $e
->getLine(),
'%code' => $e
->getCode(),
'%message' => $e
->getMessage(),
), WATCHDOG_ERROR);
}
return;
}
function globallink_entity_get_translated_array($xml, $target_lang = LANGUAGE_NONE) {
if (is_null($xml) || !is_string($xml) || $xml == '') {
return array();
}
$dom = new DomDocument();
$dom->preserveWhiteSpace = FALSE;
$dom
->loadXML($xml);
$arr = array();
$contents = $dom
->getElementsByTagName('content');
foreach ($contents as $content) {
if (!is_null($content->attributes)) {
foreach ($content->attributes as $attr_name => $attr_node) {
switch ($attr_name) {
case 'rid':
$arr['rid'] = $attr_node->value;
break;
case 'nid':
$arr['nid'] = $attr_node->value;
break;
case 'vid':
$arr['vid'] = $attr_node->value;
break;
}
}
}
}
$titles = $dom
->getElementsByTagName('title');
foreach ($titles as $title) {
$arr['title'] = $title->nodeValue;
}
$paths = $dom
->getElementsByTagName('path');
foreach ($paths as $path) {
$arr['path'] = $path->nodeValue;
}
$field_image = $dom
->getElementsByTagName('field_image');
foreach ($field_image as $attr) {
$field_image_obj = new GLFieldImage();
if (!is_null($attr->attributes)) {
foreach ($attr->attributes as $attr_name => $attr_node) {
switch ($attr_name) {
case 'type':
if ($attr_node->value == 'title') {
$field_image_obj->title = $attr->nodeValue;
}
if ($attr_node->value == 'alt') {
$field_image_obj->alt = $attr->nodeValue;
}
continue 2;
case 'delta':
$field_image_obj->delta = $attr_node->value;
continue 2;
case 'field_name':
$field_image_obj->field_name = $attr_node->value;
continue 2;
}
}
if (is_null($field_image_obj->delta)) {
$field_image_obj->delta = '0';
}
if (isset($field_image_obj->field_name)) {
if (!isset($arr[$field_image_obj->field_name][$target_lang][$field_image_obj->delta])) {
$arr[$field_image_obj->field_name][$target_lang][$field_image_obj->delta] = new GLFieldImage();
}
if (isset($field_image_obj->title)) {
$arr[$field_image_obj->field_name][$target_lang][$field_image_obj->delta]->title = $field_image_obj->title;
}
if (isset($field_image_obj->alt)) {
$arr[$field_image_obj->field_name][$target_lang][$field_image_obj->delta]->alt = $field_image_obj->alt;
}
}
else {
$arr[$field_image_obj->field_name][$target_lang][$field_image_obj->delta] = $field_image_obj;
}
}
}
$fields = $dom
->getElementsByTagName('field');
foreach ($fields as $field) {
$field_obj = new GLField();
$field_obj->type = 'field';
$field_obj->translatedContent = $field->nodeValue;
if (!is_null($field->attributes)) {
foreach ($field->attributes as $attr_name => $attr_node) {
switch ($attr_name) {
case 'entity_type':
$field_obj->entityType = $attr_node->value;
continue 2;
case 'content_type':
$field_obj->contentType = $attr_node->value;
continue 2;
case 'parent_fc':
$field_obj->parentFCName = $attr_node->value;
continue 2;
case 'bundle':
$field_obj->bundle = $attr_node->value;
continue 2;
case 'entity_id':
$field_obj->entityId = $attr_node->value;
continue 2;
case 'field_name':
$field_obj->fieldName = $attr_node->value;
continue 2;
case 'label':
$field_obj->fieldLabel = $attr_node->value;
continue 2;
case 'delta':
$field_obj->delta = $attr_node->value;
continue 2;
case 'format':
$field_obj->format = $attr_node->value;
continue 2;
}
}
if (is_null($field_obj->entityId)) {
$field_obj->entityId = '0';
}
if (is_null($field_obj->bundle)) {
$field_obj->bundle = $field_obj->fieldName;
}
if (is_null($field_obj->delta)) {
$field_obj->delta = '0';
}
if ($field_obj->entityType == GLOBALLINK_ENTITY_TYPE_NODE) {
$arr[$field_obj->fieldName][$target_lang][$field_obj->delta] = $field_obj;
}
else {
$arr['field_collection'][$field_obj->parentFCName][$field_obj->bundle][$field_obj->entityId][$field_obj->fieldName][$target_lang][$field_obj->delta] = $field_obj;
}
}
}
$metatags = $dom
->getElementsByTagName('metatag');
foreach ($metatags as $metatag) {
$metatag_obj = new GLField();
$metatag_obj->type = 'metatag';
$metatag_obj->translatedContent = $metatag->nodeValue;
if (!is_null($metatag->attributes)) {
foreach ($metatag->attributes as $attr_name => $attr_node) {
switch ($attr_name) {
case 'entity_type':
$metatag_obj->entityType = $attr_node->value;
continue 2;
case 'content_type':
$field_obj->contentType = $attr_node->value;
continue 2;
case 'bundle':
$field_obj->bundle = $attr_node->value;
continue 2;
case 'entity_id':
$metatag_obj->entityId = $attr_node->value;
continue 2;
case 'name':
$metatag_obj->fieldName = $attr_node->value;
continue 2;
case 'label':
$metatag_obj->fieldLabel = $attr_node->value;
continue 2;
}
}
if (is_null($metatag_obj->entityId)) {
$metatag_obj->entityId = '0';
}
if (is_null($metatag_obj->bundle)) {
$metatag_obj->bundle = $metatag_obj->fieldName;
}
$arr['metatag'][$metatag_obj->bundle][$metatag_obj->entityId] = $metatag_obj;
}
}
return $arr;
}
function globallink_entity_get_active_submission_by_nid($nid) {
$query = db_select('globallink_document', 'gd');
$query
->join('globallink_submission', 'gs', 'gd.submission_rid = gs.rid');
$query
->condition('gd.object_id', $nid, '=');
$query
->condition('gd.entity_type', GLOBALLINK_ENTITY_TYPE_ENTITY, '=');
$or = db_or();
$or
->condition('gd.target_status', GLOBALLINK_STATUS_TRANSLATION_SENT, '=');
$or
->condition('gd.target_status', GLOBALLINK_STATUS_TRANSLATION_COMPLETED, '=');
$or
->condition('gd.target_status', GLOBALLINK_STATUS_TRANSLATION_ERROR, '=');
$query
->condition($or);
$query
->fields('gd');
$query
->fields('gs', array(
'submission',
'source_lang_code',
'source_lang_name',
'sub_target_lang_code',
'sub_target_lang_name',
));
$results = $query
->execute()
->fetchAll();
return $results;
}
function globallink_entity_save(&$node) {
try {
node_save($node);
return TRUE;
} catch (Exception $e) {
watchdog(GLOBALLINK_MODULE, 'Exception - %function - File[%file], Line[%line], Code[%code], Message[%message]', array(
'%function' => __FUNCTION__,
'%file' => $e
->getFile(),
'%line' => $e
->getLine(),
'%code' => $e
->getCode(),
'%message' => $e
->getMessage(),
), WATCHDOG_ERROR);
return FALSE;
}
}
function globallink_entity_save_translated_entity_with_fields(&$node, $translation_arr, $target_lang) {
$config_fields = globallink_get_config_fields($node->type);
foreach ($config_fields as $field_name => $field_info) {
$source_lang = $node->language;
if ($field_info->field_type == 'image' && $field_info->translatable == 1) {
if (isset($node->{$field_name}[$source_lang])) {
$node->{$field_name}[$target_lang] = $node->{$field_name}[$source_lang];
}
}
else {
if ($field_info->field_type == 'entityreference' && $field_info->translatable == 1) {
if (isset($node->{$field_name}[$source_lang])) {
$node->{$field_name}[$target_lang] = $node->{$field_name}[$source_lang];
}
}
}
}
$nonconfigs = field_info_instances('node', $node->type);
foreach ($nonconfigs as $key => $value) {
if (!array_key_exists($value['field_name'], $config_fields)) {
$source_lang = $node->language;
if (isset($node->{$value['field_name']}[$source_lang])) {
$node->{$value['field_name']}[$target_lang] = $node->{$value['field_name']}[$source_lang];
}
}
}
foreach ($translation_arr as $field => $values) {
if (!is_array($values)) {
continue;
}
$body_summary = '';
$field_def = field_info_field($field);
if ($field_def['type'] == 'text_with_summary' && isset($translation_arr[$field . '@summary'])) {
$body_summary = $translation_arr[$field . '@summary'][$target_lang];
}
foreach ($values as $target => $arr_value) {
foreach ($arr_value as $obj) {
if ($field_def['type'] == 'image') {
if (is_object($obj)) {
$delta = key($node->{$field}[$target]);
if (isset($obj->title)) {
$node->{$field}[$target][$delta]['title'] = $obj->title;
}
if (isset($obj->alt)) {
$node->{$field}[$target][$delta]['alt'] = $obj->alt;
}
}
}
if (!isset($obj->translatedContent)) {
continue;
}
$format = '';
if (!empty($obj->format)) {
$format = $obj->format;
}
if (isset($node->{$field}[$target])) {
if (strpos($field, '@summary') == false) {
if ($field_def['type'] == 'text_with_summary' && !empty($body_summary)) {
$node->{$field}[$target][$obj->delta] = array(
'value' => $obj->translatedContent,
'summary' => $body_summary[$obj->delta]->translatedContent,
'format' => $format,
);
}
else {
$node->{$field}[$target][$obj->delta] = array(
'value' => $obj->translatedContent,
'format' => $format,
);
}
}
}
else {
if (strpos($field, '@summary') == false) {
if ($field_def['type'] == 'text_with_summary' && !empty($body_summary)) {
$node->{$field}[$target] = array(
$obj->delta => array(
'value' => $obj->translatedContent,
'summary' => $body_summary[$obj->delta]->translatedContent,
'format' => $format,
),
);
}
else {
$node->{$field}[$target] = array(
$obj->delta => array(
'value' => $obj->translatedContent,
'format' => $format,
),
);
}
}
}
}
}
}
$is_hook_enabled = variable_get('globallink_implementation_type', 0);
if ($is_hook_enabled == 1) {
globallink_update_node_hook($node, $node);
}
$handler = entity_translation_get_handler('node', $node);
$translation = array(
'entity_type' => GLOBALLINK_ENTITY_TYPE_NODE,
'entity_id' => $node->nid,
'language' => $target_lang,
'source' => $node->language,
'uid' => $node->uid,
'status' => variable_get('globallink_publish_node', 0),
'translate' => 0,
'created' => $node->created,
'changed' => $node->changed,
);
if (module_exists('field_collection') && isset($translation_arr['field_collection'])) {
globallink_entity_save_field_collections($node, $translation_arr, $target_lang);
}
$node->revision = variable_get('globallink_entity_create_revisions', 0);
$handler
->setTranslation($translation, $node);
$handler
->saveTranslations();
$success = globallink_entity_save($node);
return $success;
}
function globallink_entity_save_field_collections($node, $t_arr, $target_lang) {
$field_arr = field_info_instances(GLOBALLINK_ENTITY_TYPE_NODE, $node->type);
$keys = array_keys($field_arr);
foreach ($keys as $field) {
$items = field_get_items(GLOBALLINK_ENTITY_TYPE_NODE, $node, $field);
if ($items) {
$field_def = field_read_field($field);
if ($field_def['type'] == 'field_collection') {
if (isset($t_arr['field_collection'][$field])) {
foreach ($items as $entity_id_arr) {
if (isset($entity_id_arr['value'])) {
$fc_entity_id = $entity_id_arr['value'];
globallink_entity_save_field_collections_recursively(GLOBALLINK_ENTITY_TYPE_NODE, $node, $fc_entity_id, $t_arr['field_collection'][$field], $target_lang);
}
}
}
}
}
}
}
function globallink_entity_save_field_collections_recursively($entity_type, $host_entity, $fc_entity_id, $translated_fc_arr, $target_lang) {
$field_collection_item_entity = entity_load_unchanged('field_collection_item', $fc_entity_id);
if (!$field_collection_item_entity) {
return;
}
$field_collection_name = $field_collection_item_entity->field_name;
$field_collection_item_array = get_object_vars($field_collection_item_entity);
$arr = array_keys($field_collection_item_array);
$new_field_collection_arr = array(
'field_name' => $field_collection_name,
);
foreach ($arr as $key) {
$fc_field_def = field_read_field($key);
if (!empty($fc_field_def) && isset($fc_field_def['type'])) {
switch ($fc_field_def['type']) {
case 'list_boolean':
case 'file':
case 'taxonomy_term_reference':
continue 2;
break;
case 'field_collection':
if (isset($field_collection_item_array[$key]) && isset($field_collection_item_array[$key][$target_lang])) {
$field_data_arr = $field_collection_item_array[$key][$target_lang];
$new_field_collection_arr[$key][$target_lang] = $field_data_arr;
}
break;
default:
$lang = key($field_collection_item_array[$key]);
if (!isset($field_collection_item_array[$key])) {
break;
}
if (!isset($field_collection_item_array[$key][$lang])) {
break;
}
$field_data_arr = $field_collection_item_array[$key][$lang];
foreach ($field_data_arr as $delta => $field_data) {
if (isset($translated_fc_arr[$field_collection_name][$fc_entity_id][$key][$target_lang][$delta])) {
$gl_obj = $translated_fc_arr[$field_collection_name][$fc_entity_id][$key][$target_lang][$delta];
if (is_object($gl_obj)) {
$translated_content = $gl_obj->translatedContent;
}
else {
$translated_content = $gl_obj;
}
$new_field_collection_arr[$key][$target_lang][$delta] = array(
'value' => $translated_content,
);
if (isset($field_data['format'])) {
$new_field_collection_arr[$key][$target_lang][$delta]['format'] = $field_data['format'];
}
}
}
}
}
}
if ($entity_type != GLOBALLINK_ENTITY_TYPE_NODE) {
$host_entity->tpt_skip = TRUE;
}
$new_entity = entity_create('field_collection_item', $new_field_collection_arr);
$new_entity
->setHostEntity($entity_type, $host_entity, $target_lang);
$new_entity
->save(TRUE);
field_attach_presave($entity_type, $host_entity);
field_attach_update($entity_type, $host_entity);
foreach ($arr as $key) {
$fc_field_def = field_read_field($key);
if ($fc_field_def && !empty($fc_field_def) && isset($fc_field_def['type'])) {
if ($fc_field_def['type'] == 'field_collection') {
$items = field_get_items('field_collection_item', $field_collection_item_entity, $key);
if ($items) {
foreach ($items as $entity_id_arr) {
if (isset($entity_id_arr['value'])) {
$fc_entity_id = $entity_id_arr['value'];
globallink_entity_save_field_collections_recursively('field_collection_item', $new_entity, $fc_entity_id, $translated_fc_arr, $target_lang);
}
}
}
}
}
}
}
function globallink_entity_get_xml($node, $target_arr, $tnid = NULL, $tvid = NULL, &$name = '', $for_display = FALSE, $source_lang = '') {
if (is_null($node)) {
return GLOBALLINK_STATUS_TRANSLATION_SOURCE_DELETED;
}
elseif (!$node) {
return GLOBALLINK_STATUS_TRANSLATION_SOURCE_DELETED;
}
if ($node && is_object($node)) {
if ($node->language != 'en') {
$name = 'Node_' . $node->nid . '_Non_English' . $name;
}
else {
$name = globallink_format_file_name($node->title) . '_' . $node->nid . $name;
}
$xml = globallink_entity_generate_xml_document($node, $target_arr, $tnid, $tvid, $for_display, $source_lang);
return $xml;
}
return GLOBALLINK_STATUS_TRANSLATION_SOURCE_DELETED;
}
function globallink_entity_generate_xml_document($node, $target_arr, $tnid = NULL, $tvid = NULL, $for_display = FALSE, $source_lang = '') {
$is_hook_enabled = variable_get('globallink_implementation_type', 0);
$entityref = array();
try {
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = TRUE;
$root = $dom
->createElement('content');
$nid = $dom
->createAttribute('nid');
if ($tnid != NULL) {
$nid->value = $tnid;
}
else {
$nid->value = $node->nid;
}
$root
->appendChild($nid);
$vid = $dom
->createAttribute('vid');
if ($tvid != NULL) {
$vid->value = $tvid;
}
else {
$vid->value = $node->vid;
}
$root
->appendChild($vid);
$url = $dom
->createAttribute('pageUrl');
$path = path_load(array(
'source' => 'node/' . $node->nid,
'language' => $source_lang,
));
if (isset($path['alias'])) {
$url->value = url($path['alias'], array(
'absolute' => TRUE,
));
}
else {
$url->value = url('node/' . $node->nid, array(
'absolute' => TRUE,
));
}
if (!empty($path)) {
globallink_insert_child_element($dom, $root, 'path', $path['alias']);
}
$root
->appendChild($url);
$dom
->appendChild($root);
$field_arr = field_info_instances(GLOBALLINK_ENTITY_TYPE_NODE, $node->type);
$keys = array_keys($field_arr);
foreach ($keys as $field) {
$field_def = field_read_field($field);
$items = field_get_items(GLOBALLINK_ENTITY_TYPE_NODE, $node, $field, $source_lang);
if ($items) {
$parent_fc = '';
if ($field_def['type'] == 'field_collection') {
$parent_fc = $field;
if (isset($items[0]['revision_id'])) {
entity_load('field_collection_item', array(
$items[0]['value'],
), array(
'revision_id' => $items[0]['revision_id'],
));
}
}
$entityref[] = globallink_entity_traverse_fields_and_field_collections(GLOBALLINK_ENTITY_TYPE_NODE, $node->type, $parent_fc, $node->type, $node->nid, $items, $field, $field_def, $dom, $root, $node, $target_arr, $is_hook_enabled);
}
}
if (module_exists('metatag')) {
if (isset($node->metatags)) {
$ignore_metatags = [
'geo.position',
'icbm',
'rating',
'referrer',
'revisit-after',
'content-language',
];
if ($for_display) {
if (!empty($node->metatags[$node->language])) {
$metatags = $node->metatags[$node->language];
foreach ($metatags as $name => $value) {
if (isset($value['value']) && !is_array($value['value'])) {
if (in_array($name, $ignore_metatags)) {
continue;
}
globallink_insert_child_element($dom, $root, 'metatag', $value['value'], array(
'entity_type' => GLOBALLINK_ENTITY_TYPE_NODE,
'name' => $name,
'label' => 'Metatag - ' . ucwords($name),
));
}
}
}
}
elseif (globallink_is_field_configured_for_translation(GLOBALLINK_ENTITY_TYPE_NODE, $node->type, 'metatags', $node->type)) {
if (!empty($node->metatags[$node->language])) {
$metatags = $node->metatags[$node->language];
foreach ($metatags as $name => $value) {
if (isset($value['value']) && !is_array($value['value'])) {
if (in_array($name, $ignore_metatags)) {
continue;
}
globallink_insert_child_element($dom, $root, 'metatag', $value['value'], array(
'entity_type' => GLOBALLINK_ENTITY_TYPE_NODE,
'name' => $name,
'label' => 'Metatag - ' . ucwords($name),
));
}
}
}
}
}
}
} catch (Exception $e) {
watchdog(GLOBALLINK_MODULE, 'Exception - %function - File[%file], Line[%line], Code[%code], Message[%message]', array(
'%function' => __FUNCTION__,
'%file' => $e
->getFile(),
'%line' => $e
->getLine(),
'%code' => $e
->getCode(),
'%message' => $e
->getMessage(),
), WATCHDOG_ERROR);
throw $e;
}
$root_element = $dom
->getElementsByTagName('content')
->item(0);
if (!$root_element
->hasChildNodes() && count($entityref) == 0) {
return FALSE;
}
$result = array(
$dom
->saveXML(),
$entityref,
);
return $result;
}
function globallink_entity_traverse_fields_and_field_collections($entity_type, $content_type, $parent_fc, $bundle, $entity_id, $items, $field, $field_def, $dom, $root, $source_node, $target_arr, $is_hook_enabled = 0) {
if (!$items) {
return;
}
switch ($field_def['type']) {
case 'list_boolean':
case 'file':
case 'taxonomy_term_reference':
return;
}
if ($field_def['type'] != 'field_collection') {
if ($is_hook_enabled == 0) {
if (!globallink_is_field_configured_for_translation($entity_type, $bundle, $field, $content_type)) {
return;
}
foreach ($items as $delta => $item) {
$field_info_all = field_info_field($field);
$sum_field = '';
if ($field_info_all['type'] == 'text_with_summary' && globallink_is_field_configured_for_translation($entity_type, $bundle, $field . '@summary', $content_type)) {
$sum_field = $field . '@summary';
}
if (isset($item['summary']) && is_string($item['summary']) && !empty($sum_field)) {
if (globallink_is_field_configured_for_translation($entity_type, $bundle, $field, $content_type)) {
$f_label = $field . ' Summary';
$f_value = $item['summary'];
$f_format = isset($item['format']) && !is_null($item['format']) ? $item['format'] : '';
globallink_insert_child_element($dom, $root, 'field', $f_value, array(
'entity_type' => $entity_type,
'content_type' => $content_type,
'parent_fc' => $parent_fc,
'bundle' => $bundle,
'entity_id' => $entity_id,
'field_name' => $sum_field,
'label' => $f_label,
'delta' => $delta,
'format' => $f_format,
));
}
}
if (isset($item['value']) && is_string($item['value'])) {
$f_label = field_info_instance($entity_type, $field, $bundle);
$f_value = $item['value'];
$f_format = isset($item['format']) && !is_null($item['format']) ? $item['format'] : '';
globallink_insert_child_element($dom, $root, 'field', $f_value, array(
'entity_type' => $entity_type,
'content_type' => $content_type,
'parent_fc' => $parent_fc,
'bundle' => $bundle,
'entity_id' => $entity_id,
'field_name' => $field,
'label' => $f_label['label'],
'delta' => $delta,
'format' => $f_format,
));
}
elseif ($field_def['type'] == 'link_field' && isset($item['title']) && is_string($item['title'])) {
$f_label = field_info_instance($entity_type, $field, $bundle);
$f_value = $item['title'];
$f_format = isset($item['format']) && !is_null($item['format']) ? $item['format'] : '';
globallink_insert_child_element($dom, $root, 'field', $f_value, array(
'entity_type' => $entity_type,
'content_type' => $content_type,
'parent_fc' => $parent_fc,
'bundle' => $bundle,
'entity_id' => $entity_id,
'field_name' => $field,
'label' => $f_label['label'],
'delta' => $delta,
'format' => $f_format,
));
}
elseif ($field_def['type'] == 'image') {
$f_label = field_info_instance($entity_type, $field, $bundle);
if (isset($item['alt'])) {
$f_value = $item['alt'];
globallink_insert_child_element($dom, $root, 'field_image', $f_value, array(
'field_name' => $field,
'type' => 'alt',
'delta' => $delta,
));
}
if (isset($item['title'])) {
$f_value = $item['title'];
globallink_insert_child_element($dom, $root, 'field_image', $f_value, array(
'field_name' => $field,
'type' => 'title',
'delta' => $delta,
));
}
}
elseif ($field_def['type'] == 'entityreference' && isset($item['target_id'])) {
$reference_type = $field_info_all['settings']['target_type'];
$reference_id = $item['target_id'];
if (!isset($_SESSION['eck'][$reference_type][$reference_id]) || $_SESSION['eck'][$reference_type][$reference_id] != $reference_id) {
$_SESSION['eck'][$reference_type][$reference_id] = $reference_id;
$entity_ref[$reference_type][] = $reference_id;
}
else {
watchdog(GLOBALLINK_MODULE, 'Skipping entity Id - %id as it is in active submission', array(
'%id' => $reference_id,
), WATCHDOG_DEBUG);
}
}
}
}
else {
if (!globallink_is_field_translatable($source_node, $field, $target_arr)) {
return;
}
foreach ($items as $delta => $item) {
$field_info_all = field_info_field($field);
$sum_field = '';
if ($field_info_all['type'] == 'text_with_summary' && globallink_is_field_configured_for_translation($entity_type, $bundle, $field . '@summary', $content_type)) {
$sum_field = $field . '@summary';
}
if (isset($item['summary']) && is_string($item['summary']) && !empty($sum_field)) {
if (globallink_is_field_configured_for_translation($entity_type, $bundle, $field, $content_type)) {
$f_label = $field . ' Summary';
$f_value = $item['summary'];
$f_format = isset($item['format']) && !is_null($item['format']) ? $item['format'] : '';
globallink_insert_child_element($dom, $root, 'field', $f_value, array(
'entity_type' => $entity_type,
'content_type' => $content_type,
'parent_fc' => $parent_fc,
'bundle' => $bundle,
'entity_id' => $entity_id,
'field_name' => $sum_field,
'label' => $f_label,
'delta' => $delta,
'format' => $f_format,
));
}
}
if (isset($item['value']) && is_string($item['value'])) {
$f_label = field_info_instance($entity_type, $field, $bundle);
$f_value = $item['value'];
$f_format = isset($item['format']) && !is_null($item['format']) ? $item['format'] : '';
globallink_insert_child_element($dom, $root, 'field', $f_value, array(
'entity_type' => $entity_type,
'content_type' => $content_type,
'parent_fc' => $parent_fc,
'bundle' => $bundle,
'entity_id' => $entity_id,
'field_name' => $field,
'label' => $f_label['label'],
'delta' => $delta,
'format' => $f_format,
));
}
elseif ($field_def['type'] == 'link_field' && isset($item['title']) && is_string($item['title'])) {
$f_label = field_info_instance($entity_type, $field, $bundle);
$f_value = $item['title'];
$f_format = isset($item['format']) && !is_null($item['format']) ? $item['format'] : '';
globallink_insert_child_element($dom, $root, 'field', $f_value, array(
'entity_type' => $entity_type,
'content_type' => $content_type,
'parent_fc' => $parent_fc,
'bundle' => $bundle,
'entity_id' => $entity_id,
'field_name' => $field,
'label' => $f_label['label'],
'delta' => $delta,
'format' => $f_format,
));
}
elseif ($field_def['type'] == 'entityreference' && isset($item['target_id'])) {
$reference_type = $field_info_all['settings']['target_type'];
$reference_id = array(
$item['target_id'],
);
if (!isset($_SESSION['eck'][$reference_type][$reference_id]) || $_SESSION['eck'][$reference_type][$reference_id] != $reference_id) {
$_SESSION['eck'][$reference_type][$reference_id] = $reference_id;
$entity_ref[$reference_type][] = $reference_id;
}
else {
watchdog(GLOBALLINK_MODULE, 'Skipping entity Id - %id as it is in active submission', array(
'%id' => $reference_id,
), WATCHDOG_DEBUG);
}
}
}
}
}
else {
if (!module_exists('field_collection')) {
return;
}
foreach ($items as $entity_id_arr) {
if (!isset($entity_id_arr['value'])) {
continue;
}
$fc_entity_id = $entity_id_arr['value'];
$field_collection_item_entity_arr = array();
if (isset($entity_id_arr['revision_id'])) {
$field_collection_item_entity_arr = entity_load('field_collection_item', array(
$fc_entity_id,
), array(
'revision_id' => $entity_id_arr['revision_id'],
));
}
else {
$field_collection_item_entity_arr = entity_load('field_collection_item', array(
$fc_entity_id,
));
}
if (!$field_collection_item_entity_arr || !is_array($field_collection_item_entity_arr) || sizeof($field_collection_item_entity_arr) < 1) {
continue;
}
$field_collection_item_entity = $field_collection_item_entity_arr[$fc_entity_id];
$field_collection_name = $field_collection_item_entity->field_name;
$field_collection_item_array = get_object_vars($field_collection_item_entity);
$arr = array_keys($field_collection_item_array);
foreach ($arr as $key) {
$fc_field_def = field_read_field($key);
if (!$fc_field_def || empty($fc_field_def) || !isset($fc_field_def['type'])) {
continue;
}
switch ($fc_field_def['type']) {
case 'list_boolean':
case 'file':
case 'taxonomy_term_reference':
continue 2;
}
$fc_item = field_get_items('field_collection_item', $field_collection_item_entity, $key);
if ($fc_item) {
globallink_entity_traverse_fields_and_field_collections('field_collection_item', $content_type, $parent_fc, $field_collection_name, $fc_entity_id, $fc_item, $key, $fc_field_def, $dom, $root, $source_node, $target_arr, $is_hook_enabled);
}
}
}
}
return $entity_ref;
}