Jump to:

5539 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Auto Add new pages

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 789 Views
  • landyman
    Avatar
    Community Member
    11 Posts

    Auto Add new pages Link to this post

    Hello -
    I am not sure if I can do this, so I thought I'd ask here.
    I want the CMS to be able to add a Page automatically when other pages are added and certain conditions are met.

    For example:
    I have pages that include data for a city and state. I have city and state pages that act as directories for these products. Is there a way that I can add a page, have the CMS check if that city and state already exists, and if it doesn't, then add pages for the city and state? That way, a default one can be made, but a user can go and edit content for that city later if they want.

    Thanks in advance!

  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Re: Auto Add new pages Link to this post

    Hi,

    I'd recommend overriding onAfterWrite function Page that you are adding, then that can call a function like the following to create the page if "certain conditions are met"... and make sure that the page has not already been created is one of those conditions

       function makePage($pg,$URLSegment,$Title,$strContent,$bShowInMenus,$bShowInSearch,$iParentID=-1)
       {
          $pg->URLSegment = $URLSegment;
          $pg->Title = $Title;
          $pg->MenuTitle = '';
          $pg->Content = $strContent;
          $pg->MetaTitle = '';
          $pg->MetaDescription = '';
          $pg->MetaKeywords = '';
          $pg->ExtraMeta = '';
          $pg->ShowInMenus = $bShowInMenus;
          $pg->ShowInSearch = $bShowInSearch;
          $pg->HomepageForDomain = '';
          $pg->ProvideComments = '';
          $pg->Sort = '';
          $pg->HasBrokenFile = false;
          $pg->HasBrokenLink = false;
          $pg->Status = '';
          $pg->ReportClass = '';
          $pg->CanViewType = 'Anyone';
          $pg->CanEditType = 'LoggedInUsers';
          $pg->ToDo = '';

          if ($iParentID!=-1)
             $pg->ParentID = $iParentID;

          $pg->write();

          $pg->writeToStage('Stage');
          $pg->publish("Stage", "Live");

          return $pg->ID;
       }

    usage...

    $this->makePage(
       new Page(),
       $strURLSegment,
       $strName,
       $strContent,
       true,
       true,
       $this->ID
    );

  • landyman
    Avatar
    Community Member
    11 Posts

    Re: Auto Add new pages Link to this post

    Perfect. Thanks! I haven't added all the code yet, but this is definitely enough to get me going.

    789 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.