From 490c9707497bc8bcffa4c1bf70907c5a9e515734 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Wed, 24 Mar 2021 23:41:59 +0000 Subject: [PATCH] Relation manager hooks --- src/Resources/Pages/ListRecords.php | 4 ++++ src/Resources/RelationManager/AttachRecord.php | 5 +++++ .../RelationManager/Concerns/CanCallHooks.php | 15 +++++++++++++++ src/Resources/RelationManager/CreateRecord.php | 13 +++++++++++++ src/Resources/RelationManager/EditRecord.php | 13 +++++++++++++ src/Resources/RelationManager/ListRecords.php | 9 +++++++++ 6 files changed, 59 insertions(+) create mode 100644 src/Resources/RelationManager/Concerns/CanCallHooks.php diff --git a/src/Resources/Pages/ListRecords.php b/src/Resources/Pages/ListRecords.php index 9769b72775..900ceeee9e 100644 --- a/src/Resources/Pages/ListRecords.php +++ b/src/Resources/Pages/ListRecords.php @@ -44,6 +44,8 @@ class ListRecords extends Page { $this->authorize('delete'); + $this->callHook('beforeDelete'); + static::getModel()::destroy( static::getModel()::find($this->selected) ->filter(function ($record) { @@ -53,6 +55,8 @@ class ListRecords extends Page ->toArray(), ); + $this->callHook('afterDelete'); + $this->selected = []; } diff --git a/src/Resources/RelationManager/AttachRecord.php b/src/Resources/RelationManager/AttachRecord.php index 88c54d2a18..8639335217 100644 --- a/src/Resources/RelationManager/AttachRecord.php +++ b/src/Resources/RelationManager/AttachRecord.php @@ -10,6 +10,7 @@ use Livewire\Component; class AttachRecord extends Component { + use Concerns\CanCallHooks; use HasForm; public $manager; @@ -24,8 +25,12 @@ class AttachRecord extends Component $this->validate(); + $this->callHook('beforeAttach'); + $this->owner->{$this->getRelationshipName()}()->attach($this->related); + $this->callHook('afterAttach'); + $this->emit('refreshRelationManagerList', $manager); if (! $another) { diff --git a/src/Resources/RelationManager/Concerns/CanCallHooks.php b/src/Resources/RelationManager/Concerns/CanCallHooks.php new file mode 100644 index 0000000000..190a862177 --- /dev/null +++ b/src/Resources/RelationManager/Concerns/CanCallHooks.php @@ -0,0 +1,15 @@ +{$hook}(); + } +} diff --git a/src/Resources/RelationManager/CreateRecord.php b/src/Resources/RelationManager/CreateRecord.php index 24255d1ccd..6ff593dc89 100644 --- a/src/Resources/RelationManager/CreateRecord.php +++ b/src/Resources/RelationManager/CreateRecord.php @@ -8,6 +8,7 @@ use Livewire\Component; class CreateRecord extends Component { + use Concerns\CanCallHooks; use HasForm; public $manager; @@ -22,14 +23,22 @@ class CreateRecord extends Component { $manager = $this->manager; + $this->callHook('beforeValidate'); + $this->validateTemporaryUploadedFiles(); $this->storeTemporaryUploadedFiles(); $this->validate(); + $this->callHook('afterValidate'); + + $this->callHook('beforeCreate'); + $this->owner->{$this->getRelationshipName()}()->create($this->record); + $this->callHook('afterCreate'); + $this->emit('refreshRelationManagerList', $manager); if ($another) { @@ -64,7 +73,11 @@ class CreateRecord extends Component { $this->record = []; + $this->callHook('beforeFill'); + $this->fillWithFormDefaults(); + + $this->callHook('afterFill'); } protected function form(Form $form) diff --git a/src/Resources/RelationManager/EditRecord.php b/src/Resources/RelationManager/EditRecord.php index 5b115fa1bf..52ff31b933 100644 --- a/src/Resources/RelationManager/EditRecord.php +++ b/src/Resources/RelationManager/EditRecord.php @@ -9,6 +9,7 @@ use Livewire\Component; class EditRecord extends Component { + use Concerns\CanCallHooks; use HasForm; public $manager; @@ -54,14 +55,22 @@ class EditRecord extends Component abort_unless(Filament::can('update', $this->record), 403); + $this->callHook('beforeValidate'); + $this->validateTemporaryUploadedFiles(); $this->storeTemporaryUploadedFiles(); $this->validate(); + $this->callHook('afterValidate'); + + $this->callHook('beforeSave'); + $this->record->save(); + $this->callHook('afterSave'); + $this->emit('refreshRelationManagerList', $manager); $this->dispatchBrowserEvent('close', "{$manager}RelationManagerEditModal"); @@ -76,8 +85,12 @@ class EditRecord extends Component return; } + $this->callHook('beforeFill'); + $this->record = $this->getQuery()->find($record); $this->resetTemporaryUploadedFiles(); + + $this->callHook('afterFill'); } protected function fillRecord() diff --git a/src/Resources/RelationManager/ListRecords.php b/src/Resources/RelationManager/ListRecords.php index 7b94d387d3..5dc256d5cf 100644 --- a/src/Resources/RelationManager/ListRecords.php +++ b/src/Resources/RelationManager/ListRecords.php @@ -10,6 +10,7 @@ use Livewire\Component; class ListRecords extends Component { + use Concerns\CanCallHooks; use HasTable; public $canAttach; @@ -42,6 +43,8 @@ class ListRecords extends Component { abort_unless($this->canDelete, 403); + $this->callHook('beforeDelete'); + $this->model::destroy( $this->model::find($this->selected) ->filter(function ($record) { @@ -51,6 +54,8 @@ class ListRecords extends Component ->toArray(), ); + $this->callHook('afterDelete'); + $this->selected = []; } @@ -60,8 +65,12 @@ class ListRecords extends Component $relationship = $this->getRelationship(); + $this->callHook('beforeDetach'); + $relationship->detach($this->selected); + $this->callHook('afterDetach'); + $this->dispatchBrowserEvent('close', $manager . 'RelationManagerDetachModal'); $this->dispatchBrowserEvent('notify', __($manager::$detachModalDetachedMessage));