Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Translatable and Data Object Manager

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 3657 Views
  • Johnny
    Avatar
    Community Member
    34 Posts

    Translatable and Data Object Manager Link to this post

    Hi UncleCheeze!

    Sites that enable translation won't work with Data Object Manager.

    The main problem is that the locale parameter is not propagated in most requests.

    Anyway, I found a work around, and I think you might be interrested to include those changes in your trunk!

    First, I need to enable translations for all the classes (SiteTree, all classes using DataObject), and also for the DataObjectManager and DataObjectManager_Item classes. For those two last, I know it's bad, but it's the work around I found.

    so, in my _config.php, I have :

    Translatable::set_default_locale('fr_FR');
    Object::add_extension('SiteTree', 'Translatable');
    Object::add_extension('Performer', 'Translatable');
    Object::add_extension('DataObjectManager', 'Translatable');
    Object::add_extension('DataObjectManager_Item', 'Translatable');

    After /dev/build, you'll see there's a Locale DB field add for DataObject classes (Performer, in my case)

    Then, in the Performer Class, i need to add an hidden field :

       function getCMSFields_forPopup() {
          $fields = new FieldSet();
          $fields->push( new TextField( 'FullName', 'Nom' ) );
          $fields->push( new TextField( 'Tagline' ) );
          $fields->push( new TextField( 'Web' ) );
          $fields->push( new HiddenField( $name='Locale', $title='Locale', $value=Translatable::get_current_locale() ) );
          return $fields;
       }

    It would be good if the the field would add up itself... But I didn't have the time to work on it...

    After, I needed to edit you DataObjectManager.php :

    Modify getQueryString method :

       protected function getQueryString($params = array())
       {
          $per_page = isset($params['per_page'])? $params['per_page'] :    $this->per_page;
          $show_all = isset($params['show_all'])? $params['show_all'] :    $this->showAll;
          $sort     = isset($params['sort'])? $params['sort']       :    $this->sort;
          $sort_dir = isset($params['sort_dir'])? $params['sort_dir'] :    $this->sort_dir;
          $filter = isset($params['filter'])? $params['filter']    :    $this->filter;
          $search = isset($params['search'])? $params['search']    :    $this->search;
          $locale = $this->hasExtension('Translatable') ? "&locale=".Translatable::get_current_locale() : '';

          return "ctf[{$this->Name()}][per_page]={$per_page}&ctf[{$this->Name()}][showall]={$show_all}&ctf[{$this->Name()}][sort]={$sort}&ctf[{$this->Name()}][sort_dir]={$sort_dir}&ctf[{$this->Name()}][search]={$search}&ctf[{$this->Name()}][filter]={$filter}".$locale;
       }

    Modify the AddLink method :

       public function AddLink() {
          $currentLang = ($this->hasExtension('Translatable')) ? '?locale='.Translatable::get_current_locale() : '';
          return $this->BaseLink() . '/add'.$currentLang;
       }

    And add thoses methods for the DataObjectManager_Item class :

       public function EditLink() {
          $currentLang = ($this->hasExtension('Translatable')) ? '?locale='.Translatable::get_current_locale() : '';
          return parent::EditLink().$currentLang;
       }
       public function DeleteLink() {
          $currentLang = ($this->hasExtension('Translatable')) ? '?locale='.Translatable::get_current_locale() : '';
          return parent::DeleteLink().$currentLang;
       }

    So it works for a has_many relationship. I think I gonna have to work a little more for a many_many relationship. Thank god, I don't have to use a FileObjectManager !!!

    For now I broke the code, so I can't get (easily) in sync with your trunk!

    I know this issue involves a lot of work... And thanks for your good work!

    JP

  • UncleCheese
    Avatar
    4085 Posts

    Re: Translatable and Data Object Manager Link to this post

    Looks interesting. Thanks for looking into this. Is there a better way to do this? I'm not sure what the Translatable extension is, but how are other modules adapting to it? Does this information have to be passed through the request?

  • Johnny
    Avatar
    Community Member
    34 Posts

    Re: Translatable and Data Object Manager Link to this post

    I'm not sure how to handle it correctly... but for sure the 'locale' parameter must be passed for requests that deal with translated objects.

    Maybe you'll have to get a look deeper about multilangual content : http://doc.silverstripe.org/doku.php?id=multilingualcontent

    You can try to setup a small site using a has_many relationship (testimonials, for example). Enable translations like the doc says, and starts translating a page that has a DataObjectManager attached to it.

    Maybe SS team would help!
    Good luck!

    JP

  • rodreading
    Avatar
    Community Member
    6 Posts

    Re: Translatable and Data Object Manager Link to this post

    I am having the same problem with my site Johnny, but I do not understand the exact location of the files that you edited.
    I also have translatable enabled. I have a french translation side to my site. The image gallery works great until I switch to my french language translation side to my site, every time I try to upload a photo... I get nothing in the popup window - acutually where I should have the option to upload a photo in the popup window it gives me the error page on my site.

  • idefix
    Avatar
    Community Member
    11 Posts

    Re: Translatable and Data Object Manager Link to this post

    Hi,

    is there a defined way to use the DataObjectManager with Translatable?
    Johnny's recipe doesn't work for Silverstripe 2.3.3

    I always get an error
    DataObjectDecorator->setOwner(): Trying to decorate an object of class 'DataObjectManager' with 'Translatable', only Dataobject subclasses are supported.

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Translatable and Data Object Manager Link to this post

    The easiest way of fixing this is to edit CMSMain.php and store the locale in the session.
    The ticket is here along with a patch for the fix.

    In all my testing I have never gotten this to work properly in the 2.3.x branch but the fix works properly if you run on the 2.4 branch from the SVN like I'm doing right now. The responsible method for this should be the Link method in CMSMain.php but modifying that has pretty far reaching consequences so it's not recommended trying that.

    If anyone wants to go the DOM route like Johnny does then I would recommend extending the classes you need and overwriting the methods so you don't touch the original code. However I would really recommend using the CMSMain patch since it's so much simpler and it shouldn't be up to the DOM to keep track of locale variables.

    Simply download the patch and apply it to CMSMain.php. If you want to update via SVN simply revert the file, update from SVN and then apply the patch again. I personally have a couple of modified files in the core and having all my fixes in a patch makes updating so much easier.

    3657 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.