From e3987507615bdd27465088bcf2b481eaf0e9383b Mon Sep 17 00:00:00 2001 From: Adam Weston Date: Sat, 15 Jun 2024 15:41:55 -0400 Subject: [PATCH] Fix: tenancy --- src/Components/QuickCreateMenu.php | 64 +++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) 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()