Use class for field definition

Hi there.

I like to outsource the definition of field props, methods… to a module. Something like:

Kirby::plugin('brand/name', [
	'fields' => [
        'myfield' => new MyField(), // <- But it expecting an Array 🤷‍♂️
    ]
);

I don’t want to deal with static methods or even worse, with outside defined variables.
Do someone could give me little a hint to achieve this?

Btw. Is there a possibility to access props from a field in a field method? Now i jam some props that i needed into files value. This is hard to handle and is a big mess.

I’m really, really thankful for some inputs…

You can use field as class like following:

Kirby::plugin('brand/name', [
	'fields' => [
        'myfield' => MyField::class
    ]
);

@Microman Did you test it? This is how I use it in my own plugins. It has to be working.

I test it. :+1:

It’s to mention, that the custom class needs to be in the namespace of Kirby\Form and inhered from Field class:

index.php


load([
    'Kirby\\Form\\MyField'  => __DIR__ . '/lib/MyField.php',
]);

/lib/MyField.php

namespace Kirby\Form;

class MyField extends Field
{
	public function __construct(array $params = [])
	{
		// Do stuff with the field.
		parent::__construct($params);
	}
}