I’m accustomed to generating slugs by using {“slug” : true} in the options JSON in version 1. However it doesn’t seem to work in v2
Ok I’ve committed the sin of not reading as this was already solved here.
For quick reference, paste the following code into your /config/bootstrap.php file (if bootstrap.php isn’t there create the file)
<?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:
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
Thanks to @artur for the solution!


