Introduction
In this walkthrough, we’ll build a simple plugin that adds a new form component that can be used in forms. This also means it will be available to users in their panels. You can find the original plugin at awcodes/headings. The examples below use Filament 4 APIs.Preface
Please read the getting started guide and the documentation on custom form fields 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:binconfigdatabasesrc/Commandssrc/Facadesstubs
composer.json. Keep the Awcodes\Headings\ mapping for src/ and the service provider registration under extra.laravel.providers.
For this example, you’ll provide a stylesheet that loads only where the component is used, using x-load-css. Update package.json to include cssnano, postcss, postcss-cli and postcss-nesting to build it.
postcss.config.js file to configure postcss.
Step 3: Setting up the provider
Now that we have our plugin cleaned up, we can start adding our code. The boilerplate in thesrc/HeadingsServiceProvider.php file has a lot going on so, let’s delete everything and start from scratch.
We need to be able to register our stylesheet with the Filament Asset Manager so that we can load it on demand in our Blade view. To do this, we’ll need to add the following to the packageBooted method in our service provider.
Note the loadedOnRequest() method. This is important, because it tells Filament to only load the stylesheet when it’s needed.
Step 4: Creating our component
Next, we’ll need to create our component. Create a new file atsrc/Heading.php and add the following code.
Step 5: Rendering our component
Next, we’ll need to create the view for our component. Create a new file atresources/views/heading.blade.php and add the following code.
Use x-load-css to load the stylesheet only where the component is rendered. The attribute bag’s color() method supplies Filament’s text color variables for both light and dark mode. See lazy loading CSS and colors for more information.
Step 6: Adding some styles
Next, let’s provide some custom styling for our field. We’ll add the following toresources/css/index.css. And run npm run build to compile our CSS.
Step 7: 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: