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.

Support

Does anyone know how to call another plugins public methods?

Started by Peter Gilmour 7 years ago · 2 replies · 802 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.

6 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 Thomas, 1 week ago
3 66 10 minutes ago
Support · by Anna, 3 days ago
2 72 21 hours ago
Support · by Justin Young, 22 hours ago
1 40 22 hours ago
Support · by Duc , 1 week ago
2 76 6 days ago
Support · by Colin Hume, 1 week ago
2 70 6 days ago