Reason for this topic: The addons BetterSlugs and Unique Slug do not work yet with our new and improved codebase. Secondly, I need slugs for my routes.
Purpose for this topic: I want to have a simple bootstrap.php file in my config folder to generate the slug for me.
My idea: Having a field of type ‘text’ named ‘slug’ and in its options a key ‘slugField’ with the name of the field to generate the slug from. When I create/update an entry, cockpit needs to generate and save the (unique) slug if field slug is empty.
Progress:
I have cockpit-core (2.0.2) installed and created different collections like posts, pages, etc.
In my pages collection, there are several fields like title and slug. (top part of picture)
And in the options of the ‘slug’ field, I added ‘slugField’ as the key and ‘title’ as the value. (bottom part of picture)
This was rather simple thanks you our devs of cockpit.
Help needed:
I have tried to put together a ‘bootstrap.php’ to act when a collection is being saved, but it still isn’t generating a slug. Simple slugs for collections in Cockpit CMS
What am I missing here? Somebody push me in the right direction please
Hi 314!
I updated my file a little.
Check the last version on my gist (link in first post).
It checks if the generated slug is unique in the collection.
if not then adds an incremental count.
to do: when i hit ‘save’ i would like to trigger a ‘page refresh’. so it updates the slug-field. Now i need to go back to the list-view of the collection.
Any help is appreciated
Hey thank you!
I tried with the updated script but still without luck.
Not sure if i’am using it correctly.
Should i really just be named bootstrap.php and then put in the folder config?
There is no file named bootstrap.php in the config folder. I do have to create it.
Or do i have to enable external script somehow in cockpit?
Update would be nice yeah, but seems difficult without js…
The redirect, you thought of in your script seems like a viable way.
Doesn’t it work?
Yes, just create it in the config folder and it should just work.
make sure you have a field (type = ‘text’) named ‘slug’ in your collection and in the fields options add a json key ‘slugField’ with the value of the field you want to generate the slug from (example => “slugField: ‘name’,”)
I changed my “bootstrap.php”-file a little.
When i updated an item, it would add a -1 to the slug.
Changes:
When it searches for the slug in the collection, it checks if the ‘_id’ is the same as the one you want to update. (no more unneccesary ‘-1’)
If the field named ‘slug’ already has a value, it uses this value for the search in the collection. (now you can change it yourself if you want)
Make sure to grab the latest version on my gist (link in first post).
@artur i would be very greatful if you could help me with my ‘page-refresh’-problem (line 69 and 70).
i tried for instance several versions of “$this->reroute(‘/content/items/pages’);” and even \header(“Location:…”) to no avail.
Hi everybody,
I have updated my code to include the ‘sluggification’ of localized fields if applicable.
Grab the updated code if you are interested (link in first post).
Enjoy the rest of your day!
f.