PHP code example of levgenij / filament-translatable
1. Go to this page and download the library: Download levgenij/filament-translatable 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/ */
levgenij / filament-translatable example snippets
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Levgenij\LaravelTranslatable\Translatable;
class Category extends Model
{
use Translatable;
protected $fillable = [
'parent_id',
'is_active',
'sort',
// Translatable fields must also be in fillable
'title',
'slug',
'description',
];
/**
* Translatable attributes.
*/
public array $translatable = [
'title',
'slug',
'description',
'meta_title',
'meta_description',
];
}
namespace App\Filament\Resources;
use Filament\Schemas\Schema;
use Filament\Schemas\Components\Section;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Components\Select;
use Filament\Resources\Resource;
use Levgenij\FilamentTranslatable\Concerns\TranslatableResource;
class CategoryResource extends Resource
{
use TranslatableResource;
protected static ?string $model = Category::class;
public static function form(Schema $schema): Schema
{
return $schema->components([
Section::make()->schema([
// Translatable fields (detected from $model->translatable)
TextInput::make('title')
->label('Title')
->
namespace App\Filament\Resources\CategoryResource\Pages;
use App\Filament\Resources\CategoryResource;
use Filament\Resources\Pages\CreateRecord;
use Levgenij\FilamentTranslatable\Concerns\HasTranslatableFields;
class CreateCategory extends CreateRecord
{
use HasTranslatableFields;
protected static string $resource = CategoryResource::class;
}
namespace App\Filament\Resources\CategoryResource\Pages;
use App\Filament\Resources\CategoryResource;
use Filament\Resources\Pages\EditRecord;
use Levgenij\FilamentTranslatable\Concerns\HasTranslatableFields;
class EditCategory extends EditRecord
{
use HasTranslatableFields;
protected static string $resource = CategoryResource::class;
}
'missing_translation_badge' => [
'enabled' => true,
'label' => '•',
// Any Filament color name
'color' => 'warning',
],
'blank' => [
// Set to true to treat HTML markup without text ("<p></p>", "<p><br></p>") as blank.
// An empty RichEditor submits "<p></p>", so enable this when forms use it.
'strip_tags' => false,
// Used with strip_tags: tags that count as content even without text
'content_tags' => ['img', 'iframe', 'video', 'audio', 'embed', 'object', 'svg'],
// Characters ignored when looking for text (non-breaking space, zero-width space)
'invisible_characters' => ["\u{00A0}", "\u{200B}"],
],
class EditCategory extends EditRecord
{
use HasTranslatableFields;
protected function mutateFormDataBeforeFill(array $data): array
{
// First call parent method to load translations
$data = parent::mutateFormDataBeforeFill($data);
// Your additional logic
$data['custom_field'] = $this->record->computeSomething();
return $data;
}
}
class CreateCategory extends CreateRecord
{
use HasTranslatableFields;
protected function mutateFormDataBeforeCreate(array $data): array
{
// Your logic before creation
if (! empty($data['image'])) {
$data['image'] = $this->processImage($data['image']);
}
return $data;
}
}
use Filament\Schemas\Schema;
use Filament\Forms\Components\TextInput;
public static function form(Schema $schema): Schema
{
return $schema->components([
TextInput::make('slug')
->