@@ -386,46 +386,76 @@ function createNewObject( $contentObjectAttribute, $name )
386386 return $ newObjectInstance ->attribute ( 'id ' );
387387 }
388388
389- function storeObjectAttribute ( $ attribute )
389+ function storeObjectAttribute ( $ contentObjectAttribute )
390390 {
391- $ content = $ attribute ->content ();
391+ $ content = $ contentObjectAttribute ->content ();
392392 if ( isset ( $ content ['new_object ' ] ) )
393393 {
394- $ newID = $ this ->createNewObject ( $ attribute , $ content ['new_object ' ] );
394+ $ newID = $ this ->createNewObject ( $ contentObjectAttribute , $ content ['new_object ' ] );
395395 // if this is a single element selection mode (radio or dropdown), then the newly created item is the only one selected
396396 if ( $ newID )
397397 {
398398 if ( isset ( $ content ['singleselect ' ] ) )
399399 $ content ['relation_list ' ] = array ();
400- $ content ['relation_list ' ][] = $ this ->appendObject ( $ newID , 0 , $ attribute );
400+ $ content ['relation_list ' ][] = $ this ->appendObject ( $ newID , 0 , $ contentObjectAttribute );
401401 }
402402 unset( $ content ['new_object ' ] );
403- $ attribute ->setContent ( $ content );
403+ $ contentObjectAttribute ->setContent ( $ content );
404404 }
405405
406- $ contentClassAttributeID = $ attribute ->ContentClassAttributeID ;
407- $ contentObjectID = $ attribute ->ContentObjectID ;
408- $ contentObjectVersion = $ attribute ->Version ;
406+ $ contentClassAttributeID = $ contentObjectAttribute ->ContentClassAttributeID ;
407+ $ contentObjectID = $ contentObjectAttribute ->ContentObjectID ;
408+ $ contentObjectVersion = $ contentObjectAttribute ->Version ;
409+ $ languageCode = $ contentObjectAttribute ->attribute ( 'language_code ' );
409410
410- $ obj = $ attribute ->object ();
411- //get eZContentObjectVersion
412- $ currVerobj = $ obj ->version ( $ contentObjectVersion );
411+ /** @var eZContentObject */
412+ $ contentObject = $ contentObjectAttribute ->object ();
413413
414- // create translation List
415- // $translationList will contain for example eng-GB, ita-IT etc.
416- $ translationList = $ currVerobj ->translations ( false );
414+ if ( $ contentObjectAttribute ->ID !== null )
415+ {
416+ // cleanup previous relations
417+ $ contentObject ->removeContentObjectRelation ( false , $ contentObjectVersion , $ contentClassAttributeID , eZContentObject::RELATION_ATTRIBUTE );
417418
418- // get current language_code
419- $ langCode = $ attribute ->attribute ( 'language_code ' );
420- // get count of LanguageCode in translationList
421- $ countTsl = count ( $ translationList );
422- // order by asc
423- sort ( $ translationList );
419+ // if translatable, we need to re-add the relations for other languages of (previously) published version.
420+ $ publishedVersionNo = $ contentObject ->publishedVersion ();
421+ if ( $ contentObjectAttribute ->contentClassAttributeCanTranslate () && $ publishedVersionNo > 0 )
422+ {
423+ $ existingRelations = array ();
424424
425- // check if previous relation(s) should first be removed
426- if ( !$ attribute ->contentClassAttributeCanTranslate () )
427- {
428- $ obj ->removeContentObjectRelation ( false , $ contentObjectVersion , $ contentClassAttributeID , eZContentObject::RELATION_ATTRIBUTE );
425+ // get published translations of this attribute
426+ $ pubAttribute = eZContentObjectAttribute::fetch ($ contentObjectAttribute ->ID , $ publishedVersionNo );
427+ if ( $ pubAttribute )
428+ {
429+ foreach ( $ pubAttribute ->fetchAttributeTranslations () as $ attributeTranslation )
430+ {
431+ // skip if language is the one being saved
432+ if ( $ attributeTranslation ->LanguageCode === $ languageCode )
433+ continue ;
434+
435+ $ relationList = $ attributeTranslation ->value ();
436+ foreach ($ relList ['relation_list ' ] as $ relationItem ) {
437+ $ existingRelations [] = $ relationItem ['contentobject_id ' ];
438+ }
439+ }
440+ }
441+
442+ // fetch existing attribute translations for current editing version
443+ foreach ( $ contentObjectAttribute ->fetchAttributeTranslations () as $ attributeTranslation )
444+ {
445+ if ( $ attributeTranslation ->LanguageCode === $ languageCode )
446+ continue ;
447+
448+ $ relationList = $ attributeTranslation ->value ();
449+ foreach ($ relationList ['relation_list ' ] as $ relationItem ) {
450+ $ existingRelations [] = $ relationItem ['contentobject_id ' ];
451+ }
452+ }
453+
454+ foreach ( array_unique ($ existingRelations ) as $ existingObjectId )
455+ {
456+ $ contentObject ->addContentObjectRelation ( $ existingObjectId , $ contentObjectVersion , $ contentClassAttributeID , eZContentObject::RELATION_ATTRIBUTE );
457+ }
458+ }
429459 }
430460
431461 foreach ( $ content ['relation_list ' ] as $ relationItem )
@@ -462,7 +492,7 @@ function storeObjectAttribute( $attribute )
462492 }
463493 }
464494 }
465- return $ this ->storeObjectAttributeContent ( $ attribute , $ content );
495+ return $ this ->storeObjectAttributeContent ( $ contentObjectAttribute , $ content );
466496 }
467497
468498 function onPublish ( $ contentObjectAttribute , $ contentObject , $ publishedNodes )
0 commit comments