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.

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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Prevent "home/" from being deleted or renamed


Go to End


3 Posts   1799 Views

Avatar
DeklinKelly

Community Member, 197 Posts

25 August 2010 at 12:17am

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

Community Member, 271 Posts

25 August 2010 at 12:53am

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

Community Member, 4 Posts

5 October 2010 at 1:33am

Thank you for this solution. Works perfect.