Is there a way to have a static URLSegment for a given page? Sometimes I have to hardcode URLSegments into my template, and, although it's unlikely, it would be nice if I could be sure the client wouldn't change the title and mess things up.
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.
Hi UncleCheese.
Well. You could use the Page ID, since that's immutable by your client. But this would fail if the client deletes the page and tries to re-create it.
In the getCMSFields function you could make the ID visible for the user inside the CMS:
$fields->addFieldToTab(
'Root.Content.Main',
new ReadonlyField('PageId', 'Page ID', $this->ID)
);
And in the template you would do something like:
<% if ID = 3 %>
.. specific content for page with id 3 here ...
<% end_if %>
Another alternative could be a field that can only be written once.
Here's an example how this could be done:
public static $db = array(
'MyIDField' => 'Varchar(64)'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
if($this->MyIDField == null){
$fields->addFieldToTab(
'Root.Content.Main',
new TextField('MyIDField', 'Page-ID')
);
} else {
$fields->addFieldToTab(
'Root.Content.Main',
new ReadonlyField('MyIDField', 'Page-ID')
);
}
return $fields;
}