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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

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   1378 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!