1

I have created a (private) plugin, ginacpmods. The main class is called GinaCp which extends Plugin.

The plugin has an admin page which is rendered with templates/index.twig. This twig file contains a button which, will execute craft.ginacpmods.clearAll(). This doesn't work. All I get back is

Calling unknown method: craft\web\twig\variables\CraftVariable::ginacpmods()

I've tried referencing ginacp, ginacpmods, _ginacpmods but neither works. I'm starting to wonder what I'm missing.

The function I'm calling is dead simple right now and doesn't really do anything yet:

plugins/ginacpmods/src/GinaCp.php:
class GinaCp extends Plugin {
    [...]

    public function clearAll(): bool|array|string {
        return 'Done.';
    }

Is there something I need to do in order to expose the clearAll() function to twig?

1 Answer 1

1

Turns out I was missing the crucial part of exposing the class to twig itself. I didn't know I had to do that.

So the solution ended up being to add this to the plugin init:

Event::on(
    CraftVariable::class,
    CraftVariable::EVENT_INIT,
    function (Event $event) {
        $variable = $event->sender;
        $variable->set('ginacpmods', GinaCp::class);
    }
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.