diff --git a/field_collection.install b/field_collection.install index 91edfbb..bb82014 100644 --- a/field_collection.install +++ b/field_collection.install @@ -281,3 +281,30 @@ function field_collection_update_7004() { } } } + +/** + * Update any fields inside field collections that were already set up to use + * Entity Translation before the patch for it was applied. + */ +function field_collection_update_7005() { + $fc_results = array(); + foreach (field_info_fields() as $f_name => $field) { + if ($field['translatable'] == 1 && isset($field['bundles']['field_collection_item'])) { + $query = new EntityFieldQuery(); + $query->entityCondition('entity_type', 'field_collection_item') + ->fieldLanguageCondition($f_name, LANGUAGE_NONE); + $result = $query->execute(); + if (isset($result['field_collection_item'])) { + $fc_results = $fc_results + $result['field_collection_item']; + } + } + } + if (count($fc_results)) { + $ids = array_keys($fc_results); + $field_collection_items = entity_load('field_collection_item', $ids); + foreach ($field_collection_items as $item) { + $item->copy_translations(LANGUAGE_NONE); + $item->save(); + } + } +} diff --git a/field_collection.module b/field_collection.module index 19e365f..009bd24 100644 --- a/field_collection.module +++ b/field_collection.module @@ -543,18 +543,7 @@ class FieldCollectionItemEntity extends Entity { // Copy the values of translatable fields for a new field collection item. if (field_collection_item_is_translatable() && !empty($this->is_new) && $this->langcode == LANGUAGE_NONE) { - $host_et_handler = entity_translation_get_handler($this->hostEntityType(), $this->hostEntity()); - $host_languages = array_keys($host_et_handler->getTranslations()->data); - $target_languages = array_diff($host_languages, array($host_et_handler->getLanguage())); - $et_handler = entity_translation_get_handler($this->entityType(), $this); - $fields = array_keys(field_info_instances('field_collection_item', $this->field_name)); - foreach ($target_languages as $lang_code) { - foreach ($fields as $t_field) { - if (isset($this->{$t_field}[$et_handler->getLanguage()])) { - $this->{$t_field}[$lang_code] = $this->{$t_field}[$et_handler->getLanguage()]; - } - } - } + $this->copy_translations(); } // Only save directly if we are told to skip saving the host entity. Else, @@ -588,6 +577,29 @@ class FieldCollectionItemEntity extends Entity { } } + public function copy_translations($source_language = NULL) { + $host_et_handler = entity_translation_get_handler($this->hostEntityType(), $this->hostEntity()); + $host_languages = array_keys($host_et_handler->getTranslations()->data); + if (empty($host_languages)) { + $host_languages = array(entity_language($this->hostEntityType(), $this->hostEntity())); + } + $source_language = isset($source_language) ? $source_language : $host_et_handler->getLanguage(); + $target_languages = array_diff($host_languages, array($source_language)); + $et_handler = entity_translation_get_handler($this->entityType(), $this); + $fields = array_keys(field_info_instances('field_collection_item', $this->field_name)); + + foreach ($fields as $t_field) { + foreach ($target_languages as $lang_code) { + if (isset($this->{$t_field}[$source_language])) { + $this->{$t_field}[$lang_code] = $this->{$t_field}[$source_language]; + } + } + if ($source_language == LANGUAGE_NONE && count($this->{$t_field}) > 1) { + $this->{$t_field}[$source_language] = NULL; + } + } + } + /** * Deletes the field collection item and the reference in the host entity. */