Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

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

Prevent "home/" from being deleted or renamed


Reply

3 Posts   1062 Views

Avatar
DeklinKelly

25 August 2010 at 12:17am Community Member, 197 Posts

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/"

Avatar
Martijn

25 August 2010 at 12:53am Community Member, 271 Posts

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

Avatar
WhiteGfx

5 October 2010 at 1:33am Community Member, 4 Posts

Thank you for this solution. Works perfect.