eZContentOperationCollection::copyTranslations PHP Method

copyTranslations() public static method

*! Copies missing translations from published version to the draft.
public static copyTranslations ( $objectID, $versionNum )
    public static function copyTranslations($objectID, $versionNum)
    {
        $object = eZContentObject::fetch($objectID);
        if (!$object instanceof eZContentObject) {
            return array('status' => eZModuleOperationInfo::STATUS_CANCELLED);
        }
        $publishedVersionNum = $object->attribute('current_version');
        if (!$publishedVersionNum) {
            return;
        }
        $publishedVersion = $object->version($publishedVersionNum);
        $publishedVersionTranslations = $publishedVersion->translations();
        $publishedLanguages = eZContentLanguage::languagesByMask($object->attribute('language_mask'));
        $publishedLanguageCodes = array_keys($publishedLanguages);
        $version = $object->version($versionNum);
        $versionTranslationList = array_keys(eZContentLanguage::languagesByMask($version->attribute('language_mask')));
        foreach ($publishedVersionTranslations as $translation) {
            $translationLanguageCode = $translation->attribute('language_code');
            if (in_array($translationLanguageCode, $versionTranslationList) || !in_array($translationLanguageCode, $publishedLanguageCodes)) {
                continue;
            }
            foreach ($translation->objectAttributes() as $attribute) {
                $clonedAttribute = $attribute->cloneContentObjectAttribute($versionNum, $publishedVersionNum, $objectID);
                $clonedAttribute->sync();
            }
        }
        $version->updateLanguageMask();
    }

Usage Example

 /**
  * Refreshes content object attributes for current fields.
  * Mandatory to update good attributes when publishing
  * @param eZContentObjectVersion $version Draft to be published
  * @see SQLIContentPublisher::publish()
  */
 public function refreshDataMap(eZContentObjectVersion $version)
 {
     $contentObject = $version->contentObject();
     // Copy missing translations from published version to current draft if needed
     eZContentOperationCollection::copyTranslations($contentObject->attribute('id'), $version->attribute('version'));
     $dataMap = $contentObject->fetchDataMap($version->attribute('version'), $this->language);
     foreach ($this->fields as $fieldName => $field) {
         if (isset($dataMap[$fieldName])) {
             $field->setRawAttribute($dataMap[$fieldName]);
         } else {
             eZDebug::writeWarning(__METHOD__ . ' => Attribute "' . $fieldName . '" not set in data map. Cannot refresh field.', 'SQLIImport');
         }
     }
 }