Skip to content
Grav 2.1 is out: every page speaks Markdown. Read the announcement →

How to create a custom form field in Admin?

admin field form plugin plugins

Solved by Vadym View solution

Started by Karmalakas 2 years ago · 8 replies · 873 views
2 years ago

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

PHP
    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 😕

2 years ago

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

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

There are actually some examples in the docs.

2 years ago

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

2 years ago

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

2 years ago

@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

last edited 02/25/25 by Pedro M
2 years ago

@b.da:
you add templates directory only outside admin

: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

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 6 years ago
13 2070 7 months ago
Forms & Blueprints · by Hugo Oliveira, 8 months ago
0 573 8 months ago
Forms & Blueprints · by Flachy Joe, 9 months ago
9 663 9 months ago
Forms & Blueprints · by Augustus, 9 months ago
7 713 9 months ago
Forms & Blueprints · by Julien, 10 months ago
10 699 10 months ago