简体   繁体   English

shopware 5中的事件优先级

[英]event priority in shopware 5

i have 2 plugins both use same event Enlight_Controller_Action_PostDispatchSecure_Frontend_Checkout in plugin A i assing a variable to smarty and need that variable for my other plugin.我有 2 个插件都在插件 A 中使用相同的事件Enlight_Controller_Action_PostDispatchSecure_Frontend_Checkout我为 smarty 分配一个变量,并且我的其他插件需要该变量。 but plugin B runs before plugin A. so i set priorities and tested it with a var_dump();但是插件 B 在插件 A 之前运行。所以我设置了优先级并使用 var_dump();

Plugin A:插件 A:

 public static function getSubscribedEvents()
{
    return [
        'Enlight_Controller_Action_PostDispatchSecure_Frontend_Checkout' => array('onCheckout', 1)
    ];
}
public function onPostDispatchCheckout(\Enlight_Event_EventArgs $args)
    {
        var_dump("Plugin A");
    }

Plugin B:插件 B:

public static function getSubscribedEvents()
{
    return [
        'Enlight_Controller_Action_PostDispatchSecure_Frontend_Checkout' => array('onCheckout', 2)
    ];
}
    public function onPostDispatchCheckout(\Enlight_Event_EventArgs $args)
    {
        var_dump("Plugin B");
    }

now when I run it, the output is:现在当我运行它时,输出是:

plugin B, Plugin A

but plugin A must run first, what am I doing wrong ?但是插件A必须先运行,我做错了什么? Thanks谢谢

The higher the number, the earlier a subscriber is executed.数字越大,订阅者执行得越早。 So you have to change the priorities.所以你必须改变优先级。

Plugin A:插件 A:

 public static function getSubscribedEvents()
{
    return [
        'Enlight_Controller_Action_PostDispatchSecure_Frontend_Checkout' => array('onCheckout', 2)
    ];
}
public function onPostDispatchCheckout(\Enlight_Event_EventArgs $args)
    {
        var_dump("Plugin A");
    }

Plugin B:插件 B:

public static function getSubscribedEvents()
{
    return [
        'Enlight_Controller_Action_PostDispatchSecure_Frontend_Checkout' => array('onCheckout', 1)
    ];
}
    public function onPostDispatchCheckout(\Enlight_Event_EventArgs $args)
    {
        var_dump("Plugin B");
    }

As Pawel mentioned you can set the priority in the event subscription.正如 Pawel 提到的,您可以在事件订阅中设置优先级。 There are two syntaxes to do this有两种语法可以做到这一点

event => [subscriber => priority]事件 => [订阅者 => 优先级]

'Enlight_Controller_Action_PostDispatchSecure_Frontend_Checkout' => ['onCheckout' => 1] 'Enlight_Controller_Action_PostDispatchSecure_Frontend_Checkout' => ['onCheckout' => 1]

or或者

event => [subscriber, priority]事件 => [订阅者,优先级]

'Enlight_Controller_Action_PostDispatchSecure_Frontend_Checkout' => ['onCheckout', 1] 'Enlight_Controller_Action_PostDispatchSecure_Frontend_Checkout' => ['onCheckout', 1]

As far as I know its like z-index, the higher one wins.据我所知,它就像 z-index,较高的获胜。 And you might need to reinstall the plugin and clear the cache.您可能需要重新安装插件并清除缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM