Faloude Faloude - 4 months ago 8
Javascript Question

What would be the simplest way to automatically have the URL segment updated?

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.

enter image description here

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?

Answer

You could make an extension for SiteTree and include the function onAfterWrite like this... which will change if they changed the title and not the URL...

class AutoURLSync extends Extension {

    public function onBeforeWrite() {

        $this->owner->URLSegment = $this->owner->generateURLSegment($this->owner->Title);

    }
}

Removing the "if" would mean it always is changed.

With this in _config/config.yml to link up the extension...

SiteTree:
    extensions:
      - AutoURLSync
Comments