Skip to content

Commit d5c7ddc

Browse files
authored
Merge pull request #62 from awcodes/feat/disable-create-another
Feat: support create another as optional
2 parents 2fd739e + cc0370f commit d5c7ddc

File tree

3 files changed

+109
-52
lines changed

3 files changed

+109
-52
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,23 @@ public function panel(Panel $panel): Panel
135135
}
136136
```
137137

138+
### Create Another
139+
140+
By default, the ability to create another record will respect the settings of your 'create record' or 'list records' create action. This can be overridden to either enable or disable it for all resources with the `createAnother()` method.
141+
142+
```php
143+
use Awcodes\FilamentQuickCreate\QuickCreatePlugin;
144+
145+
public function panel(Panel $panel): Panel
146+
{
147+
return $panel
148+
->plugins([
149+
QuickCreatePlugin::make()
150+
->createAnother(false),
151+
])
152+
}
153+
```
154+
138155
### Appearance
139156

140157
#### Rounded

src/Components/QuickCreateMenu.php

Lines changed: 78 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -76,75 +76,101 @@ protected function cacheActions(): void
7676

7777
public function getActions(): array
7878
{
79-
return collect($this->resources)->transform(function ($resource) {
80-
$r = App::make($resource['resource_name']);
81-
82-
return CreateAction::make($resource['action_name'])
83-
->authorize($r::canCreate())
84-
->model($resource['model'])
85-
->slideOver(fn (): bool => QuickCreatePlugin::get()->shouldUseSlideOver())
86-
->form(function ($arguments, $form) use ($r) {
87-
return $r->form($form->operation('create')->columns());
88-
})->action(function (array $arguments, Form $form, CreateAction $action) use ($r): void {
89-
$model = $action->getModel();
90-
91-
$record = $action->process(function (array $data, HasActions $livewire) use ($model, $action, $r): Model {
92-
if ($translatableContentDriver = $livewire->makeFilamentTranslatableContentDriver()) {
93-
$record = $translatableContentDriver->makeRecord($model, $data);
94-
} else {
95-
$record = new $model();
96-
$record->fill($data);
79+
return collect($this->resources)
80+
->transform(function ($resource) {
81+
$r = App::make($resource['resource_name']);
82+
$canCreateAnother = QuickCreatePlugin::get()->canCreateAnother();
83+
84+
if ($canCreateAnother === null) {
85+
if ($r->hasPage('create')) {
86+
$canCreateAnother = App::make($r->getPages()['create']->getPage())::canCreateAnother();
87+
} else {
88+
$page = isset($r->getPages()['index'])
89+
? $r->getPages()['index']->getPage()
90+
: null;
91+
92+
if ($page) {
93+
$reflectionMethod = new \ReflectionMethod($page, 'getHeaderActions');
94+
$actions = $reflectionMethod->invoke(new $page());
95+
$createAction = collect($actions)->filter(function ($action) {
96+
return $action instanceof CreateAction;
97+
})->first();
98+
99+
if ($createAction) {
100+
$canCreateAnother = $createAction->canCreateAnother();
101+
}
97102
}
103+
}
104+
}
105+
106+
return CreateAction::make($resource['action_name'])
107+
->authorize($r::canCreate())
108+
->model($resource['model'])
109+
->slideOver(fn (): bool => QuickCreatePlugin::get()->shouldUseSlideOver())
110+
->form(function ($arguments, $form) use ($r) {
111+
return $r->form($form->operation('create')->columns());
112+
})
113+
->createAnother($canCreateAnother)
114+
->action(function (array $arguments, Form $form, CreateAction $action) use ($r): void {
115+
$model = $action->getModel();
116+
117+
$record = $action->process(function (array $data, HasActions $livewire) use ($model, $action, $r): Model {
118+
if ($translatableContentDriver = $livewire->makeFilamentTranslatableContentDriver()) {
119+
$record = $translatableContentDriver->makeRecord($model, $data);
120+
} else {
121+
$record = new $model();
122+
$record->fill($data);
123+
}
98124

99-
if ($relationship = $action->getRelationship()) {
100-
/** @phpstan-ignore-next-line */
101-
$relationship->save($record);
125+
if ($relationship = $action->getRelationship()) {
126+
/** @phpstan-ignore-next-line */
127+
$relationship->save($record);
102128

103-
return $record;
104-
}
129+
return $record;
130+
}
105131

106-
if (
107-
$r::isScopedToTenant() &&
108-
($tenant = Filament::getTenant())
109-
) {
110-
$relationship = $r::getTenantRelationship($tenant);
132+
if (
133+
$r::isScopedToTenant() &&
134+
($tenant = Filament::getTenant())
135+
) {
136+
$relationship = $r::getTenantRelationship($tenant);
111137

112-
if ($relationship instanceof HasManyThrough) {
113-
$record->save();
138+
if ($relationship instanceof HasManyThrough) {
139+
$record->save();
114140

115-
return $record;
116-
}
141+
return $record;
142+
}
117143

118-
return $relationship->save($record);
119-
}
144+
return $relationship->save($record);
145+
}
120146

121-
$record->save();
147+
$record->save();
122148

123-
return $record;
124-
});
149+
return $record;
150+
});
125151

126-
$action->record($record);
127-
$form->model($record)->saveRelationships();
152+
$action->record($record);
153+
$form->model($record)->saveRelationships();
128154

129-
if ($arguments['another'] ?? false) {
130-
$action->callAfter();
131-
$action->sendSuccessNotification();
155+
if ($arguments['another'] ?? false) {
156+
$action->callAfter();
157+
$action->sendSuccessNotification();
132158

133-
$action->record(null);
159+
$action->record(null);
134160

135-
// Ensure that the form record is anonymized so that relationships aren't loaded.
136-
$form->model($model);
161+
// Ensure that the form record is anonymized so that relationships aren't loaded.
162+
$form->model($model);
137163

138-
$form->fill();
164+
$form->fill();
139165

140-
$action->halt();
166+
$action->halt();
141167

142-
return;
143-
}
168+
return;
169+
}
144170

145-
$action->success();
146-
});
147-
})
171+
$action->success();
172+
});
173+
})
148174
->values()
149175
->toArray();
150176
}

src/QuickCreatePlugin.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class QuickCreatePlugin implements Plugin
4242

4343
protected string | array | Closure | null $keyBindings = null;
4444

45+
protected bool | Closure | null $createAnother = null;
46+
4547
public function boot(Panel $panel): void
4648
{
4749
Livewire::component('quick-create-menu', Components\QuickCreateMenu::class);
@@ -261,4 +263,16 @@ public function getKeyBindings(): ?array
261263
{
262264
return collect($this->evaluate($this->keyBindings))->toArray();
263265
}
266+
267+
public function createAnother(bool | Closure $condition = true): static
268+
{
269+
$this->createAnother = $condition;
270+
271+
return $this;
272+
}
273+
274+
public function canCreateAnother(): ?bool
275+
{
276+
return $this->evaluate($this->createAnother);
277+
}
264278
}

0 commit comments

Comments
 (0)