2

I have my project built with SilverStripe 3.1.13 (both CMS and Framework), and I'm using silverstripe-translatable and dataobject-translatable in order to organize my translations on the website.

Let's say I have Post.php (which is DataObject for working with Posts on my website), and each post got it own category (many-many relationship goes here, but it doesn't really matter)).

The problem is: I hit Save button when creating new Post dataobject and I want to these category'ies would be duplicated automatically to another locales.

How could I implement that in my app? I want to save these values (they're boolean) into another translations of these dataobjects.

pavjel
  • 486
  • 10
  • 25

1 Answers1

2

There is afaik no built in functionality but you can write something like this in the onAfterWrite() method:

public function onAfterWrite() {
    $this->syncCategories();
}

public function syncCategories() {
    //check if you're in main language, assuminig en_US here
    if ($this->Locale !== 'en_US') return;
    foreach ($this->getTranslations() as $translatedPage) {
        //sync relations here
        $translatedPage->RelationName = $this->RelationName;
        //maybe more fine grained locic for only publishing when translated page is alredy published
        $translatedPage->doPublish(); //writes and publishes in one
    }
}
wmk
  • 4,598
  • 1
  • 20
  • 37
  • updating a $many_many relation you need to take care of items added and items removed, but you won't need to publish the page again. – wmk Oct 09 '15 at 09:27