Translatable Builder/Repeater Form Field

Hello, I have been trying to resolve this problem for some time without finding a solution. I am therefore asking to you today to find out if you had more perspective, a better understanding or even a solution for this problem because my research led to nothing...
5 Replies
Math_syo
Math_syo3mo ago
What I am trying to do: Translate a builder form field Code: My Page model :
class Page extends Model
{
use HasFactory, HasTranslations;

protected $table = "filament_fabricator__pages";
/**
* The attributes that are mass assignable.
*/
protected $fillable = [
'name',
'title',
'blocks',
];

/**
* The attributes that should be translatable.
*/
public $translatable = [
'title',
'blocks',
];

protected $casts = [
'blocks' => 'array',
];
}
class Page extends Model
{
use HasFactory, HasTranslations;

protected $table = "filament_fabricator__pages";
/**
* The attributes that are mass assignable.
*/
protected $fillable = [
'name',
'title',
'blocks',
];

/**
* The attributes that should be translatable.
*/
public $translatable = [
'title',
'blocks',
];

protected $casts = [
'blocks' => 'array',
];
}
My Filament Ressource :
public static function form(Form $form): Form
{
return $form->schema([
Forms\Components\Grid::make(1)->schema([
Forms\Components\Section::make()->schema([
Forms\Components\TextInput::make('title')
->required(),

Forms\Components\TextInput::make('name')
->label(__('Name'))
->required(),
]),
]),

Forms\Components\Grid::make(1)->schema([
Builder::make('blocks')
->label(__('filament-fabricator::page-resource.labels.blocks'))
->blocks(FilamentFabricator::getPageBlocks())
->mutateDehydratedStateUsing(static function (?array $state): array {
if (! is_array($state)) {
return array_values([]);
}

$registerPageBlockNames = array_keys(FilamentFabricator::getPageBlocksRaw());

return collect($state)
->filter(fn (array $block) => in_array($block['type'], $registerPageBlockNames, true))
->values()
->toArray();
}),
]),
]);
}
public static function form(Form $form): Form
{
return $form->schema([
Forms\Components\Grid::make(1)->schema([
Forms\Components\Section::make()->schema([
Forms\Components\TextInput::make('title')
->required(),

Forms\Components\TextInput::make('name')
->label(__('Name'))
->required(),
]),
]),

Forms\Components\Grid::make(1)->schema([
Builder::make('blocks')
->label(__('filament-fabricator::page-resource.labels.blocks'))
->blocks(FilamentFabricator::getPageBlocks())
->mutateDehydratedStateUsing(static function (?array $state): array {
if (! is_array($state)) {
return array_values([]);
}

$registerPageBlockNames = array_keys(FilamentFabricator::getPageBlocksRaw());

return collect($state)
->filter(fn (array $block) => in_array($block['type'], $registerPageBlockNames, true))
->values()
->toArray();
}),
]),
]);
}
My issue/the error:
count(): Argument #1 ($value) must be of type Countable|array, int given
count(): Argument #1 ($value) must be of type Countable|array, int given
I can save a french and english for my builder but after saved, if I switch from french to english I have this error. It seams to be a known issue but is there a solution ? Moreover, the saved version of french or english seams to don't be whats expected :
Math_syo
Math_syo3mo ago
In english the "image" field is a curator relationship, so in french I should only have the curator media ID, as in english, not all his attributes... ^^'
Dimitar
Dimitar3mo ago
I prefer creating separate page for each language, that way you can have pages for each language with different slug, much simplier
Math_syo
Math_syo3mo ago
I understand that it could be another solution but I prefer the way I'm trying to do. If there is absolutely no solution I'll try yours, thanks ^^
Want results from more Discord servers?
Add your server
More Posts