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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

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   2688 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.