Skip to content
● Grav 2.2 is out: 2x faster cold starts and 96% less memory on big sites. Read the announcement →

Does anyone know how to call another plugins public methods?

Started by Peter Gilmour 7 years ago · 2 replies · 962 views
7 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 webmasterodp, 5 days ago
4 131 4 days ago
Support · by nwhitfield, 6 days ago
2 111 6 days ago
Support · by stell, 2 weeks ago
5 356 1 week ago
Support · by gravinator, 1 month ago
5 547 1 week ago
Support · by Gravver, 3 weeks ago
6 340 3 weeks ago