Last active
September 6, 2016 14:10
-
-
Save thezenmonkey/6e6730023af553f12e3ab762ace3b08a to your computer and use it in GitHub Desktop.
Extension to use for the Basis of integrating SilverStripe Block with Translatable. Add this extension to your page type or copy the onTranslatableCreate function to your page class.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class TranslatableBlockPage extends DataExtension { | |
/** | |
* Simple support for Translatable, when a page is translated, copy all content blocks and relate to translated page | |
* Apply to Pages | |
*/ | |
public function onTranslatableCreate() { | |
$translatedPage = $this->owner; | |
// Getting the parent translation | |
/* Get Locale from $_POST to ensure you're copying translation that triggered createTranslation */ | |
$postLang = Controller::curr()->request->postVar('Locale'); | |
$originalPage = $this->owner->getTranslation($postLang); | |
//This is Key, set local to the source to ensure you're duplicating the right set. By default Locale is transalted page not the source. | |
Translatable::set_current_locale($postLang); | |
if($originalPage->Blocks()->count()) { | |
foreach($originalPage->Blocks() as $originalBlock) { | |
$block = $originalBlock->createTranslation($translatedPage->Locale); | |
$translatedPage->Blocks()->add( | |
$block, | |
array('Sort' => $originalBlock->Sort, 'BlockArea' => $originalBlock->BlockArea) | |
); | |
} | |
} | |
if($originalPage->DisabledBlocks()->count()) { | |
foreach($originalPage->DisabledBlocks() as $originalBlock) { | |
$block = $originalBlock->createTranslation($translatedPage->Locale); | |
$translatedPage->Blocks()->add( | |
$block, | |
array('Sort' => $originalBlock->Sort, 'BlockArea' => $originalBlock->BlockArea) | |
); | |
} | |
} | |
//Set Locale back to transalted page. | |
Translatable::set_current_locale($translatedPage->Locale); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment