-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lukas Frey <[email protected]>
- Loading branch information
1 parent
ad9b313
commit 8648281
Showing
15 changed files
with
445 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace Guava\Filament\NestedResources; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Ancestor | ||
{ | ||
public function __construct( | ||
protected string $resource, | ||
protected ?string $relationship = null, | ||
) { | ||
} | ||
|
||
public function getResource(): string | ||
{ | ||
return $this->resource; | ||
} | ||
|
||
public function getRelationship(): string | ||
{ | ||
if (! $this->relationship) { | ||
return $this->getResource()::getModelLabel(); | ||
} | ||
|
||
return $this->relationship; | ||
} | ||
|
||
public function getRouteParameterName(): string | ||
{ | ||
return get_resource_route_parameter($this->getResource()); | ||
} | ||
|
||
public function getRouteParameters(Model $record): array | ||
{ | ||
$ancestor = $this; | ||
$related = $record; | ||
|
||
$parameters = []; | ||
do { | ||
// For 'create' actions, a model is not yet created, so the owner record is being sent. | ||
// In this case, the ancestor relation and received model are offset by one level and this | ||
// will re-sync the depth again. | ||
if ($ancestor->getResource()::getModel() === $related::class) { | ||
$parameters[$ancestor->getRouteParameterName()] = $related->id; | ||
continue; | ||
} | ||
|
||
$related = $ancestor->getRelatedModel($related); | ||
$parameters[$ancestor->getRouteParameterName()] = $related->id; | ||
} while ($ancestor = $ancestor->getResource()::getAncestor()); | ||
|
||
return $parameters; | ||
} | ||
|
||
public function getNormalizedRouteParameters(Model $record): array | ||
{ | ||
return array_values( | ||
array_reverse( | ||
$this->getRouteParameters($record) | ||
) | ||
); | ||
} | ||
|
||
public function getRelatedModel(Model $record): Model | ||
{ | ||
return $record->{$this->getRelationship()}; | ||
} | ||
|
||
public static function make(string $resource, string $relationship = null) | ||
{ | ||
return app(static::class, [ | ||
'resource' => $resource, | ||
'relationship' => $relationship, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Guava\Filament\NestedResources\Concerns; | ||
|
||
use Guava\Filament\NestedResources\Ancestor; | ||
|
||
trait HasAncestor | ||
{ | ||
public static function hasAncestor(): bool | ||
{ | ||
return static::getAncestor() !== null; | ||
} | ||
|
||
public static function getAncestor(): ?Ancestor | ||
{ | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Guava\Filament\NestedResources\Pages; | ||
|
||
use Filament\Resources\Pages\CreateRecord; | ||
use Guava\Filament\NestedResources\Ancestor; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Support\Arr; | ||
|
||
class NestedCreateRecord extends CreateRecord | ||
{ | ||
use NestedPage; | ||
|
||
protected function mutateFormDataBeforeCreate(array $data): array | ||
{ | ||
$ancestor = $this->getAncestorRecord(); | ||
|
||
return parent::mutateFormDataBeforeCreate([ | ||
...$data, | ||
...$ancestor, | ||
]); | ||
} | ||
|
||
protected function getAncestorRecord(): array | ||
{ | ||
$id = Arr::last($this->getRouteParameterIds()); | ||
$ancestor = static::getResource()::getAncestor(); | ||
$record = $ancestor->getResource()::getModel()::find($id); | ||
$fake = new (static::getModel())(); | ||
/** @var BelongsTo $relation */ | ||
$relation = $fake->{$ancestor->getRelationship()}(); | ||
|
||
return [$relation->getForeignKeyName() => $record->id]; | ||
} | ||
|
||
protected function getRedirectUrlParameters(): array | ||
{ | ||
/** @var Ancestor $ancestor */ | ||
$ancestor = $this::getResource()::getAncestor(); | ||
|
||
return $ancestor->getNormalizedRouteParameters($this->getRecord()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Guava\Filament\NestedResources\Pages; | ||
|
||
use Filament\Actions\DeleteAction; | ||
use Filament\Resources\Pages\EditRecord; | ||
|
||
class NestedEditRecord extends EditRecord | ||
{ | ||
use NestedPage; | ||
|
||
protected function configureDeleteAction(DeleteAction $action): void | ||
{ | ||
$resource = static::getResource(); | ||
$ancestor = $resource::getAncestor(); | ||
|
||
if (!$ancestor) { | ||
parent::configureDeleteAction($action); | ||
return; | ||
} | ||
|
||
$ancestorResource = $ancestor->getResource(); | ||
|
||
$action | ||
->authorize($resource::canDelete($this->getRecord())) | ||
->successRedirectUrl( | ||
$resource::hasPage('index') | ||
? $resource::getUrl('index') | ||
: $ancestorResource::getUrl('edit', [ | ||
...$ancestor->getNormalizedRouteParameters($this->getRecord()), | ||
]) | ||
) | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Guava\Filament\NestedResources\Pages; | ||
|
||
use Filament\Actions\CreateAction; | ||
use Filament\Resources\Pages\ListRecords; | ||
use Filament\Tables; | ||
use Filament\Tables\Table; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class NestedListRecords extends ListRecords | ||
{ | ||
use NestedPage; | ||
|
||
protected function makeTable(): Table | ||
{ | ||
return parent::makeTable() | ||
->recordUrl(fn (Model $record) => static::getResource()::getUrl('edit', [ | ||
...static::getResource()::getAncestor()->getNormalizedRouteParameters($record), | ||
'record' => $record, | ||
])) | ||
; | ||
} | ||
|
||
protected function configureCreateAction(Tables\Actions\CreateAction | CreateAction $action): void | ||
{ | ||
parent::configureCreateAction($action); | ||
$action->url(static::getResource()::getUrl('create', $this->getRouteParameterIds())); | ||
} | ||
} |
Oops, something went wrong.