How to create a custom form field in Admin?

So I’m developing a plugin, which would show something like a plan feature comparison table

Plan 1 Plan2
Feature 1 v v
Feature 2 x v
Feature 3 x v

I have a list of plans and list of features. What I want now, is to show a grid of inputs to save values of plan-feature relations.

I can’t figure out how to create such a thing. I can’t even create a simplest custom input field. I have my Twig template in user/plugins/my-plugin/templates/forms/fields/my-field/my-field.html.twig and I have it defined in my blueprint, but it still outputs the default text field. I also have

    public function onGetPageTemplates(Event $event)
    {
        $locator = Grav::instance()['locator'];
        $event->types->scanTemplates($locator->findResource(sprintf('plugin://%s/templates', $this->name)));
    }

But I assume maybe for form field templates there’s something more required. Can’t find any docs :confused:

Hi, @Karmalakas did you add templates directory to twig lookup paths?

public function onTwigTemplatePaths(): void
{
    $this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}

There are actually some examples in the docs.

I did and these page and modular templates are available, but not the one as a form field

Not sure what’s missing, could you share your plugin’s blueprints.yaml, yourplugin.php, and yourfield.html.twig files?

Here’s what I currently have (pretty much everything except this one part)

Oh, I think I found the missing piece…

I’ll try it when I get back home

@Karmalakas

I think you should create within the root folder of your plugin a folder called admin/themes/grav/templates/forms and place your template there. Take a look at the modifications I made in the Deliver theme, in which I modified some field templates in the administration panel.

Look at the deliver.php file to adapt the code referring to the path of the Admin templates

@Karmalakas, might be useful, but first you add templates directory only outside admin (line 69), try

    public static function getSubscribedEvents()
    {
        return [
            'onPluginsInitialized' => [
                ['onPluginsInitialized', 0]
            ],
            'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
        ];
    }
1 Like

:man_facepalming: you’re correct. adding onTwigTemplatePaths to both admin and not, now shows the field output. No need for getFormFieldTypes() or moving the template deep under /admin/... as @pmoreno suggested