Jump to:

23358 Posts in 18066 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » populateDefaults method on a Page object

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 1327 Views
  • bartvanirsel
    Avatar
    Community Member
    94 Posts

    populateDefaults method on a Page object Link to this post

    I'm trying to force populateDefaults method on an extended Page object, but it does not work, is this only working on a
    DataObject?

    public function populateDefaults() {

    parent::populateDefaults();
    $this->Title = 'ForcedTitle';

    }

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: populateDefaults method on a Page object Link to this post

    Hi Bartvanirsel,

    This function does work on any page class, however the reason it isnt working on the Title field is because I believe there is some JS which runs to set the default page title which would override this.

    Try setting another field to see if that works, if it does then obiusly there is some funny stuff going on to generate the default title and you may have to look deeper to override it.

    Hope that helps.

    Aram

    www.ssbits.com - SilverStripe tutorials, tips and other bits

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: populateDefaults method on a Page object Link to this post

    I can confirm this is not working for Page Titles.

    However this does the trick for me:

    function populateDefaults() {
          parent::populateDefaults();
          $this->Title = _t('Page.TITLE', "My translated title");
       }
       
       function onBeforeWrite() {
          parent::onBeforeWrite();
          if($this->Status == 'New page'){
             $this->populateDefaults();
          }
       }

    Edit:

    You could also check by version:

    if($this->Version == 0){
             $this->populateDefaults();
          }

  • bartvanirsel
    Avatar
    Community Member
    94 Posts

    Re: populateDefaults method on a Page object Link to this post

    Hey Martijn,

    checking on $this->Version worked! thanks

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