diff --git a/src/Components/QuickCreateMenu.php b/src/Components/QuickCreateMenu.php index 6d83954..4b3547c 100644 --- a/src/Components/QuickCreateMenu.php +++ b/src/Components/QuickCreateMenu.php @@ -9,14 +9,18 @@ use Filament\Actions\Concerns\InteractsWithActions; use Filament\Actions\Contracts\HasActions; use Filament\Actions\CreateAction; +use Filament\Facades\Filament; use Filament\Forms\Concerns\InteractsWithForms; use Filament\Forms\Contracts\HasForms; +use Filament\Forms\Form; use Illuminate\Contracts\View\View; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Support\Facades\App; use InvalidArgumentException; use Livewire\Component; -class QuickCreateMenu extends Component implements HasForms, HasActions +class QuickCreateMenu extends Component implements HasActions, HasForms { use InteractsWithActions; use InteractsWithForms; @@ -78,6 +82,64 @@ public function getActions(): array ->slideOver(fn (): bool => QuickCreatePlugin::get()->shouldUseSlideOver()) ->form(function ($arguments, $form) use ($r) { return $r->form($form->operation('create')->columns()); + })->action(function (array $arguments, Form $form, CreateAction $action) use ($r): void { + $model = $action->getModel(); + + $record = $action->process(function (array $data, HasActions $livewire) use ($model, $action, $r): Model { + if ($translatableContentDriver = $livewire->makeFilamentTranslatableContentDriver()) { + $record = $translatableContentDriver->makeRecord($model, $data); + } else { + $record = new $model(); + $record->fill($data); + } + + if ($relationship = $action->getRelationship()) { + /** @phpstan-ignore-next-line */ + $relationship->save($record); + + return $record; + } + + if ( + $r::isScopedToTenant() && + ($tenant = Filament::getTenant()) + ) { + $relationship = $r::getTenantRelationship($tenant); + + if ($relationship instanceof HasManyThrough) { + $record->save(); + + return $record; + } + + return $relationship->save($record); + } + + $record->save(); + + return $record; + }); + + $action->record($record); + $form->model($record)->saveRelationships(); + + if ($arguments['another'] ?? false) { + $action->callAfter(); + $action->sendSuccessNotification(); + + $action->record(null); + + // Ensure that the form record is anonymized so that relationships aren't loaded. + $form->model($model); + + $form->fill(); + + $action->halt(); + + return; + } + + $action->success(); }); }) ->values()