This question is about the URL Update feature when changing the value of a Page
Title
field. The behaviour is coded into CMSMain.EditForm.js.
I'm stripping down and customising the CMS to be usable by an absolute basic computer user or the negligent client, who will most likely skip pressing the Update URL button upon page name change. In these cases it would be very convenient if the URLSegment
would be automatically updated.
Q: What would be the simplest way to automatically have the URL segment updated, IE simulate the result that would appear upon clicking the "Update URL" button, after changing the Title
field?
You could make an extension for SiteTree and include the function onBeforeWrite like this. This will make a change if they updated the Title and not the URL:
class AutoURLSync extends Extension {
public function onBeforeWrite() {
// If Title is changed, but URLSegment is not,
// then update the URLSegment here
if($this->owner->isChanged('Title',2) && !$this->owner->isChanged('URLSegment',2)) {
$this->owner->URLSegment = $this->owner->generateURLSegment($this->owner->Title);
}
}
}
Removing the "if" would mean it always is changed.
Add this in _config/config.yml
to link up the extension:
SiteTree:
extensions:
- AutoURLSync
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With