You are here

function lingotek_process_field_collection_xml in Lingotek Translation 7.7

Same name and namespace in other branches
  1. 7.6 lingotek.remote.inc \lingotek_process_field_collection_xml()
1 call to lingotek_process_field_collection_xml()
lingotek_process_entity_xml in ./lingotek.remote.inc

File

./lingotek.remote.inc, line 501

Code

function lingotek_process_field_collection_xml($xml, $entity_type, &$entity, $field_name, $delta, $langcode, $node_based_translation) {
  $field_info = field_info_field($field_name);
  $curr_field_data =& $entity->{$field_name};
  $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[$default_language][$delta]['value'];
  }
  else {
    if (!$node_based_translation) {

      // The field-collection field must be empty.
      return;
    }

    // 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_info['field_name'],
    ));

    // Grandfather fields (anything that's an array) from the source field collection
    $original_entity = lingotek_entity_load_single($entity_type, $entity->tnid);
    $original_field_collection = lingotek_entity_load_single('field_collection_item', $original_entity->{$field_name}[LANGUAGE_NONE][$delta]['value']);
    if (!empty($original_field_collection)) {
      foreach ($original_field_collection as $k => $v) {
        if (is_array($v)) {
          $field_collection_item->{$k} = $v;
        }
      }
    }
    $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
    return;
  }
  $field_collection_item->type = $field_info['field_name'];
  $field_collection_item->language = $entity->language;

  // RECURSION FOR FIELD COLLECTIONS
  lingotek_process_entity_xml($xml, $field_collection_item, 'field_collection_item', $langcode, $node_based_translation);
}