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

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


Reply

5 Posts   908 Views

Avatar
DeklinKelly

4 August 2010 at 1:06am Community Member, 197 Posts

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

Avatar
Martijn

4 August 2010 at 4:20am Community Member, 271 Posts

<?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

4 August 2010 at 5:42am Community Member, 197 Posts

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

4 August 2010 at 6:08am Community Member, 271 Posts

Same, but use FoobarPage instead of Page

DataObject::get_one('FoobarPage');

$page = new FoobarPage ();

Avatar
DeklinKelly

4 August 2010 at 6:34am Community Member, 197 Posts

Perfect, thanks!