area17 / twill

Twill is an open source CMS toolkit for Laravel that helps developers rapidly create a custom admin console that is intuitive, powerful and flexible. Chat with us on Discord at https://discord.gg/cnWk7EFv8R.
https://twillcms.com
Apache License 2.0
3.63k stars 560 forks source link

Slugs are missing on duplicate #2617

Open Tofandel opened 4 weeks ago

Tofandel commented 4 weeks ago

Description

When duplicating a model with the duplicate feature, the slug is not created, resulting in routing issues, when saving a model outside of twill, the slug is also not created

This is what I'm doing to fix both problems

    public function handleSlugsOnSave(): void
    {
        if (empty($this->twillSlugData)) {
            $this->twillSlugData = array_filter($this->getSlugParams(), fn ($params) => ! empty($params['slug']));

            foreach ($this->twillSlugData as &$datum) {
                $datum['active'] ??= true;
            }
        }

        $this->originalHandleSlugsOnSave();
    }

I'm wondering why the slug handling is coded like this

if ($this->twillSlugData === []) { // Why return here?
    return;
}

foreach (getLocales() as $locale) {
    $this->disableLocaleSlugs($locale);
}

// This means this condition is useless
$slugParams = $this->twillSlugData !== [] ? $this->twillSlugData : $this->getSlugParams();

Seems both those lines where added in the same PR https://github.com/area17/twill/pull/1897, and it wasn't a case of the first return was added later to fix some issue