New Addon - BetterSlugs

Hi Cockpit Community,

Cockpit provides a basic mechanism to handle slugs and we have the UniqueSlugs addon that enhances the core by providing uniqueness of slugs, however, and mostly because I have a scenario that requires a few more options I created a new addon that provides the possibility to define complete paths with dynamic tokens (e.g. blogs/[date:Y]/[field:title] or pages/[field:category]/[field:title], etc.)

The full details on addon are on the github page - https://github.com/pauloamgomes/CockpitCMS-BetterSlugs

1 Like

This plugin works great! I’m just confused as to how to actually implement the slugs once they’re generated. How would I navigate to, for example, blogs/2019/newpost ?

From what I understand, the only way to retrieve a single entry in a collection is to use the api and ?filter the GET request or attach a JSON to a POST request.

Hey @ColinTravis, great you liked it… the slugs are just a representation on how you should access information in your frontend, remember that cockpit is a headless CMS. Let’s imagine you have a blog website, and you have a collection named blog, with a title, summary, date, etc… typically each entry should be accessed in form of blogs/<year>/<post-title>. Having that in slug helps you to build your routes in the frontend, that can be usefully mostly if you are using a static site generator like react-static or gatsby, where all blog collection entries are fetched and the routes are built using the slug as path.

Hey @pauloamgomes

I just opened a ticket for BetterSlugs on github:

  • There’s a typo on line 56
  • Slugs only seem to refresh/update if a lug already exists for the entry. If I run refresh, entries that have not been ‘touched’ yet, won’t update.

Thanks @jasonday for reporting and fix

@pauloamgomes can you speak to the other issue - updating/refresh fields that have not been touched? I get an error unless I open each entry and save it, then the refresh works.

Should be fixed, check on the github issue page.

My format:
{"format": "[collection:name]/[callback:slugDateParts]/[field:name]"}
only returns the name of the entry e.g. “/my-first-event”, so I have some questions/remarks:

  • [collection:name] doesn’t work
  • callback function doesn’t work.
  • Where to place the callback function?
  • How to address a field name in a callback function? Like this “$entry[fieldName]”?

I don’t know whether it is relevant, but the BetterSlug-field was added to an existing collection.

Regards,
Zignature

p.s.
Thanks for all your add-on work! Much appreciated :slight_smile:

Thanks @Zignature for pointing the issue, in fact the collection:name wasn’t working due a regression. Should be ok now.
Regarding the callback function that is described in the readme, in such case you should put your function in a boostrap.php file, the function should be like:

function slugDateParts($entry, $app, $lang = FALSE) {
  return $entry['fieldName'];
}

Thanks for the tips!

I got the callbacks working. Had to split them into 3 functions because lime/helper/utils.php changed my slashes into dashes.
Here’s what I got in config/bootstrap.php:

<?php

function slugYear($entry, $app, $lang = FALSE) {
    $slugYear = substr($entry['start_time'], 0, 4);
    return $slugYear;
}

function slugMonth($entry, $app, $lang = FALSE) {
    $slugMonth = substr($entry['start_time'], 5, 2);
    return $slugMonth;
}

function slugDay($entry, $app, $lang = FALSE) {
    $slugDay = substr($entry['start_time'], 8, 2);
    return $slugDay;
}

And this is my format:

{
  "format": "[collection:name]/[callback:slugYear]/[callback:slugMonth]/[callback:slugDay]/[field:name]"
}

But [collection:name], unfortunately, still doesn’t work.

I updated both Cockpit and BetterSlugs to the latest versions. That didn’t help though.

That’s weird, I tried exactly with the same format you have and works:

The error happens when adding a new entry?

I’ll try it with a new collection. See if that works.

It happened editing an entry.
I’ll give it a shot with a new entry too.

I tried in 3 ways and none of them returned the collection name.

  1. Editing an entry in collection Events
  2. Adding a new entry to collection Events
  3. Created a new collection Events2 and added an entry

:confused:

If I don’t get it to work, I’ll (have to) add the collection name to my FE routing. That’ll work too :slight_smile:

Hi, I get an error message (“system error”) when I try to save a collection since installing the addon. The collection is being saved, but the confirmation message has been replaced with the error message.

Can you raise an issue in Github addon repo? Provide also details like what is the field configuration.

After further testing it seems it’s Detektivo that’s causing some issues, not Better Slugs.
Should I open a new thread?