eminiarts / nova-tabs

Laravel Nova Tabs Package
457 stars 141 forks source link

Proposal: slug customization #249

Open RVxLab opened 1 year ago

RVxLab commented 1 year ago

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()));
}
marcfil commented 1 year ago

Hi @RVxLab,

Could you turn this into a PR and test it?

RVxLab commented 1 year ago

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.