Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unique slugs (next) #501

Closed
ineentho opened this issue Dec 22, 2016 · 12 comments
Closed

Unique slugs (next) #501

ineentho opened this issue Dec 22, 2016 · 12 comments

Comments

@ineentho
Copy link

As far as I can see, there's no way to enforce an unique slug in cockpit next. This forces us to use URLs such as /news/58541375423d3doc1761310462/my-slug instead of a much more clean /news/my-slug.

What are your thoughts on this? Should we create a separate slug field in addition to the existing {slug: true} option available on text fields, or should we somehow make the slug provided with text fields editable?

@aheinze
Copy link
Member

aheinze commented Dec 22, 2016

Hi,

yes you're right the slug functionality is pretty simple. What you can do is to create a custom bootstrap.php in the config folder and hook into collections.save.before.{$collectionname} event and make sure that your slugs are unique.

Greets,
Artur

@ineentho
Copy link
Author

Thanks, that's a pretty good solution, I will probably go with that for now. Still, it's not a complete solution for some (edge)cases like if I want two posts with the same title but different slugs.

Is that something that you want fixed by a separate slug field that auto generates slugs based on a specified field (like the title) until the slug field is edited (maybe a flag is set slug_field_name_custom = true)?

@BenRoe
Copy link

BenRoe commented Dec 26, 2016

Can someone @ineentho @aheinze post a example bootstrap.php

@aheinze
Copy link
Member

aheinze commented Dec 27, 2016

create a file bootstrap.php in the config folder with the following code:

<?php

$app->on("collections.save.before.{$collectionname}", function($name, &$entry, $isUpdate) {
  // custom code to modify $entry before saving
});

hope this helps.

@BenRoe
Copy link

BenRoe commented Dec 27, 2016

Thanks @aheinze but it does not help. I am Front-End guy and have rudimentary php knowledge.

@gryphonmyers
Copy link

Correct me if I'm wrong, but wasn't there a "unique" flag for fields in the legacy version of Cockpit? What is the thinking behind removing it? You are right that the hooks are there to do it ourselves, but it does seem like fundamental functionality for a CMS.

@aheinze
Copy link
Member

aheinze commented Jan 20, 2017

@gryphonmyers there was no unique option, just a slug option for text fields which behaved like in Next

@piotr-cz
Copy link
Contributor

piotr-cz commented Feb 25, 2017

I've added a functionality to set flag on slug to be unique within selected collection in legacy cockpit version here #338 and here #341.

However since PRs have been merged there was no new release.

@gryphonmyers
Copy link

gryphonmyers commented Feb 25, 2017 via email

@piotr-cz
Copy link
Contributor

piotr-cz commented Feb 25, 2017

No, you aren't.

@fabianmu
Copy link

Here's a solution which works fine for me to create slugs automatically: https://gist.github.com/fabianmu/5f73a6c2303e08add4e00dc2e548ef2d
You just need to replace the name of the collection aktuelles and the entry's field which you want to base your slug on, name in my case.

In case you create an entry with name = test it would store test as slug for the first creating, another entry with the same name will have slug = test-1.

@raffaelj
Copy link
Contributor

@fabianmu I modified your script with the annotations from Artur and I rewrote it to make it more flexible.

https://github.com/raffaelj/cockpit-scripts/blob/master/unique-slugs/bootstrap.php

@aheinze aheinze closed this as completed Sep 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants