Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 880
Semicolon Avatar asked Jul 19 '16 12:07

Semicolon


1 Answers

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
like image 68
Barry Avatar answered Oct 16 '22 03:10

Barry