You are here

function field_collection_entity_translation_insert in Field collection 7

Implements hook_entity_translation_insert().

File

./field_collection.module, line 2191
Module implementing field collection field type.

Code

function field_collection_entity_translation_insert($entity_type, $entity, $translation, $values = array()) {

  // Check if some of the values inserted are of a field_collection field.
  if (!empty($values)) {
    foreach ($values as $field_name => $value) {
      $field = field_info_field($field_name);
      if ($field['type'] === 'field_collection') {

        // We have found a field collection.
        $language = $translation['language'];
        $source_language = $translation['source'];
        if (!empty($value[$language])) {
          $source_items = !empty($entity->{$field_name}[$source_language]) ? field_collection_field_item_to_ids($entity->{$field_name}[$source_language]) : array();
          foreach ($value[$language] as $delta => $field_value) {

            // Check if this field collection item belongs to the source language.
            if (!isset($field_value['entity']) && ($fc_entity = field_collection_field_get_entity($field_value)) && in_array($fc_entity->item_id, $source_items)) {

              // Clone the field collection item.
              $new_fc_entity = clone $fc_entity;
              $new_fc_entity->item_id = NULL;
              $new_fc_entity->revision_id = NULL;
              $new_fc_entity->is_new = TRUE;

              // Set the new entity for saving it later.
              $entity->{$field_name}[$language][$delta]['entity'] = $new_fc_entity;
            }
          }
        }
      }
    }
  }
}