mirror of
https://github.com/filamentphp/filament.git
synced 2026-09-24 15:42:09 +08:00
Relation manager hooks
This commit is contained in:
@@ -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 = [];
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Filament\Resources\RelationManager\Concerns;
|
||||
|
||||
trait CanCallHooks
|
||||
{
|
||||
protected function callHook($hook)
|
||||
{
|
||||
if (! method_exists($this, $hook)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->{$hook}();
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user