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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

populateDefaults method on a Page object


Go to End


4 Posts   1889 Views

Avatar
bartvanirsel

Community Member, 96 Posts

30 March 2010 at 10:28pm

Edited: 30/03/2010 10:28pm

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

Community Member, 598 Posts

1 April 2010 at 12:50pm

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

Community Member, 271 Posts

2 April 2010 at 12:49am

Edited: 02/04/2010 12:59am

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

Community Member, 96 Posts

2 April 2010 at 3:41am

Hey Martijn,

checking on $this->Version worked! thanks