ibrostudio/filament-dynamic-resource-children

Allows Filament plugins developer to register pages or relation managers in a resource living at the app level or in an another plugin

v1.0.0 2022-06-10 18:00 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status

This package allows Filament plugins developer to register pages or relation managers in a resource living at the app level or in an another plugin.

Installation

You can install the package via composer:

composer require ibrostudio/filament-dynamic-resource-children

Usage

Prepare your resource

Add the trait CanHaveDynamicChildren to your resource class.

use IBroStudio\FilamentDynamicResourceChildren\Concerns\HasDynamicChildren;

class ExampleParentResource extends Resource
{
    use HasDynamicChildren;
}

Then, in that resource class, modify getRelations() and getPages() methods:

Before:

    public static function getRelations(): array
    {
        return [];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListExampleParents::route('/'),
            'create' => Pages\CreateExampleParent::route('/create'),
            'view' => Pages\ViewExampleParent::route('/{record}'),
            'edit' => Pages\EditExampleParent::route('/{record}/edit'),
        ];
    }

After:

    public static function getRelations(): array
    {
        return array_merge(
            [],
            self::getDynamicRelationManagers()
        );
    }

    public static function getPages(): array
    {
        return array_merge(
            [
                'index' => Pages\ListExampleParents::route('/'),
                'create' => Pages\CreateExampleParent::route('/create'),
                'view' => Pages\ViewExampleParent::route('/{record}'),
                'edit' => Pages\EditExampleParent::route('/{record}/edit'),
            ],
            self::getDynamicPages()
        );
    }

Link your children with their parent

Create your page or relation manager as usual in your plugin.

Then register it in your plugin service provider like this:

use App\Filament\Resources\ExampleParentResource;
use Filament\PluginServiceProvider;
use Spatie\LaravelPackageTools\Package;
use VendorName\MyPlugin\Filament\Resources\ExampleParentResource\Pages\MyFirstPage;
use VendorName\MyPlugin\Filament\Resources\ExampleParentResource\Pages\MySecondPage;
use VendorName\MyPlugin\Filament\Resources\ExampleParentResource\RelationManagers\MyFirstRelationManager;
use VendorName\MyPlugin\Filament\Resources\ExampleParentResource\RelationManagers\MySecondRelationManager;

class MyPluginServiceProvider extends PluginServiceProvider
{
    protected array $pages = [
        MyFirstPage::class,
        MySecondPage::class,
    ];

    protected array $relationManagers = [
        MyFirstRelationManager::class,
        MySecondRelationManager::class,
    ];

    public function configurePackage(Package $package): void
    {
        $this->packageConfiguring($package);

        $package
            ->name('my-plugin')
            ->hasViews();
    }

    public function packageRegistered(): void
    {
        parent::packageRegistered();

        $this->app->resolving('filament', function () {

            ExampleParentResource::addDynamicPages([
                'first-page-key' => [
                    'class' => MyFirstPage::class,
                    'route' => '/first-page-slug'
                ],
                'second-page-key' => [
                    'class' => MySecondPage::class,
                    'route' => '/second-page-slug'
                ],
            ]);

            ExampleParentResource::addDynamicRelationManagers([
                MyFirstRelationManager::class,
                MySecondRelationManager::class,
            ]);

        });
    }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.