Does anyone know how to call another plugins public methods?

I have run into a wall trying to call a public function from another plugin.

The $this->grav[‘plugins’]->get(‘my_plugin’) doesn’t return an instance of the plugin.

Please help!

Hi @peterg87, welcome to the Grav Forum.

Yes, well that’s a good one!

I was hoping that something like

public function onPageInitialized()
    {

        dump(FormPrefillerPlugin::getFilePath('user://data/test.yaml'));exit;

in one plugin would call that function in another plugin.

In fact it does but unfortunately fails with a “Using $this when not in object context” error.

My knowledge of PHP and Grav is too limited so for me it stops here. I hope someone else steps in. If not I suggest you post in in the Grav Discord Chat as this is really a developers question.

You can access public, static methods, but not class instances: https://github.com/getgrav/grav/pull/2277

If the plugin you’re wanting to access delivers a service that can be instantiated, such as a PSR-4 class, you can spawn that: $myPluginItem = new \Grav\Plugin\MyPlugin\Item;.

Or for a static method within the main plugin: $filepath = \Grav\Plugin\FormPrefillerPlugin::getFilePath(...).