Advanced Guides

Custom Fields

Custom Fields allow you to have reusable [Filament Fields](https://filamentphp.com/docs/3.x/forms/fields/getting-started) or groups of Fields to help streamline Content Block creation that may reuse similar logic.

Usage

To create a new Custom Field, run the following command:

    
1php artisan make:autoload-field CUSTOM_FIELD_NAME

This will place a new file in your Laravel application under app/Filament/Content/Fields/ that looks like this:

    
1namespace App\Filament\Content\Fields;
2 
3use Filament\Forms;
4use Mergeloop\Autoload\Filament\Content\AutoloadField;
5 
6class MyCustomField extends AutoloadField
7{
8 public function handle(): array
9 {
10 return [
11 // ...
12 ];
13 }
14}

From here, you can add any [Filament fields](https://filamentphp.com/docs/3.x/forms/fields/getting-started) you wish. You may also include other Custom Fields as well!

Field Parameters

You may also add parameters to your Custom Fields that enable you to have conditional logic. An example is setting a Field's "required" parameter.

For this, you will need to create a new class property and setter method within you Custom Field class:

    
1namespace App\Filament\Content\Fields;
2 
3use Filament\Forms;
4use Mergeloop\Autoload\Filament\Content\AutoloadField;
5 
6class MyCustomField extends AutoloadField
7{
8 public bool $required = false;
9 
10 public function required(bool $required = true): static
11 {
12 $this->required = $required;
13 
14 return $this;
15 }
16 
17 public function handle(): array
18 {
19 return [
20 // $this->required
21 ];
22 }
23}
Note

It is important to note that your setter methods must return the static $this!

You can then call these methods when initializing your Custom Field like so;

    
1\App\Filament\Content\Fields\MyCustomField::make('name')
2 ->required(true|false)

Then, within your Custom Field, you can access this parameter via its property:

    
1\Filament\Forms\Components\TextInput::make('name')
2 ->required($this->required)

Autoload Fields

Autoload comes pre-bundled with several useful Custom Fields already that you can use. Include them within your Content Block's layout file in place of a Filament field. You can pass parameters to each of them within their make() method, examples below:

Text

    
1// Standard
2\Mergeloop\Autoload\Filament\Content\Fields\TextField::make('text');
3 
4// With Parameters
5\Mergeloop\Autoload\Filament\Content\Fields\TextField::make('text')
6 // the label displayed to the CMS User when editing this field, default is "Text"
7 ->label('Text')
8 // the Filament field rules, default is "['nullable']"
9 ->rules(['nullable']);

Media

It is recommended to use this Autoload-provided custom field for all Media Assets that you wish to include in your Content Block

    
1// Standard
2\Mergeloop\Autoload\Filament\Content\Fields\MediaField::make('image');
3 
4// With Parameters
5\Mergeloop\Autoload\Filament\Content\Fields\MediaField::make('image')
6 // the label displayed to the CMS User when editing this field, default is "Image"
7 ->label('Image')
8 // Whether the field is required, default is "true"
9 ->required()
10 // Whether multiple Media Assets can be selected, default is "false"
11 ->multiple();

Button

    
1// Standard
2\Mergeloop\Autoload\Filament\Content\Fields\ButtonField::make()
3 
4// With Parameters
5\Mergeloop\Autoload\Filament\Content\Fields\ButtonField::make()
6 // the key that is used to access this fields "text" value within the Blade view, default is "button_text"
7 ->textProperty('button_text')
8 // the label displayed to the CMS User when editing this fields "text", default is "Button Text"
9 ->textLabel('Button Text')
10 // the Filament fields "text" rules, default is "['nullable']"
11 ->textRules(['nullable'])
12 // the key that is used to access this fields "link" value within the Blade view, default is "button_link"
13 ->linkProperty('button_link')
14 // the label displayed to the CMS User when editing this fields "link", default is "Button Link"
15 ->linkLabel('Button Link')
16 // the Filament fields "link" rules, default is "['nullable']"
17 ->linkRules(['nullable'])
18 // An additional array of selectable options for links, default is "[]"
19 ->linkOptions([])
20 // the key that is used to access this fields "style" value within the Blade view, default is "button_style"
21 ->styleProperty('button_style')
22 // the label displayed to the CMS User when editing this fields "style", default is "Button Style"
23 ->styleLabel('Button Style')
24 // the Filament fields "style" rules, default is "['nullable']"
25 ->styleRules(['nullable'])
26 // the text displayed when a user is searching for a predefined link, default is nothing.
27 ->searchMessage()
28 // Whether the field will display all registered Pages Slugs as an available Link option, default is "true"
29 ->includePages(true|false)
30 // Whether the field will display all the available Media Assets path as an available Link option, default is "true"
31 ->includeMedia(true|false)
32 // An additional array of selectable options for preset Button styles
33 ->options([
34 'primary' => 'Primary',
35 'secondary' => 'Secondary',
36 'tertiary' => 'Tertiary',
37 ]);

Custom Link

If you wish to have the ability to link directly to a Page or a Media Asset without typing out the full URL or relative path, you can use this custom field:

    
1// Standard
2\Mergeloop\Autoload\Filament\Content\Fields\CustomLinkField::make('custom_link');
3 
4// With Parameters
5\Mergeloop\Autoload\Filament\Content\Fields\CustomLinkField::make('custom_link')
6 // the label displayed to the CMS User when editing this field, default is "Custom Link"
7 ->label('Custom Link')
8 // the Filament field rules, default is "['nullable']"
9 ->rules(['nullable'])
10 // Whether the field will display all registered Pages and their Slugs, default is "true"
11 ->includePages(true|false)
12 // Whether the field will display all the available Media Assets and their paths, default is "true"
13 ->includeMedia(true|false)
14 // Whether the field can be searchable, default is "true"
15 ->creatable(true|false)
16 // Whether the field will allow the User to create their own link without selecting an existing Page or Media Asset, default is "true"
17 ->searchable(true|false)
18 // An additional array of selectable options for links, default is "[]"
19 ->options([]);

Simple Repeater

    
1// Standard
2\Mergeloop\Autoload\Filament\Content\Fields\SimpleRepeaterField::make('repeater');
3 
4// With Parameters
5\Mergeloop\Autoload\Filament\Content\Fields\SimpleRepeaterField::make('repeater')
6 // the label displayed to the CMS User when editing this field, default is "Simple Repeater"
7 ->label('Simple Repeater')
8 // The label on the button to add a new item to the repeater, default is "Add Item"
9 ->buttonLabel('Add Item')
10 // The unique, repeatable block key, default is "Block"
11 ->block(;block)
12 // If included, will display as the label for the block. If not, then numeric values will be used. Default is "null"
13 ->blockLabel(null)
14 // Array of grouped Filament Fields
15 ->schema([]);
Previous
Styling Override
Code highlighting provided by Torchlight