Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Forms & Blueprints

How to create a custom form field in Admin?

admin field form plugin plugins

Solved by Vadym View solution

Started by Karmalakas 1 year ago · 8 replies · 246 views
1 year 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 😕

1 year 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.

1 year ago

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

1 year ago

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

1 year 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
1 year ago Solution

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

JS
    public static function getSubscribedEvents()
    {
        return [
            'onPluginsInitialized' => [
                ['onPluginsInitialized', 0]
            ],
            'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
        ];
    }
👍 1
last edited 02/25/25 by Vadym
1 year 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, 5 years ago
13 1133 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 60 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 131 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 107 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 125 7 months ago