Preface
Please read the docs on panel plugin development and the getting started guide before continuing.Introduction
In this walkthrough, we’ll build a simple plugin that adds a new form field that can be used in forms. This also means it will be available to users in their panels. You can find the final code for this plugin at https://github.com/awcodes/clock-widget.Step 1: Create the plugin
First, we’ll create the plugin using the steps outlined in the getting started guide.Step 2: Clean up
Next, we’ll clean up the plugin to remove the boilerplate code we don’t need. This will seem like a lot, but since this is a simple plugin, we can remove a lot of the boilerplate code. Remove the following directories and files:configdatabasesrc/Commandssrc/Facadesstubs
ClockWidgetPlugin.php file.
ClockWidgetPlugin.php
resources/csspostcss.config.js
composer.json file to remove unneeded options.
package.json file to remove unneeded options. Replace the contents of package.json with the following.
Step 3: Setting up the provider
Now that we have our plugin cleaned up, we can start adding our code. The boilerplate in thesrc/ClockWidgetServiceProvider.php file has a lot going on so, let’s delete everything and start from scratch.
In this example, we will be registering an async Alpine component. Since these assets are only loaded on request, we can register them as normal in the
packageBooted() method. If you are registering assets, like CSS or JS files, that get loaded on every page regardless of if they are used or not, you should register them in the register() method of the Plugin configuration object, using $panel->assets(). Otherwise, if you register them in the packageBooted() method, they will be loaded in every panel, regardless of whether or not the plugin has been registered for that panel.packageBooted method in our service provider. This will register our widget component with Livewire and our Alpine component with the Filament Asset Manager.
Step 4: Create the widget
Now we can create our widget. We’ll first need to extend Filament’sWidget class in our ClockWidget.php file and tell it where to find the view for the widget. Since we are using the PackageServiceProvider to register our views, we can use the :: syntax to tell Filament where to find the view.
resources/views/widget.blade.php and add the following code. We’ll make use of Filament’s Blade components to save time on writing the HTML for the widget.
We are using async Alpine to load our Alpine component, so we’ll need to add the x-load attribute to the div to tell Alpine to load our component. You can learn more about this in the Core Concepts section of the docs.
src/js/index.js. And build our assets with npm run build.
resources/lang/en/widget.php.
Step 5: Update your README
You’ll want to update yourREADME.md file to include instructions on how to install your plugin and any other information you want to share with users, Like how to use it in their projects. For example: