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...
11 Replies
Math_syo
Math_syoOP8mo 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_syoOP8mo 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
Dimitar8mo 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_syoOP8mo 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 ^^
Soundmit
Soundmit6mo ago
@Dimitar how do you mange multiple pages? You also have different menu for any language?
Dimitar
Dimitar6mo ago
Yes different menu
Soundmit
Soundmit6mo ago
i create pages with my model, with your solution i can't have 2 pages with slug / for the home..
Dimitar
Dimitar6mo ago
You should be able I have unique(slug,lang) Also my routing have {lang} as prefix before each url
Soundmit
Soundmit6mo ago
My slug field is unique uhm, i made a test i've create a page in english (i use the action language switcher) then a page in italian i can see the english page if APP_LOCALE=en and the italian page if APP_LOCALE=it but i need a way to link the 2+n pages (relation) for when i swith the langiage from frontend
Want results from more Discord servers?
Add your server