Jump to:

23453 Posts in 18904 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Prevent "home/" from being deleted or renamed

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: 1049 Views
  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Prevent "home/" from being deleted or renamed Link to this post

    If the URLSegment of a page is "home/" I want to:

    1.) prevent the page from being deleted
    2.) prevent the URLSegment from being changed to anything besides "home/"

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: Prevent "home/" from being deleted or renamed Link to this post

    This is how I setup my homepage so it prevents renaming URLSegment, prevents deleting by non admins and force to be the first page in the SiteTree.

    <?php

    class HomePage extends Page{

       static $icon = 'axyrpack/images/icons/home';
       
       static $singular_name    = 'HomePage';
       static $plural_name    = 'HomePages';
       
       function canCreate() {
          return !DataObject::get_one($this->class);
       }
       
       function canDelete() {   
          return Permission::check("ADMIN");
       }

       function populateDefaults(){
          parent::populateDefaults();
       }
       
       function onBeforeWrite () {
          parent::onBeforeWrite();
          if($this->ID){
             $this->ParentID         = 0;
             $this->Sort          = 0;
             if(Object::has_extension('SiteTree','Translatable') && $this->Locale != Translatable::default_locale()) {
                $parts = explode('_',$this->Locale);
                $this->URLSegment       = 'home-'.$parts[0];
             } else {
                $this->URLSegment       = 'home';   
             }
          }
       }
       
       function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->addFieldToTab("Root.Content.Metadata",new HiddenField('ClassName', 'ClassName'));
          $fields->addFieldToTab("Root.Content.Metadata",new HiddenField('URLSegment', 'URLSegment'));
          $fields->removeFieldsFromTab("Root.Content.Metadata", array('URL'));
          return $fields;   
       }
    }

    class HomePage_Controller extends Page_Controller {
       
       function init(){
          parent::init();   
       }   
    }

  • WhiteGfx
    Avatar
    Community Member
    4 Posts

    Re: Prevent "home/" from being deleted or renamed Link to this post

    Thank you for this solution. Works perfect.

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