Jump to:

3429 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Creating Site Content from PHP Code For a Multilingual Site

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

Page: 1
Go to End
Author Topic: 1380 Views
  • elgordo
    Avatar
    Community Member
    70 Posts

    Creating Site Content from PHP Code For a Multilingual Site Link to this post

    hi

    I ran into a problem with creating content for a multilingual website (English and Thai) which I now think I have resolved but would appreciate peer review from those who understand the internals of Silverstripe better than I do.

    The scenario was one of this - a user can register to the site and then contribute material, in this case a suggested link, to the webmaster. The intention was to use the fields from this form to create a Link object (containing a title, description and website url) in the database, unpublished, and email the web master. He or she can then decide whether to reject the submission or drag and drop it to an appropriate place in the website.

    I found some code examples that suggested the following:

    $link = new Link();
    $link->ParentID = $this->ID;
    $link->Title = 'Whatever';
    $link->Url = 'http://www.whatever.com';
    $link->Description = 'This is a description of whatever.com';
    $link->write();

    // publication of this item not required, will be done manually by the webmaster

    Now this worked in the en_US version of the website, but *not* the th_TH version (Thai). When I looked in the database I observed the following:

    1) The parent ID of the Link object is a page in the Thai SiteTree, with locale th_TH
    2) The newly created Link had a locale of en_US

    Things are kind of broken now, as data is in the database but not rendering in the SiteTree in /admin/cms. After I realised the locale issue I changed the code above to end with

    $link->Locale = Translatable::get_current_locale();
    $link->write();

    This resolved the issues I was observing.

    So in short if you are working outside of the en_US site tree you need to both set the parentID *and* the locale to see the new item correctly placed both on the site and the administrative interface.

    Regards

    Gordon

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: Creating Site Content from PHP Code For a Multilingual Site Link to this post

    Hi,

    I remember there being an issue with translatable DataObjects and the setting of the correct locale. Don't know if this would solve the initial problem in your setup, but just in case... See this ticket/patch:

    http://open.silverstripe.org/attachment/ticket/4199/ComplexTableField-Translatable.diff

  • elgordo
    Avatar
    Community Member
    70 Posts

    Re: Creating Site Content from PHP Code For a Multilingual Site Link to this post

    Thanks for that pointer - I will add the existence check for the Translatable class to my code

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