Skip to content

Commit

Permalink
Fix: tenancy
Browse files Browse the repository at this point in the history
  • Loading branch information
awcodes committed Jun 15, 2024
1 parent dc90431 commit e398750
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion src/Components/QuickCreateMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit e398750

Please sign in to comment.