1

I have created a "custom" authentication plugin and inside of it there is a function (onAjaxMyfunction) that is called from a module through com_ajax.

The call "url" looks like this:

/index.php?option=com_ajax&plugin=name_of_the_plugin&group=authentication&format=raw

Is it possible to call another function from the same plugin through ajax (ie onAjaxMySecondFunction)?

I have already tried to modify the url by adding $method = name_of_the_method at the end, but it keeps calling the function onAjaxMyfunction instead of onAjaxMySecondFunction.

I also though to create a second plugin which extends the class of the first so I can make a separate ajax call there. But by doing this I am not sure how can I extend the class of the first plugin. Both are under authentication folder.

So plugin A looks like this:

class PlgAuthenticationMycustomauth extends CMSPlugin

How should I write it to extend it? I tried the following:

class PlgAuthenticationMySecondPlugin extends PlgAuthenticationMycustomauth

but that didn't work.

2 Answers 2

2

Plugins call a single method based on the name of the plugin. But you can pass anything to the method including the name of another method you would like to run.

Hence, running a call like the following.

<script>
jQuery.ajax({
    url: 'index.php?option=com_ajax&plugin=MyPluginName&group=authentication&format=raw&custom_method=TestB',
    type: "get",
    success :function(response){
        console.log(response);
    }
});
</script>

You can catch the custom method and (after validating it) use it to run any method.

protected $app;

public function onAjaxMyPluginName()
{
    $valid_methods = ['TestA','TestB'];
    $requested_method = $this->app->input->get('custom_method');
    $method = in_array($requested_method, $valid_methods) ? $requested_method : 'TestA';
    return $this->{$method}();
}

private function TestA()
{
    return 'sup';
}

private function TestB()
{
    return 'hey';
}
1
  • Wow thanks a lot! :) I did a workaround like you mentioned but with switch inside the ajax function. So I "catch" the url (/index.php?option=com_ajax&plugin=My_Plugin&group=authentication&task=function1_task&format=raw) and whatever the task is I call the function I want.
    – anthi_p
    Commented Jan 31, 2023 at 7:15
1

To clarify, com_ajax does not call any method in your plugin. Instead, it triggers a regular event to which all loaded plugins can respond. So in theory the plugin query variable should be the name of the event and not the name of your plugin. This means a single plugin can have multiple AJAX events by using different plugin parameter in the com_ajax request. In case you want to avoid other plugins from butting in, you would need to ensure the uniqueness of the event name. So using something like the full plugin name is still an option. You can then add a suffix to allow different events or use additional query parameters like shown in James's answer.

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.