diff --git a/field_collection.module b/field_collection.module index ffe3764..19e365f 100644 --- a/field_collection.module +++ b/field_collection.module @@ -536,18 +536,32 @@ class FieldCollectionItemEntity extends Entity { * revision updates might be skipped. Use with care. */ public function save($skip_host_save = FALSE) { - $was_new = !empty($this->is_new); - // Make sure we have a host entity during creation. if (!empty($this->is_new) && !(isset($this->hostEntityId) || isset($this->hostEntity) || isset($this->hostEntityRevisionId))) { throw new Exception("Unable to create a field collection item without a given host 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()]; + } + } + } + } + // Only save directly if we are told to skip saving the host entity. Else, // we always save via the host as saving the host might trigger saving // field collection items anyway (e.g. if a new revision is created). if ($skip_host_save) { - $ret = entity_get_controller($this->entityType)->save($this); + return entity_get_controller($this->entityType)->save($this); } else { $host_entity = $this->hostEntity(); @@ -570,27 +584,8 @@ class FieldCollectionItemEntity extends Entity { else { $host_entity->{$this->field_name}[$this->langcode][] = array('entity' => $this); } - $ret = entity_save($this->hostEntityType, $host_entity); + return entity_save($this->hostEntityType, $host_entity); } - - // Copy the values of translatable fields for a new field collection item. - if (field_collection_item_is_translatable() && $was_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->save(TRUE); - } - - return $ret; } /**