Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ phpstan.neon
testbench.yaml
vendor
node_modules
.DS_Store
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,45 @@ class ParentComponent extends Component
}

```

## Handling Form Deletion with Custom Relations

If your project has custom relations to the `FilamentForm` model (like tests, surveys, etc.), you can handle their deletion by listening to the `FilamentFormForceDeletingEvent` event. This event is fired before a form is force deleted.

```php
namespace App\Listeners;

use Tapp\FilamentFormBuilder\Events\FilamentFormForceDeletingEvent;

class HandleCustomFormDeletion
{
public function handle(FilamentFormForceDeletingEvent $event): void
{
// Access the form being deleted
$form = $event->form;

// Delete your custom relations
$form->tests()->delete();
$form->surveys()->delete();
// ... etc
}
}
```

Register the listener in your `EventServiceProvider`:

```php
protected $listen = [
\Tapp\FilamentFormBuilder\Events\FilamentFormForceDeletingEvent::class => [
\App\Listeners\HandleCustomFormDeletion::class,
],
];
```

This way, when a form is force deleted:
1. The package will fire the `FilamentFormForceDeletingEvent`
2. Your listener will handle the deletion of custom relations
3. The package will then delete its own relations (form fields and form users)
4. Finally, the form itself will be deleted

This approach keeps the package clean and project-agnostic while allowing you to handle custom relations in your project.
17 changes: 17 additions & 0 deletions src/Events/FilamentFormForceDeletingEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Tapp\FilamentFormBuilder\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Tapp\FilamentFormBuilder\Models\FilamentForm;

class FilamentFormForceDeletingEvent
{
use Dispatchable, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(public FilamentForm $form) {}
}
47 changes: 47 additions & 0 deletions src/Filament/Resources/FilamentFormResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;
use Tapp\FilamentFormBuilder\Filament\Resources\FilamentFormResource\Pages\CreateFilamentForm;
use Tapp\FilamentFormBuilder\Filament\Resources\FilamentFormResource\Pages\EditFilamentForm;
Expand Down Expand Up @@ -134,6 +136,51 @@ public static function table(Table $table): Table

return Redirect::to('/admin/filament-forms/'.$formCopy->id.'/edit');
}),
Action::make('delete')
->requiresConfirmation()
->form([
Forms\Components\Checkbox::make('force_delete')
->label('Force delete')
->helperText('This will delete the form and all related records. This action cannot be undone.'),
])
->action(function (FilamentForm $record, array $data) {
try {
DB::beginTransaction();

// Fire a "before delete" event that projects can listen to
if ($data['force_delete'] ?? false) {
event(new \Tapp\FilamentFormBuilder\Events\FilamentFormForceDeletingEvent($record));

// Only delete relations that are part of the package
$record->filamentFormFields()->delete();
$record->filamentFormUsers()->delete();
}

$record->delete();

DB::commit();

Notification::make()
->success()
->title('Form deleted')
->send();
} catch (QueryException $e) {
DB::rollBack();

if (! ($data['force_delete'] ?? false) && str_contains($e->getMessage(), 'foreign key constraint fails')) {
Notification::make()
->warning()
->title('Cannot delete form')
->body('This form has related records. Use force delete to remove all related records.')
->persistent()
->send();

return;
}

throw $e;
}
}),
])
->bulkActions([
BulkActionGroup::make([
Expand Down