Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

Does anyone know how to call another plugins public methods?

Started by Peter Gilmour 8 years ago · 2 replies · 1061 views
8 years ago

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!

7 years ago

Hi @peterg87, welcome to the Grav Forum.

Yes, well that's a good one!

I was hoping that something like

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

7 years ago

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(...).

Suggested topics

Topic Participants Replies Views Activity
Support · by TomW, 2 weeks ago
8 228 10 hours ago
Support · by alex, 2 months ago
8 667 1 day ago
Support · by Supple, 5 days ago
1 98 1 day ago
Support · by TomW, 1 week ago
3 160 3 days ago
Support · by TomW, 6 days ago
2 106 6 days ago