ponzu-cms / ponzu

Headless CMS with automatic JSON API. Featuring auto-HTTPS from Let's Encrypt, HTTP/2 Server Push, and flexible server framework written in Go.
https://docs.ponzu-cms.org
BSD 3-Clause "New" or "Revised" License
5.68k stars 387 forks source link

How to change slug on edit #308

Closed junnotantra closed 5 years ago

junnotantra commented 5 years ago

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 :+1:

logicalphase commented 5 years ago

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

junnotantra commented 5 years ago

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

logicalphase commented 5 years ago

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.

junnotantra commented 5 years ago

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. :+1:

Not sure if it's expected or not

logicalphase commented 5 years ago

That might be all you need. I'll still take swat at it. Just remove the style. Adding a button that makes that field editable might be a good idea. I want to check for unwanted words, like "the, a, and. . . ." since Google Search ignores them. But not sure if that's what every user wants.

junnotantra commented 5 years ago

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

logicalphase commented 5 years ago

Will do and update here.

olliephillips commented 5 years ago

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)
}
junnotantra commented 5 years ago

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

olliephillips commented 5 years ago

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