What would be the easiest way to automatically update a URL segment?

This question is about the URL Update function when changing the value of the Page Title field. The behavior is encoded in CMSMain.EditForm.js.

enter image description here

I remove and configure the CMS so that it can be used by an absolute base computer user or a careless client, who will most likely miss the click on the Refresh URL button when changing the page name. In these cases, it would be very convenient if the URLSegment is automatically updated.

Q: What will be the easiest way to automatically update a URL segment, IE to simulate the result that appears when you click the "Update URL" button after changing the Title field?

+6
source share
1 answer

You can make an extension for SiteTree and enable the onBeforeWrite function like this. This will make a change if they update the Title, 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 "if" means that it always changes.

Add this to _config/config.yml to bind the extension:

 SiteTree: extensions: - AutoURLSync 
+7
source

All Articles