1

I have a custom plugin in a custom plugin group (maidan):

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.1" type="plugin" group="maidan" method="upgrade">
    <name>My custom plugin</name>

It is in a custom group because my component uses that group for certain things.

Now i want to trigger an event when unpublishing an item from my component. I'm doing that using this in the publish method inside the model:

$event = AbstractEvent::create("onMyObjectAfterStateChange", [ 'subject' => $this, 'pks' => $cids, 'state' => $state]);
$eventResult = Factory::getApplication()->getDispatcher()->dispatch("onMyObjectAfterStateChange", $event);

In a system plugin i can do this to react:

public function onMyObjectAfterStateChange(EventInterface $event)
{
    echo "onMyObjectAfterStateChange";
    die();

But in my custom group plugin i cannot get the event trigered. I tried importing my group befor triggering the event:

PluginHelper::importPlugin('maidan');

Also i tried using the Subsriberinterface inside my plugin to catch the event, but nothing happens:

defined('_JEXEC') or die;

class Myplugin extends CMSPlugin implements SubscriberInterface
{
    public function __construct(&$subject, $config)
    {
        die("---");
        parent::__construct($subject, $config);
    }

    public static function getSubscribedEvents(): array
    {
        return [
            'onMyObjectAfterStateChange' => 'onMyObjectAfterStateChange',
        ];
    }

    public function onMyObjectAfterStateChange(EventInterface $event)
    {
        echo "onMyObjectAfterStateChange";
        die("---");
    }
}
5
  • 2
    Does your plugin have a service provider (services/provider.php file)?
    – Sharky
    Dec 11, 2023 at 13:52
  • That was it. Thanks.
    – maidan
    Dec 11, 2023 at 14:39
  • But i still have to do this before triggering the plugin, right: PluginHelper::importPlugin('maidan');
    – maidan
    Dec 11, 2023 at 14:51
  • Yes, that's correct.
    – Sharky
    Dec 11, 2023 at 20:25
  • 1
    Could Maidan or @Sharky please post the resolution as an Answer to help identify this Question as resolved.
    – Irata
    Dec 11, 2023 at 23:59

1 Answer 1

0

The plugin needs to be constructed using namespaces and a service provider, since I presume @maiden is building the plugin for Joomla 4+.

This means first adding the namespace tag to the plugin xml file, of the format

<namespace path="src">MyCompany\Plugin\System\MyExample</namespace>

Then instead of the usual single php file, e.g. myexample.php, it is necessary to create two folders, once called services the other src. In the services folder you put the provider.php file, which returns an anonymous PHP class which implements the Joomla dependency injection interface Joomla\DI\ServiceProviderInterface.

In the folder src you put your plugin files with the plugin php class. For details explanation of how to construct plugin for Joomla 4+, you should refer to the section of book by Nicholas Dionysopoulos about plugin construction. If building a provider.php service class is completely new to you, you should also read the section about service provide from the same book.

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.