Jump to:

10931 Posts in 2614 Topics by 1810 members

All other Modules

SilverStripe Forums » All other Modules » Translatable 3.1 duplicate related dataobjects (onTranslatableCreate)

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 234 Views
  • Nobrainer Web
    Avatar
    Community Member
    134 Posts

    Translatable 3.1 duplicate related dataobjects (onTranslatableCreate) Link to this post

    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)

  • Nobrainer Web
    Avatar
    Community Member
    134 Posts

    Re: Translatable 3.1 duplicate related dataobjects (onTranslatableCreate) Link to this post

    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);
          }

       }   

    234 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.