I want to create a page called "Terms" when dev/build is run, unless a page called "Terms" already exists.
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.
<?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();
}
}
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();
}
Same, but use FoobarPage instead of Page
DataObject::get_one('FoobarPage');
$page = new FoobarPage ();
Perfect, thanks!