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/"
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
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.
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/"
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();
}
}
Thank you for this solution. Works perfect.