Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

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.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Always update URLSegment when Page Name is changed


Go to End


7 Posts   2324 Views

Avatar
Sygmoral

Community Member, 46 Posts

19 December 2014 at 5:51am

I keep telling my clients to click the "Update URL" button on the right side of the Page Name when they create a new page and name it, but they just don't seem to be able to remember that, and so keep creating pages that are addressed "new-page", "new-page-1", ...

I know that there are some situations where changing the page name will automatically change the URLSegment as well, but I'm not actually sure when. At least it does not happen in 3.1.8 when you create a new page and then change the Page Name: the button appears, and you have to actually click it.

Any way for me to make it so that changing the page name ALWAYS updates URLSegment, especially if it is still in its default form (new-[pagetype]-x)? (I think the latter should simply always happen actually; who wants their URLSegments to stay as "new-page-..."?...)

Note: the interface is in Dutch, so it's not actually "new-page", it's "nieuwe-pagina". Perhaps that interferes with the check to automatically update the URLSegment, that might be a bug?

Avatar
Faloude

Community Member, 55 Posts

20 July 2016 at 12:53am

Great post, have you found a solution yet?

I just posted the same issue on StackOverflow: http://stackoverflow.com/questions/38459162/silverstripe-automatic-url-update

Avatar
Faloude

Community Member, 55 Posts

20 July 2016 at 3:24am

Solved by StackoverFlow user Barry:

Create this new SiteTree extension class (indirect Extension)

class AutoURLSync extends Extension {
    public function onBeforeWrite() {
        $this->owner->URLSegment = $this->owner->generateURLSegment($this->owner->Title);
    }
}

Declare the extension in your _config.yml file

SiteTree:
  extensions:
    - AutoURLSync

This will automatically apply the URL that is generated by SilverStripe AFTER clicking the publish button.

Avatar
Sygmoral

Community Member, 46 Posts

20 July 2016 at 3:49am

Edited: 20/07/2016 3:50am

I like it... although I will personally keep the conditional that was originally in that SO answer:

if($this->owner->isChanged('Title',2) && !$this->owner->isChanged('URLSegment',2)) { /* set URLSegment as above */ }

Avatar
Faloude

Community Member, 55 Posts

20 July 2016 at 8:38am

What does this condition do exactly?

Avatar
Sygmoral

Community Member, 46 Posts

20 July 2016 at 8:50am

Edited: 20/07/2016 8:51am

If the user sets the URLSegment themselves, then we don't want to overwrite it with this code. For example a user may change the page name to "Column: ten tips", but the URLSegment to just "ten-tips". This extension would however overwrite that, and make it into "column-ten-tips".

So the point of that conditional is that if the user changes both Title and URLSegment, we probably don't need to take action. But if the Title is changed, and the URLSegment is not (which is exactly what this conditional says), then it looks like they forgot to click 'Update URL', and so we do it for them.

Avatar
Faloude

Community Member, 55 Posts

20 July 2016 at 9:09am

That makes sense :-)