#Self-Registering Filament Plugins

15 messages · Page 1 of 1 (latest)

quartz merlin
#

Let's imagine I am developing a package cms (based on Filament). Now I want to offer an additional package called blog.
In my laravel project now I add the two dependencies cms and blog.
The cms has the FilamentServiceProvider to register the panel.
The blog package contains a Filament plugin.

Is there anyway, that the blog package registers it's BlogPlugin "itself" into the Filament panel?
So, both, the laravel project nor the cms package doesn't need to know anything about the blog package?

Thank you 🙂

quartz merlin
#

By using $panel = filament()->getPanel('<id>') I can actually get the Panel object – But is there a way to register a plugin after the creation of the panel?

woeful jay
#

If CMS can exist without blog I’d stick with manually registration. Otherwise just add it to CMS.

quartz merlin
#

With
if ($currentPanel = filament()->getPanel('xxx')) { $currentPanel->plugins([ BlogPlugin::make(), ]); }
I am quite near I think. But, it results into:
Route [filament.xxx.resources.posts.index] not defined.

When opening the admin.

So, the plugin got registered somehow. But not completely...

woeful jay
#

Are you running this is boot or register?

quartz merlin
#

Currently in boot()

#

Doing it in register(), as panel wasn' created yet:

woeful jay
#

Try it in packageBoot() instead. Just a thought.

#

I’m thinking it’s a laravel lifecycle issue though. Which is why manual registration usually works.

quartz merlin
#

Manual registration is not an option for me.

#

But I found a soltuion

#

solution*

#

Using this, in the blog's serviceprovider's register():

$this->app->afterResolving(FilamentPluginRegistry::class, function ($service) { $service->register('blog', BlogPlugin::class); });

and in FilamentServiceProvider's panel():

->plugins( array_map( fn ($plugin) => $plugin::make(), app(FilamentPluginRegistry::class)->all() ) );

(Of course, FilamentPluginRegistry need to be a singleton.