Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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.
<?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
You can’t perform that action at this time.