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

[Request]: Support Spatie Translatable plugin #55

Open
Eyvazov opened this issue Nov 16, 2024 · 3 comments
Open

[Request]: Support Spatie Translatable plugin #55

Eyvazov opened this issue Nov 16, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@Eyvazov
Copy link

Eyvazov commented Nov 16, 2024

What happened?

How can I make filament-menu-builder plugin multilingual with Filament spatie translatable plugin?

How to reproduce the bug

How can I make filament-menu-builder plugin multilingual with Filament spatie translatable plugin?

Package Version

0.6.0

PHP Version

8.1

Laravel Version

10.10

Which operating systems does with happen with?

No response

Notes

No response

@Eyvazov Eyvazov added the bug Something isn't working label Nov 16, 2024
@InfinityXTech
Copy link

why is this a bug?

@datlechin datlechin changed the title [Bug]: [Feature Request]: Support Spatie Translatable plugin Nov 25, 2024
@datlechin datlechin added enhancement New feature or request and removed bug Something isn't working labels Nov 25, 2024
@datlechin datlechin changed the title [Feature Request]: Support Spatie Translatable plugin [equest]: Support Spatie Translatable plugin Nov 25, 2024
@datlechin datlechin changed the title [equest]: Support Spatie Translatable plugin [Request]: Support Spatie Translatable plugin Nov 25, 2024
@mckenziearts
Copy link

Hi @Eyvazov I don't think this plugin has any advantage in directly supporting Spatie Translatable Plugin.

The problem

Not everyone who uses it will need to translate their content. So if the plugin natively supports the translatable plugin, it's an additional dependency for those who don't want to use it.

The solution

You can install the Spatie plugin, and make the following modifications:

  1. In your migrations, change the column type of the fields you want to translate from string to json.
public function up(): void
{
        Schema::create(config('filament-menu-builder.tables.menus'), static function (Blueprint $table): void {
            $table->id();
            $table->json('name');
            $table->boolean('is_visible')->default(true);
            $table->timestamps();
        });

        Schema::create(config('filament-menu-builder.tables.menu_items'), static function (Blueprint $table): void {
            $table->id();
            $table->foreignIdFor(Menu::class)->constrained()->cascadeOnDelete();
            $table->foreignIdFor(MenuItem::class, 'parent_id')->nullable()->constrained($table->getTable())->nullOnDelete();
            $table->nullableMorphs('linkable');
            $table->json('title');
            $table->string('url')->nullable();
            $table->string('target', 10)->default(LinkTarget::Self);
            $table->integer('order')->default(0);
            $table->timestamps();
        });

        Schema::create(config('filament-menu-builder.tables.menu_locations'), static function (Blueprint $table): void {
            $table->id();
            $table->foreignIdFor(Menu::class)->constrained()->cascadeOnDelete();
            $table->string('location')->unique();
            $table->timestamps();
        });
}
  1. Extend Models to add the use HasTranslations trait, and add the fields you want to translate to the public array $translatable array.

Example with the Menu Model

namespace App\Models;

use Datlechin\FilamentMenuBuilder\Models\Menu as Model;
use Spatie\Translatable\HasTranslations;

class Menu extends Model
{
    use HasTranslations;

    protected $casts = [
        'is_visible' => 'boolean',
    ];

    /**
     * @var array|string[]
     */
    public array $translatable = ['name'];
}
  1. Extend the various Resources as shown in the package documentation and then in the ServiceProvider
    Here's what the MenuResource might look like (don't forget to do the same for the ListMenus and EditMenu pages)
namespace App\Filament\Resources;

use App\Filament\Resources\MenuResource\Pages;
use Datlechin\FilamentMenuBuilder\Resources\MenuResource as BaseMenuResource;
use Filament\Resources\Concerns\Translatable;

final class MenuResource extends BaseMenuResource
{
    use Translatable;

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListMenus::route('/'),
            'edit' => Pages\EditMenu::route('/{record}/edit'),
        ];
    }
}

Finally, modify your PanelProvider to look like this

use App\Filament\Resources\MenuResource;
use App\Models\Menu;
use App\Models\MenuItem;

public function panel(Panel $panel): Panel
{
        return $panel
            ->default()
            ->id('admin')
            ->plugins([
                SpatieLaravelTranslatablePlugin::make()
                    ->defaultLocales(['fr', 'en']),
                FilamentMenuBuilderPlugin::make()
                    ->usingResource(MenuResource::class)
                    ->usingMenuModel(Menu::class)
                    ->usingMenuItemModel(MenuItem::class),
            ])
}

Hope this will help.

@mstfkhazaal
Copy link

Hi @Eyvazov
Check This Pull Request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants