Jump to:

3462 Posts in 1065 Topics by 740 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Translatable page with 1-to-many data objects

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

Page: 1
Go to End
Author Topic: 1554 Views
  • Martin Pales
    Avatar
    Community Member
    19 Posts

    Translatable page with 1-to-many data objects Link to this post

    Hi,

    I'm trying to set up a home page with several exchanging headlines. I created a Headline dataobject (with title, text, image) and added the has_many relationship to the homepage.

    Everything works fine, but I have a small problem with CMS. The homepage is translatable and headlines are shared among those translated homepages (for each I can only select different headlines).

    What I would like to see in the CMS, is a separate list of headlines for each translated homepage.
    Any clue how to do this?

    Harl

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Translatable page with 1-to-many data objects Link to this post

    How are you managing the headlines? If you have a has_many to a page, you just have to filter for this page id (which implies a certain locale). For example, a ComplexTableField with the fieldname set to the name of your relationship will do this automatically.

  • Martin Pales
    Avatar
    Community Member
    19 Posts

    Re: Translatable page with 1-to-many data objects Link to this post

    Thanks for the quick reply!

    However, I don't fully understand what do you mean. Can you please be more specific and give me more details?

    In the meantime I have changed the relationship to many-many since I would like to share the same headline among pages.

    I would like to restrict the headlines availablity/selection in ManyManyDataObjectManager / ManyManyComplexTableField.
    E.g. having 'english' headlines available for selection for english pages and german headlines for german pages.

    Does my headline data object store locale when it is created somehow or should I make it translatable as well?

    Thanks.

    Harl

    It looks like this:

    class Headline extends DataObject {
       static $db = array(
          'Title' => 'Varchar',
          'Text' => 'HTMLText',
       );

       static $has_one = array(
          'Image' => 'Image',
       );

       static $belongs_many_many = array(
          'Pages' => 'Page'
       );

       function getThumbnail() {
          $Image = $this->Image();
          if ($Image) {
             return $Image->CMSThumbnail();
          }
          else {
             return null;
          }
       }

       function getCMSFields_forPopup() {
          $fields = new FieldSet();
          $fields->push(new TextField('Title'));
          $textField = new SimpleTinyMCEField('Text', 'Text', array(), 3);
          $textField->setButtons(array(
             'bold,italic,underline,|,link,unlink,anchor'
          ));
          $fields->push($textField);
          $fields->push(new ImageField('Image'));
          
          return $fields;
       }

    }

    class Page extends SiteTree {
       static $db = array(
       );
       
       static $has_one = array(
       );

       static $many_many = array(
          'Headlines' => 'Headline'
       );
       
       function getCMSFields() {
          $fields = parent::getCMSFields();

          // headlines tab
          $headlinesTableField = new ManyManyDataObjectManager(
             $this,
             'Headlines',
             'Headline',
             array(
                'Title' => 'Title',
                'Text' => 'Text',
                'Thumbnail' => 'Image',
             ),
             'getCMSFields_forPopup'
          );
          $headlinesTableField->setAddTitle('a Headline');

          $fields->addFieldToTab('Root.Content.Headlines', $headlinesTableField);

          return $fields;
       }

    }

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Translatable page with 1-to-many data objects Link to this post

    Not sure about ManyManyDataObjectManager, its a third party module. If you use ComplexTableField, you should get the relation setting for manymany out of the box (although you wont be able to choose existing elements, making it practically a has_many relationship). The trick is to filter for the current elements with $headlinesTableField->setCustomSourceItems($this->Headlines());

    > Does my headline data object store locale when it is created somehow or should I make it translatable as well?
    Your relationship is already set to a specific page ID, every page in every language has its own ID, so no Locale required

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