F
Filament10mo ago
dwiser

Action button for creating relation on view page

I'm trying to duplicate the CreateAction button that is created with a RelationManager so that I can add it to the top of a ViewRecord page. I got myself most of the way there with associating a model and reusing the Resource form but I'm not sure how to pass the record of the ViewRecord page to the form and also hide the select element in the form that pertains to the record being viewed.
// Resources\ProjectResource\Pages\ViewProject->getHeaderActions():
return [
...
Actions\CreateAction::make()
->model(Shift::class)
->label('Add Shift')
->form(fn (Forms\Form $form) => ShiftResource::form($form)),
...
];

// Resources\ShiftResource::form(Forms\Form $form):
return $form
->schema([
...
Forms\Components\Select::make('project_uuid')
->required()
->relationship(name: 'project', titleAttribute: 'name')
->searchable()
->preload()
->optionsLimit(20)
->columnSpan([
'sm' => 2,
'xl' => 3,
])
->hiddenOn(ProjectShiftsRelationManager::class),
...
]);
// Resources\ProjectResource\Pages\ViewProject->getHeaderActions():
return [
...
Actions\CreateAction::make()
->model(Shift::class)
->label('Add Shift')
->form(fn (Forms\Form $form) => ShiftResource::form($form)),
...
];

// Resources\ShiftResource::form(Forms\Form $form):
return $form
->schema([
...
Forms\Components\Select::make('project_uuid')
->required()
->relationship(name: 'project', titleAttribute: 'name')
->searchable()
->preload()
->optionsLimit(20)
->columnSpan([
'sm' => 2,
'xl' => 3,
])
->hiddenOn(ProjectShiftsRelationManager::class),
...
]);
Solution:
In my case, that doesn't work because I'm not in the context of a form or resource. Filament\Resources\Pages\ViewRecord is being passed through and doesn't have an $ownerRecord property. However, that led to me searching through methods on the ViewRecord page and I found the ->getRecord() method which gets the model for me and can be used in the current context. Working code for my situation is here:...
Jump to solution
3 Replies
dwiser
dwiserOP10mo ago
I got a little bit closer by filling in the form with static data but I can't seem to pull the record from the ViewRecord page.
Actions\CreateAction::make()
->model(Shift::class)
->fillForm(function () {
// I'm trying to get the model and it's 'uuid' from the View page.
return ['project_uuid' => 'febabc3f-93eb-3ce0-b02b-7013a84ed18c'];
})
->form(fn (Forms\Form $form) => ShiftResource::form($form)),
Actions\CreateAction::make()
->model(Shift::class)
->fillForm(function () {
// I'm trying to get the model and it's 'uuid' from the View page.
return ['project_uuid' => 'febabc3f-93eb-3ce0-b02b-7013a84ed18c'];
})
->form(fn (Forms\Form $form) => ShiftResource::form($form)),
Solution
dwiser
dwiser10mo ago
In my case, that doesn't work because I'm not in the context of a form or resource. Filament\Resources\Pages\ViewRecord is being passed through and doesn't have an $ownerRecord property. However, that led to me searching through methods on the ViewRecord page and I found the ->getRecord() method which gets the model for me and can be used in the current context. Working code for my situation is here:
// in App\Filament\App\Resources\ProjectResource\Pages\ViewProject
protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make()
->model(Shift::class)
->fillForm(fn (): array => ['project_uuid' => $this->getRecord()->uuid] )
->form(fn (Forms\Form $form) => ShiftResource::form($form)), // Reusing an existing for schema.
];
}
// in App\Filament\App\Resources\ProjectResource\Pages\ViewProject
protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make()
->model(Shift::class)
->fillForm(fn (): array => ['project_uuid' => $this->getRecord()->uuid] )
->form(fn (Forms\Form $form) => ShiftResource::form($form)), // Reusing an existing for schema.
];
}
This will give me a button on the view page of a record where I can click to create a new relationship and prefill the ID (uuid) of the current resource.
Want results from more Discord servers?
Add your server