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:

Here is a template that’s worked for me:

index.php


namespace mybrand;

use Kirby\Cms\App as Kirby;

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


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

/lib/MyField.php

namespace mybrand;

use Kirby\Form\FieldClass;

class MyField extends FieldClass
{

	protected $myProps;

    public function __construct(array $params = [])
    {
		$this->setMyProps($params['myProps'] ?? null);
        return parent::__construct($params);
    }

	public function props(): array
	{
		return A::merge(parent::props(), [
			'myProps'       => $this->myProps(),
		]);
	}

	protected function setMyProps($myProps = null)
	{
		$this->myProps = $myProps;
	}
}