eZContentOperationCollection::updateInitialLanguage PHP Method

updateInitialLanguage() public static method

Updates an contentobject's initial language
public static updateInitialLanguage ( integer $objectID, integer $newInitialLanguageID ) : array
$objectID integer
$newInitialLanguageID integer
return array An array with operation status, always true
    public static function updateInitialLanguage($objectID, $newInitialLanguageID)
    {
        $object = eZContentObject::fetch($objectID);
        $language = eZContentLanguage::fetch($newInitialLanguageID);
        if ($language and !$language->attribute('disabled')) {
            $object->setAttribute('initial_language_id', $newInitialLanguageID);
            $objectName = $object->name(false, $language->attribute('locale'));
            $object->setAttribute('name', $objectName);
            $object->store();
            if ($object->isAlwaysAvailable()) {
                $object->setAlwaysAvailableLanguageID($newInitialLanguageID);
            }
            $nodes = $object->assignedNodes();
            foreach ($nodes as $node) {
                $node->updateSubTreePath();
            }
        }
        eZContentCacheManager::clearContentCacheIfNeeded($objectID);
        return array('status' => true);
    }

Usage Example

 function runOperation(&$node)
 {
     $object = $node->attribute('object');
     $object_id = $object->attribute('id');
     $objectLocales = $object->attribute('available_languages');
     // If the alternative locale does not exist for object, create it
     if (!in_array($this->altLocale, $objectLocales)) {
         // The only translation is in locate to be removed - create a version in another locale first.
         echo "Copying the single translation in " . $this->remLocale . " to " . $this->altLocale . " so former could be removed.\n";
         $newVersion = $object->createNewVersionIn($this->altLocale, $this->remLocale, false, true, eZContentObjectVersion::STATUS_DRAFT);
         $publishResult = eZOperationHandler::execute('content', 'publish', array('object_id' => $object_id, 'version' => $newVersion->attribute('version')));
         eZContentObject::clearCache();
         $object = eZContentObject::fetch($object_id);
     }
     // Change objects main language to alternative language, if its current main language is to be removed.
     if ($object->attribute('initial_language_code') == $this->remLocale) {
         eZContentObject::clearCache();
         $object = eZContentObject::fetch($object_id);
         echo "Switching initial language to {$this->altLocale} so that " . $this->remLocale . " could be removed.\n";
         $updateResult = eZContentOperationCollection::updateInitialLanguage($object_id, $this->altLangID);
         $object->store();
         eZContentObject::clearCache();
         $object = eZContentObject::fetch($object_id);
     }
     // Now it should be safe to remove translation.
     return $object->removeTranslation($this->remLangID);
 }
All Usage Examples Of eZContentOperationCollection::updateInitialLanguage