Proposal: slug customization
RVxLab opened this issue · 2 comments
This proposal comes forward from several bug reports, primarily from Chinese users, that slugs don’t work in their resources. #145, #167, among others.
While the workaround is to use the name property which then gets slugged, it’s better to have a more systematic approach. For this I propose to add a static method to the Tab class:
class Tab
{
protected static ?Closure $createSlugUsing = null;
public static function createSlugUsing(Closure $createSlugUsing): void
{
self::$createSlugUsing = $createSlugUsing;
}
}
The signature of the closure: (Tab $tab): string
(could be enforced with a PHPStan or Psalm annotation)
During construction of the slug, the Tab class is checked if the passes closure is set and if so, used. Otherwise it will default to its current behaviour.
The closure can be passed in a service provider:
public function boot(): void
{
Tab::createSlugUsing(fn (Tab $tab) => Str::slug($tab->getName()));
}
I can turn it into a PR but I can't test it beyond a unit test, I don't have a Nova 4 license.