ponzu-cms/ponzu

How to change slug on edit

junnotantra opened this issue · 10 comments

Hi, I'm able to customize my slug by overriding String()

// String defines how an Article is printed. Update it using more descriptive
// fields from the Article struct type
func (a *Article) String() string {
	return a.Title
}

When I make an item with title what the slug, the slug will be what-the-slug

But this only happen when the item was first created. When I edit the title to be what the slug is this, I want the slug to change to what-the-slug-is-this instead of keeping the original slug what-the-slug. Is this possible?

I've tried to update the slug on BeforeSave() but changing the slug there not work as expected.

Btw, thanks for the awesome CMS 👍

Just a thought, but might want the slug edit to be implicit by adding a editable field for edit view only.

it would be wonderful to have this feature

When I get a little time this weekend I'll look at doing a PR for that. Something similar to how WordPress managed permalink changes.

Btw, just tried a stupid thing. I edited the DOM from my browser inspector.
Removed the disabled="true" from

<input value="what-the-slug-is-this" label="URL Slug" type="text" disabled="true" placeholder="Will be set automatically" name="slug">

Then save, it works. 👍

Not sure if it's expected or not

Found out, it doesn't work as expected.
I cannot retrieve the item using the forcefully edited slug.
/api/content?slug=what-the-slug-is-this returns error

while I can retrieve the item using the original slug
/api/content?slug=what-the-slug
returns

{
	"data": [
		{
			"uuid": "eeb0b6ed-ddda-4738-adae-7a3c8ef5d653",
			"id": 4,
			"slug": "what-the-slug-is-this",
			"timestamp": 1558083709000,
			"updated": 1558162849040,
			"title": "what the slug is this",
			"content": "update pls<br>",
		}
	]
}

still need help to update slug and make the item retrievable by the edited slug
thanks in advance

Will do and update here.

if you override the item's implementation of sluggable you can get very close I think, the catch being that you need to save twice, which is less than ideal.

// provide custom slug here
func (a *Article) ItemSlug() string {
	return strings.Replace(a.Title, " ", "-", -1)
}

Hi guys, I've opened a PR for this issue. If you have time, please take a look at #309
Thank you

#309 just got merged into ponzu-dev. @junnotantra thanks on behalf of the Ponzu community for your work on that, it will be useful for a good number of users. Closing this issue