Better slugs Addon for Cockpit CMS, provides a new slug field type.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
assets
cli
LICENSE
README.md
admin.php
bootstrap.php

README.md

Better Slugs Addon for Cockpit CMS

This addon enhances Cockpit CMS by providing a slug field type that can be used to generate automatically slugs for your collections. Tokens are supported in order to dynamically set values (e.g. dates, fields, etc..).

Idea was partially taken by the Unique Slugs addon https://github.com/raffaelj/cockpit_UniqueSlugs

Installation

Download and unpack addon to <cockpit-folder>/addons/BetterSlugs folder.

Usage

Add a new field of type slug to your collection and configure a format, e.g:

{
  "format": "[collection:name]/[date:Y]/[field:title]"
}

For a collection named post it will result in something like post/2019/your-post-title

{
  "format": "blogs/[date:Y]/[date:m]/[field:title]"
}

In the above example we are forcing the slug to start with blogs string blogs/2019/01/your-post-title

And using a custom callback function:

{
  "format": "blogs/[callback:slugUniqId]/[field:title]"
}

Assuming you have the slugUniqId function (e.g. in a boostrap.php addon file):

function slugUniqId($entry) {
  return uniqid();
}

it will return in something like blogs/5c2ccc816619b/your-post-title

The callback function receives the $entry array as argument.

Currently the following tokens are supported:

  • collection:value - where value is present in the collection structure (e.g. name)
  • date:value - where value is any valid php date char (e.g. Y, m, d, YMD, etc..)
  • field:value - where value is the field name (e.g. title)
  • callback:value - where value is a custom callback function

By default the generated slugs are unique, so if you have a slug field configured with format:

"format": "blogs/[date:Y]/[date:m]/[field:title]"

and your field title is "Blog Test" blogs/2019/01/blog-test, if you insert another entry with same title it will result on blogs/2019/01/blog-test-1, and next one on blogs/2019/01/blog-test-2.

The slug is only autogenerated when the field value is empty, so it can be overriden by the user with a non generated value. A CLI command can be used to update all slugs in a collection:

$ ./cp refresh-slugs --name  blog_post

Collection 'blog_post' - Refreshing slugs...
Slug for 5c2cbece164bfc004d0b7595 updated to 'blogs/2019/01/test'
Slug for 5c2cbf82164bfc0044192afc updated to 'blogs/2019/01/test-1'
Slug for 5c2cbfaf164bfc00455447b4 updated to 'blogs/2019/01/test-2'
Slug for 5c2cc22a164bfc00455447b7 updated to 'blogs/2019/01/another-post'
Slug for 5c2ccc81164bfc00455447b9 updated to 'blogs/2019/01/testing-callback'
Done! 9 entries updated in 0.032s

Copyright and license

Copyright 2019 pauloamgomes under the MIT license.