Introduction
In this walkthrough, you’ll build a plugin that adds a clock widget to a panel. You can find the original plugin at awcodes/clock-widget. The examples below use Filament 4 APIs.Preface
Please read the docs on panel plugin development and the getting started guide before continuing.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. Keep the Awcodes\ClockWidget\ mapping for src/ and the service provider registration under extra.laravel.providers.
The last step is to update the 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.
Register this asynchronous Alpine component in
packageBooted(), since it only loads where the widget is used. For assets that should load on every page of a particular panel, use $panel->assets() in a plugin object’s register() method instead. Registering those assets globally would load them in panels that do not use your plugin.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.
Use x-load and x-load-src to load the Alpine component when the widget is rendered. See asynchronous Alpine.js components for more information.
src/js/index.js. And build our assets with npm run build.
resources/lang/en/clock-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: