# Can't generate slug. cockpit v2

**URL:** <https://discourse.getcockpit.com/t/cant-generate-slug-cockpit-v2/2601>\
**Category:** Cockpit v2\
**Created:** [December 8, 2022, 5:53pm UTC](https://discourse.getcockpit.com/t/cant-generate-slug-cockpit-v2/2601 "2022-12-08T17:53:22Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![buzz](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@buzz](https://discourse.getcockpit.com/u/buzz)\
**Post date:** [December 9, 2022, 11:09am UTC](https://discourse.getcockpit.com/t/cant-generate-slug-cockpit-v2/2601/2 "2022-12-09T11:09:16Z")

</div>

Ok I’ve committed the sin of not reading as this was already solved [here](https://discourse.getcockpit.com/t/cp-2-generate-slug-when-saving-collection/2432).

For quick reference, paste the following code into your /config/bootstrap.php file (if bootstrap.php isn’t there create the file)

```auto
<?php

$app->on('content.item.save', function($modelName, $data) {

    $model = $this->module('content')->model($modelName);
    $collection = $model['type'] == 'singleton' ? 'content/singletons' : "content/collections/{$modelName}";
    $fields = $model['fields'] ?? [];

    foreach ($fields as $field) {

        // check if slug field and referenced field is in data
        if (isset($field['opts']['slugField'], $data[$field['opts']['slugField']])) {
            
            $data[$field['name']] = $this->helper('utils')->sluggify($data[$field['opts']['slugField']]);
            
            $this->dataStorage->save($collection, $data);
            break;
        }
    }
});

```

Add the desired field you want the slug to appear in and ensure it’s a text field:

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/getcockpit/original/2X/e/e65cc293a4e6cd647fdc6d45d1ac16a75479ad9c.png)

In my case I simply named it slug

In the field settings, under options → JSON add the following value -` slugField: 'heading'`

I’ve put the value ‘heading’ as that’s the field I want a slug for

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/getcockpit/original/2X/b/b75a015284c054939eec0f4d12f2bfc2fd3b68e4.png)

Thanks to @artur for the solution!

---

_[View the full topic](https://discourse.getcockpit.com/t/cant-generate-slug-cockpit-v2/2601)._
