diff --git a/eZ/Publish/Core/Persistence/Legacy/Content/FieldHandler.php b/eZ/Publish/Core/Persistence/Legacy/Content/FieldHandler.php index 22c5cc57a0f..5cab8650077 100644 --- a/eZ/Publish/Core/Persistence/Legacy/Content/FieldHandler.php +++ b/eZ/Publish/Core/Persistence/Legacy/Content/FieldHandler.php @@ -132,24 +132,42 @@ public function createNewFields( Content $content ) foreach ( $contentType->fieldDefinitions as $fieldDefinition ) { - foreach ( array_keys( $languageCodes ) as $languageCode ) + foreach ( $languageCodes as $languageCode ) { + // Skip fields passed from struct to handle them separately later if ( isset( $fields[$fieldDefinition->id][$languageCode] ) ) { - $field = $fields[$fieldDefinition->id][$languageCode]; - $this->createNewField( $field, $content ); + continue; } - else if ( $fieldDefinition->isTranslatable - || !isset( $fields[$fieldDefinition->id][$content->versionInfo->contentInfo->mainLanguageCode] ) ) + + // Copy only for untranslatable field and when field in main language exists + if ( !$fieldDefinition->isTranslatable + && isset( $fields[$fieldDefinition->id][$content->versionInfo->contentInfo->mainLanguageCode] ) + ) + { + $field = clone $fields[$fieldDefinition->id][$content->versionInfo->contentInfo->mainLanguageCode]; + $content->fields[] = $field; + $this->copyLegacyField( $field, $languageCode, $content ); + } + // In all other cases create empty field + else { $field = $this->getEmptyField( $fieldDefinition, $languageCode ); + $content->fields[] = $field; $this->createNewField( $field, $content ); } - else + } + } + + // Create fields passed from struct + foreach ( $contentType->fieldDefinitions as $fieldDefinition ) + { + foreach ( $languageCodes as $languageCode ) + { + if ( isset( $fields[$fieldDefinition->id][$languageCode] ) ) { - // Use value from main language code - $field = clone $fields[$fieldDefinition->id][$content->versionInfo->contentInfo->mainLanguageCode]; - $this->copyLegacyField( $field, $languageCode, $content ); + $field = $fields[$fieldDefinition->id][$languageCode]; + $this->createNewField( $field, $content ); } } } @@ -351,7 +369,7 @@ public function updateFields( Content $content, UpdateStruct $updateStruct ) foreach ( $contentType->fieldDefinitions as $fieldDefinition ) { - foreach ( array_keys( $languageCodes ) as $languageCode ) + foreach ( $languageCodes as $languageCode ) { if ( isset( $updateFieldMap[$fieldDefinition->id][$languageCode] ) ) { @@ -378,11 +396,18 @@ public function updateFields( Content $content, UpdateStruct $updateStruct ) else { // Use value from main language code for untranslatable field - $field = clone $contentFieldMap[$fieldDefinition->id][$content->versionInfo->contentInfo->mainLanguageCode]; + if ( isset( $updateFieldMap[$fieldDefinition->id][$content->versionInfo->contentInfo->mainLanguageCode] ) ) + { + $field = clone $updateFieldMap[$fieldDefinition->id][$content->versionInfo->contentInfo->mainLanguageCode]; + } + else + { + $field = clone $contentFieldMap[$fieldDefinition->id][$content->versionInfo->contentInfo->mainLanguageCode]; + } $this->copyLegacyField( $field, $languageCode, $content ); } } - // If existing language for untranslatable field, for which main language is updated, + // If field is not set for existing language and is untranslatable and main language is updated, // also update copied field data else if ( !$fieldDefinition->isTranslatable && isset( $updateFieldMap[$fieldDefinition->id][$content->versionInfo->contentInfo->mainLanguageCode] ) @@ -434,6 +459,11 @@ protected function getFieldMap( array $fields, &$languageCodes = null ) $fieldMap[$field->fieldDefinitionId][$field->languageCode] = $field; } + if ( isset( $languageCodes ) ) + { + $languageCodes = array_keys( $languageCodes ); + } + return $fieldMap; }