21307 Posts in 5737 Topics by 2603 members
General Questions
SilverStripe Forums » General Questions » Create a page when dev/build run if the page does not exsist
General questions about getting started with SilverStripe that don't fit in any of the categories above.
Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 716 Views |
-
Create a page when dev/build run if the page does not exsist

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.
-
Re: Create a page when dev/build run if the page does not exsist

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();
}
} -
Re: Create a page when dev/build run if the page does not exsist

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();
} -
Re: Create a page when dev/build run if the page does not exsist

4 August 2010 at 6:08am
Same, but use FoobarPage instead of Page
DataObject::get_one('FoobarPage');
$page = new FoobarPage ();
-
Re: Create a page when dev/build run if the page does not exsist

4 August 2010 at 6:34am
Perfect, thanks!
| 716 Views | ||
|
Page:
1
|
Go to Top |


