Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

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

populateDefaults method on a Page object


Reply

4 Posts   1358 Views

Avatar
bartvanirsel

30 March 2010 at 10:28pm (Last edited: 30 March 2010 10:28pm), Community Member, 94 Posts

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';

}

Avatar
Carbon Crayon

1 April 2010 at 12:50pm Community Member, 598 Posts

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

Avatar
Martijn

2 April 2010 at 12:49am (Last edited: 2 April 2010 12:59am), Community Member, 271 Posts

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();
      }

Avatar
bartvanirsel

2 April 2010 at 3:41am Community Member, 94 Posts

Hey Martijn,

checking on $this->Version worked! thanks