Generate Unique Slug Associated to Entry


How can I generate a unique slug associated to a collection entry?

Thank you

It’s not possible with the core. Use the UniqueSlugs addon.


I created a bootstrap.php file in config folder with this snippet:
$app->on(“”, function($name, &$entry, $isUpdate) use ($app) {
if (strlen($entry[‘Slug’]) > 0){
$slug = strtolower($entry[“Slug”]);
$slug = str_replace(" ", “-”, $slug);
$slug = preg_replace(’/[^A-Za-z0-9-]/’, ‘’, $slug);
$count = $app->module(‘collections’)->count($name, [“customSlug” => $slug]);
if ($count > 0) {
$slug = $slug . “-” . $count;
$entry[“customSlug”] = $slug;

If the entry in any collection contains a field called Slug, the snippet adds a field called customSlug to the entry ensuring that it is unique within the collection.

This is a very simple approach that may generate some problems in some cases but it satisfy my needs for now. The field names could be stored in the configuration instead of being hardcoded. The thing I like is that in this way I keep the slug separated from the title or the name of the entry.