Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Translatable 3.1 duplicate related dataobjects (onTranslatableCreate)


Go to End


4 Posts   1600 Views

Avatar
Nobrainer Web

Community Member, 138 Posts

7 February 2014 at 11:05pm

For my module Content Blocks: https://github.com/NobrainerWeb/Silverstripe-Content-Blocks
i would like to handle translations in a simple way.

What i would like to do is, when a page is translated
- duplicate all the many_many Blocks of that page
- change the relation for the Block from page in old language to page in new language

I have seen that there is some kind of hook in on the translatable class: "onTranslatableCreate", but how do i use this, to do the above?

I'm guessing i have to add something along the lines of the following to my https://github.com/NobrainerWeb/Silverstripe-Content-Blocks/blob/master/code/ContentBlocksModule.php

	public function onTranslatableCreate() {
	
		$pageblocks = $this->owner->Blocks();
		
		foreach($pageblocks as $orgBlock) {
			//print_r($orgBlock);
			//exit();
			$block = $orgBlock->duplicate(false);
			$block->ParentID = $duplicateWidgetArea->ID;
			$block->write();
		}
			
	}

But the function onTranslatableCreate does not get called.

Any help greatly appriciated :o)

Avatar
Nobrainer Web

Community Member, 138 Posts

10 February 2014 at 3:09am

Ok i figured it out, the following does the job.
Shared for future visitors, hope it helps.
Added this to my class ContentBlocksModule extends DataExtension (extending page)

	/**
	* Simple support for Translatable, when a page is translated, copy all content blocks and relate to translated page
	*/
	public function onTranslatableCreate() {
		
		$translatedPage = $this->owner;
		// Getting the parent translation
		//$originalPage = $translatedPage->getTranslation('en_US');
		print $this->owner->default_locale();
		$originalPage = $this->owner->getTranslation($this->owner->default_locale());
		foreach($originalPage->Blocks() as $originalBlock) {
			$block = $originalBlock->duplicate(true);
			$translatedPage->Blocks()->add($block);
		}

	}	

Avatar
zenmonkey

Community Member, 545 Posts

7 June 2016 at 1:55am

Just wanted to add a modification. This code will always return the original language page even if you're translating a transalation. To ensure you're pulling relations form the actual page you triggered the translation on you shoudl use this.

	/**
	* Simple support for Translatable, when a page is translated, copy all content blocks and relate to translated page
	*/
	public function onTranslatableCreate() {
		
		$translatedPage = $this->owner;
		// Getting the parent translation (Locale is stored in the $_POST)
		$postLang = Controller::curr()->request->postVar('Locale');

		$originalPage = $this->owner->getTranslation($postLang);
		foreach($originalPage->Blocks() as $originalBlock) {
			$block = $originalBlock->duplicate(true);
			$translatedPage->Blocks()->add($block);
		}

	}

Avatar
joelg

Community Member, 134 Posts

25 April 2017 at 8:35pm

Thanks guys. This was quite a help.