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.

All other Modules /

Discuss all other Modules here.

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

Translatable 3.1 duplicate related dataobjects (onTranslatableCreate)


Reply


2 Posts   370 Views

Avatar
Nobrainer Web

Community Member, 137 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, 137 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);
      }

   }