PHP code example of fnx-software / filament-astrotomic

1. Go to this page and download the library: Download fnx-software/filament-astrotomic library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fnx-software / filament-astrotomic example snippets


// config/translatable.php
'locales' => [
    'en',
    'es',
    'fr',
],

// app/Providers/Filament/AdminPanelProvider.php

use Filament\Panel;
use Fnxsoftware\FilamentAstrotomic\FilamentAstrotomicPlugin;

public function panel(Panel $panel): Panel
{
    return $panel
        // ...
        ->plugins([
            FilamentAstrotomicPlugin::make(),
        ]);
}

FilamentAstrotomicPlugin::make()
    ->mainLocale('ar')

use App\Models\Setting;

FilamentAstrotomicPlugin::make()
    ->mainLocale(fn () => Setting::where('key', 'default_locale')->first()?->value ?? 'en')

FilamentAstrotomicPlugin::make()
    ->locales(['ar', 'en', 'fr'])

// app/Models/Post.php

use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;
use Astrotomic\Translatable\Translatable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model implements TranslatableContract
{
    use Translatable;

    public array $translatedAttributes = [
        'title',
        'content',
    ];

    protected $fillable = [
        'author_id',
    ];
}

// app/Filament/Resources/PostResource.php

use Filament\Resources\Resource;
use Fnxsoftware\FilamentAstrotomic\Resources\Concerns\ResourceTranslatable;

class PostResource extends Resource
{
    use ResourceTranslatable;

    // ...
}

// app/Filament/Resources/PostResource/Pages/ListPosts.php

use Filament\Resources\Pages\ListRecords;
use Fnxsoftware\FilamentAstrotomic\Resources\Pages\ListTranslatable;

class ListPosts extends ListRecords
{
    use ListTranslatable;

    // ...
}

// app/Filament/Resources/PostResource/Pages/CreatePost.php

use Filament\Resources\Pages\CreateRecord;
use Fnxsoftware\FilamentAstrotomic\Resources\Pages\CreateTranslatable;

class CreatePost extends CreateRecord
{
    use CreateTranslatable;

    // ...
}

// app/Filament/Resources/PostResource/Pages/EditPost.php

use Filament\Resources\Pages\EditRecord;
use Fnxsoftware\FilamentAstrotomic\Resources\Pages\EditTranslatable;

class EditPost extends EditRecord
{
    use EditTranslatable;

    // ...
}

// app/Filament/Resources/PostResource/Pages/ViewPost.php

use Filament\Resources\Pages\ViewRecord;
use Fnxsoftware\FilamentAstrotomic\Resources\Pages\ViewTranslatable;

class ViewPost extends ViewRecord
{
    use ViewTranslatable;

    // ...
}

use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\TextInput;
use Fnxsoftware\FilamentAstrotomic\Schemas\Components\TranslatableTabs;
use Fnxsoftware\FilamentAstrotomic\TranslatableTab;

TranslatableTabs::make()
    ->localeTabSchema(fn (TranslatableTab $tab): array => [
        TextInput::make($tab->makeName('title'))
            ->

use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Fnxsoftware\FilamentAstrotomic\Schemas\Components\TranslatableTabs;
use Fnxsoftware\FilamentAstrotomic\TranslatableTab;

TranslatableTabs::make()
    ->localeTabSchema(fn (TranslatableTab $tab): array => [
        TextInput::make($tab->makeName('title'))
            ->label($tab->makePrefixLabel('Title')),

        Textarea::make($tab->makeName('description'))
            ->label($tab->makeSuffixLabel('Description')),
    ])

use Filament\Forms\Components\TextInput;
use Fnxsoftware\FilamentAstrotomic\Schemas\Components\TranslatableTabs;
use Fnxsoftware\FilamentAstrotomic\TranslatableTab;

TranslatableTabs::make()
    ->customLocales(['ar', 'en', 'fr', 'pt'])
    ->localeTabSchema(fn (TranslatableTab $tab): array => [
        TextInput::make($tab->makeName('label'))
            ->

use Fnxsoftware\FilamentAstrotomic\Schemas\Tables\TranslatableSelect;

TranslatableSelect::make('country_id')
    ->translatableRelationship('country', 'name')
    ->searchable()
    ->preload()
    ->label('Country')
    ->

use Fnxsoftware\FilamentAstrotomic\Schemas\Tables\TranslatableSelect;
use Modules\People\Models\Person;

TranslatableSelect::make('person_id')
    ->label('Person')
    ->translatableRelationship('person', 'first_name')
    ->translatableLabelUsing(fn (Person $record, string $locale): string => collect([
        $record->translate($locale)?->first_name,
        $record->translate($locale)?->father_name,
        $record->translate($locale)?->last_name,
    ])->filter()->implode(' ') ?: "#{$record->getKey()}")
    ->searchable()
    ->preload()
    ->

use Fnxsoftware\FilamentAstrotomic\Schemas\Tables\TranslatableSelect;
use Modules\People\Models\Person;

TranslatableSelect::make('person_id')
    ->label('Person')
    ->translatableRelationship('person', 'first_name')
    ->translatableLabelUsing(fn (Person $record, string $locale): string => collect([
        $record->translate($locale)?->first_name,
        $record->translate($locale)?->father_name,
        $record->translate($locale)?->last_name,
    ])->filter()->implode(' ') ?: "#{$record->getKey()}")
    ->translatableSearchAttributes([
        'first_name',
        'father_name',
        'last_name',
    ])
    ->searchable()
    ->preload()
    ->

TranslatableSelect::make('department_id')
    ->label('Department')
    ->translatableRelationship(
        relationship: 'department',
        titleAttribute: 'name',
        modifyQueryUsing: fn ($query) => $query
            ->where('active', true)
            ->orderBy('order'),
    )
    ->searchable()
    ->preload();

TranslatableSelect::make('person_id')
    ->translatableRelationship('person', 'first_name')
    ->translatableLabelUsing(fn ($record, string $locale): string => '...')
    ->translatableSearchAttributes([
        'first_name',
        'last_name',
    ]);

// In your List page, for example ListPosts.php

use Filament\Actions\CreateAction;
use Fnxsoftware\FilamentAstrotomic\Actions\LocaleSwitcher;

protected function getHeaderActions(): array
{
    return [
        CreateAction::make(),
        LocaleSwitcher::make(),
    ];
}

use Filament\Tables\Table;
use Fnxsoftware\FilamentAstrotomic\Tables\Columns\TranslatableColumn;

public static function table(Table $table): Table
{
    return $table
        ->columns([
            TranslatableColumn::make('title')
                ->searchable()
                ->sortable(),
        ]);
}

use Filament\Infolists\Infolist;
use Fnxsoftware\FilamentAstrotomic\Infolists\Components\TranslatableEntry;

public static function infolist(Infolist $infolist): Infolist
{
    return $infolist
        ->schema([
            TranslatableEntry::make('title'),
            TranslatableEntry::make('content'),
        ]);
}

use Fnxsoftware\FilamentAstrotomic\FilamentAstrotomicPlugin;

public function panel(Panel $panel): Panel
{
    return $panel
        // ...
        ->plugin(
            FilamentAstrotomicPlugin::make()
                ->force()
        );
}

use Fnxsoftware\FilamentAstrotomic\Schemas\Components\TranslatableTabs;

TranslatableTabs::make('translations')
    ->force()
    ->localeTabSchema(fn (TranslatableTab $tab): array => [
        // ...
    ])

use Fnxsoftware\FilamentAstrotomic\Actions\LocaleSwitcher;

LocaleSwitcher::make()
    ->locales(['en', 'fr']);

LocaleSwitcher::make()
    ->locales([
        'en' => 'English (US)',
        'fr' => 'Français (France)',
    ]);

public static function getTranslatableLocales(): array
{
    return ['en', 'fr'];
}

use App\Models\Post;
use Filament\Tables\Actions\EditAction;

->actions([
    EditAction::make()
        ->mutateRecordDataUsing(function (Post $record, array $data): array {
            return static::mutateTranslatableData($record, $data);
        }),
])

use Fnxsoftware\FilamentAstrotomic\Tables\Columns\TranslatableColumn;

TranslatableColumn::make('country.name')
    ->label('Country')
    ->searchable()
    ->sortable();

use Fnxsoftware\FilamentAstrotomic\Infolists\Components\TranslatableEntry;

TranslatableEntry::make('country.name')
    ->label('Country');
bash
php artisan vendor:publish --tag="translatable"