# Generate Unique Slug Associated to Entry

**URL:** <https://discourse.getcockpit.com/t/generate-unique-slug-associated-to-entry/443>\
**Category:** Support\
**Created:** [December 4, 2018, 3:28pm UTC](https://discourse.getcockpit.com/t/generate-unique-slug-associated-to-entry/443 "2018-12-04T15:28:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alessandro](https://avatars.discourse-cdn.com/v4/letter/a/ac91a4/32.png) [@alessandro](https://discourse.getcockpit.com/u/alessandro)\
**Post date:** [December 4, 2018, 3:28pm UTC](https://discourse.getcockpit.com/t/generate-unique-slug-associated-to-entry/443/1 "2018-12-04T15:28:11Z")

</div>

Hello,

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

Thank you

---

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [December 4, 2018, 5:06pm UTC](https://discourse.getcockpit.com/t/generate-unique-slug-associated-to-entry/443/2 "2018-12-04T17:06:29Z")

</div>

It’s not possible with the core. Use the [UniqueSlugs addon](https://github.com/raffaelj/cockpit_UniqueSlugs).

---

<div class="post-metadata">

**Author:** ![alessandro](https://avatars.discourse-cdn.com/v4/letter/a/ac91a4/32.png) [@alessandro](https://discourse.getcockpit.com/u/alessandro)\
**Post date:** [December 5, 2018, 5:46am UTC](https://discourse.getcockpit.com/t/generate-unique-slug-associated-to-entry/443/3 "2018-12-05T05:46:12Z")

</div>

I created a bootstrap.php file in config folder with this snippet:  
$app-\>on(“collections.save.before”, 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.
