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

Create a page when dev/build run if the page does not exsist


Go to End


5 Posts   1456 Views

Avatar
DeklinKelly

Community Member, 197 Posts

4 August 2010 at 1:06am

I want to create a page called "Terms" when dev/build is run, unless a page called "Terms" already exists.

Avatar
Martijn

Community Member, 271 Posts

4 August 2010 at 4:20am

<?php
class Page extends SiteTree {
	
	// ...
	
	function requireDefaultRecords() {
		if(!DataObject::get_one('Page', "Title = 'Terms'")){
			$page = new Page();
			$page->Title = 'Terms';
			$page->MenuTitle = $page->Title;
			$page->Content = '';
			$page->Status = 'Published';
			$page->write();
			$page->publish('Stage', 'Live');
			$page->flushCache();
			DB::alteration_message('Terms page created', 'created');
		}
		parent::requireDefaultRecords();
	}
}

Avatar
DeklinKelly

Community Member, 197 Posts

4 August 2010 at 5:42am

Thanks! Now, how can I make that page a "Foobar Page" ?

When I put this code in FoobarPage.php it does create a new page, but it creates a standard page instead of a "Foobar Page".

class FoobarPage extends Page {

   function requireDefaultRecords() { 
      if(!DataObject::get_one('Page', "Title = 'Terms'")){ 
         $page = new Page(); 
         $page->Title = 'Terms'; 
         $page->MenuTitle = $page->Title; 
         $page->Content = ''; 
         $page->Status = 'Published'; 
         $page->write(); 
         $page->publish('Stage', 'Live'); 
         $page->flushCache(); 
         DB::alteration_message('Terms page created', 'created'); 
      } 
      parent::requireDefaultRecords(); 
   } 

Avatar
Martijn

Community Member, 271 Posts

4 August 2010 at 6:08am

Same, but use FoobarPage instead of Page

DataObject::get_one('FoobarPage');

$page = new FoobarPage ();

Avatar
DeklinKelly

Community Member, 197 Posts

4 August 2010 at 6:34am

Perfect, thanks!