Jump to:

23454 Posts in 18904 Topics by 2877 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, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 898 Views
  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Create a page when dev/build run if the page does not exsist Link to this post

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

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: Create a page when dev/build run if the page does not exsist Link to this post

    <?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();
       }
    }

  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Re: Create a page when dev/build run if the page does not exsist Link to this post

    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();
    }

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: Create a page when dev/build run if the page does not exsist Link to this post

    Same, but use FoobarPage instead of Page

    DataObject::get_one('FoobarPage');

    $page = new FoobarPage ();

  • DeklinKelly
    Avatar
    Community Member
    197 Posts
    898 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.