Filament v4: Adding extra fields on ViewRecord page

Hi all, I have a use case where I have a ViewRecord page which shows data in a infolist. But I want to add some extra fields by calling an api. I prefer to override the method mutateFormDataBeforeFill https://filamentphp.com/docs/4.x/resources/viewing-records#customizing-data-before-filling-the-form but looks like it is never called. How can I add new fields to the infolist.
/**
* @param array<string, mixed> $data
* @return array<string, mixed>
*/
protected function mutateFormDataBeforeFill(array $data): array
{
dd('check'); // never gets called
$semester = Semester::where('is_active', true)->firstOrFail();
$photo = Photo::where('uin', $data['uinid']);
$primaryAdvisor = $studentAdvisors->advisors?->primaryAdvisor();
$data['student_advisor'] = $primaryAdvisor->full_name;
$data['student_photo'] = $photo->image;
....//more fields.
return $data;
}
/**
* @param array<string, mixed> $data
* @return array<string, mixed>
*/
protected function mutateFormDataBeforeFill(array $data): array
{
dd('check'); // never gets called
$semester = Semester::where('is_active', true)->firstOrFail();
$photo = Photo::where('uin', $data['uinid']);
$primaryAdvisor = $studentAdvisors->advisors?->primaryAdvisor();
$data['student_advisor'] = $primaryAdvisor->full_name;
$data['student_photo'] = $photo->image;
....//more fields.
return $data;
}
To get the mutateFormDataBeforeFill method to work I had to override the mount method like so:
public function mount(int|string $record): void
{
parent::mount($record);
$this->mutateFormDataBeforeFill($this->getRecord()->attributesToArray());
}
public function mount(int|string $record): void
{
parent::mount($record);
$this->mutateFormDataBeforeFill($this->getRecord()->attributesToArray());
}
But after this I still don't see the new appended data in the infolist. Any help is appreciated.
2 Replies
kaster
kaster2w ago
I don't know if it would work but I guess you could call it inside the ->state method of the infolist.
protected function getApiData(array $data): array
{
$semester = Semester::where('is_active', true)->firstOrFail();
$photo = Photo::where('uin', $data['uinid']);
$primaryAdvisor = $studentAdvisors->advisors?->primaryAdvisor();
$data['student_advisor'] = $primaryAdvisor->full_name;
$data['student_photo'] = $photo->image;
....//more fields.
return $data;
}
protected function getApiData(array $data): array
{
$semester = Semester::where('is_active', true)->firstOrFail();
$photo = Photo::where('uin', $data['uinid']);
$primaryAdvisor = $studentAdvisors->advisors?->primaryAdvisor();
$data['student_advisor'] = $primaryAdvisor->full_name;
$data['student_photo'] = $photo->image;
....//more fields.
return $data;
}
public function infolist(Infolist $infolist): Infolist
{
return $infolist->state([
'data' => $this->getApiData(),
])->schema([
TextEntry::('data'),
])
public function infolist(Infolist $infolist): Infolist
{
return $infolist->state([
'data' => $this->getApiData(),
])->schema([
TextEntry::('data'),
])
Something like this.
Dennis Koch
Dennis Koch2w ago
Infolists aren't forms that's why mutateFormDataBeforeFill() isn't called.

Did you find this page helpful?