Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slug: Cannot customize conversion rules #4087

Closed
scbing opened this issue Aug 4, 2021 · 2 comments
Closed

Slug: Cannot customize conversion rules #4087

scbing opened this issue Aug 4, 2021 · 2 comments

Comments

@scbing
Copy link

scbing commented Aug 4, 2021

The current Slug field is still empty after entering the Chinese title, and it needs to be manually entered to save it.

My idea is: Either change the Slug in the blueprint field configuration so that its value can be empty, or use Laravel Str::slug($title,'zh') to convert, zh can be to read the configuration file: config( 'app.locale').

The blueprint configuration can be left blank, then I will listen to the save event: EntrySaving detects whether the slug field is empty when the event is saved, and then saves this value according to my idea.

However, the default Slug field of the current blueprint field cannot be changed to a null value, and the current idea cannot be realized.

<?php

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        \Statamic\Events\EntrySaving::class => [
            \App\Listeners\TranslateEntitySlug::class,
        ]
    ];
}
<?php

namespace App\Listeners;

use App\Services\FanYi;
use Statamic\Events\EntrySaving;

class TranslateEntitySlug
{
    protected $fanyi;

    public function __construct(FanYi $fanyi)
    {
        $this->fanyi = $fanyi;
    }

    public function handle(EntrySaving $event)
    {
        /** @var \Statamic\Entries\Entry $entry */
        $entry = $event->entry;

        if ($entry->slug()) {
            return;
        }

        if (!$title = $entry->get('title')) {
            return;
        }

//        if (!$slug = \Str::of($title)->slug('-', config('app.locale'))) {
//            return;
//        }

        if (!$slug = $this->fanyi->translate($title, 'auto', 'zh')) {
            return;
        }

        $entry->slug($slug);
    }
}
@duncanmcclean
Copy link
Member

For future notice, please follow the issue template. It really does help with gathering all the information needed to fix your issue. Thanks!

@duncanmcclean
Copy link
Member

Have you seen #2817, is this a duplicate of that?

@scbing scbing closed this as completed Aug 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants