[BetterSlugs] help needed: custom slugs with callback

Hey @pauloamgomes,

Since I’m completely new to Cockpit and not much of a programmer, I could use your help.

I have an events collection with data imported from a JSON file I retrieved from Facebook.
One of the fields is start_time and it has a datestring formatted as yyyy-mm-ddThh:mm:ss-xxxx, e.g. 2020-10-02T23:00:00+0200.

I would like to extract the year, the month and the day separately to create slugs like this: /events/yyyy/mm/dd/title

A callback function would be the answer to this, I think, but how would I go about creating one. And would this function reside in /addons/BetterSlugs/bootstrap.php or /addons/BetterSlugs/Helper/Utils.php?


Something like this?


  "format": "/[collection:name]/[callback:slugDateParts]/[field:title]"


function slugDateParts($entry, $app, $lang = FALSE) {
    $year = substr($entry[start_time], 0, 4);
    $month = substr($entry[start_time], 5, 2);
    $day = substr($entry[start_time], 8, 2);
    return $year . "/" . $month . "/" . $day;


Changed the function to:

function slugDateParts($entry, $app, $lang = FALSE) {
  return str_replace('-', '/', substr($entry[start_time], 0, 10));

It seems (looks) cleaner.