Passing closure in viewData

In a Filament resource, I have some custom fields and right now im passing the record's data to the custom livewire view like this -
Forms\Components\View::make('engines')
->viewData([
'formValues' => [
'title' => $form->getRecord()?->title,
'subtitle' => $form->getRecord()?->subtitle,
'button_text' =>$form->getRecord()?->button_text,
'content' => $form->getRecord()?->content,
],

])->visible(fn(string $operation) => $operation === 'edit')
->columnSpanFull()
Forms\Components\View::make('engines')
->viewData([
'formValues' => [
'title' => $form->getRecord()?->title,
'subtitle' => $form->getRecord()?->subtitle,
'button_text' =>$form->getRecord()?->button_text,
'content' => $form->getRecord()?->content,
],

])->visible(fn(string $operation) => $operation === 'edit')
->columnSpanFull()
What i'd like to achieve is to make this component more granular so it will fit the create pages as well, which means that i need to pass the current data inside the form itself (from my experience - using Get $get), but the problem is that the viewData function is only receiving array and can't receive a Closure. How can i solve it? Thanks.
2 Replies
prowler
prowler7d ago
Ideally what i'd want to is be able to do something like this -
Forms\Components\View::make('engines')
->viewData(function(Get $get) {
return [
'formValues' => [
'title' => $get('title'),
'subtitle' => $get('subtitle'),
'button_text' => $get('button_text'),
'content' => $get('content'),
];
})
Forms\Components\View::make('engines')
->viewData(function(Get $get) {
return [
'formValues' => [
'title' => $get('title'),
'subtitle' => $get('subtitle'),
'button_text' => $get('button_text'),
'content' => $get('content'),
];
})
dissto
dissto7d ago
I think you can just access the record directly in the view without the need to pass it down?
@dd($getRecord())
@dd($getRecord())
🤔