Event procedures are triggered on certain events if the given filters apply. This tutorial will show you how to add custom event procedures and filters with a plugin. To achieve this, excerpts of a small plugin with the name ProcedurePlugin
will be shown and explained.
This chapter will show you how to add a custom procedure. A procedure is the part of an event procedure that will be executed if filters apply. The example procedure here will set the status of an order to 3.
The procedure has to be registered in the boot
method of a service provider of the plugin by using EventProceduresService
.
ProcedurePlugin\Providers\ProcedurePluginServiceProvider
<?php namespace ProcedurePlugin\Providers; use Plenty\Plugin\ServiceProvider; use Plenty\Modules\EventProcedures\Services\EventProceduresService; use Plenty\Modules\EventProcedures\Services\Entries\ProcedureEntry; use ProcedurePlugin\EventProcedures\Procedures; class ProcedurePluginServiceProvider extends ServiceProvider { /** * @param EventProceduresService $eventProceduresService * @return void */ public function boot(EventProceduresService $eventProceduresService) { $eventProceduresService->registerProcedure( 'setStatus', ProcedureEntry::EVENT_TYPE_ORDER, ['de' => 'Setze Status auf 3', 'en' => 'Set status to 3'], Procedures::class . '@setStatus' ); } }
registerProcedure
method is the module name. You can choose the name freely. The event type is the second parameter. In this example the type is EVENT_TYPE_ORDER
. This way the procedure will be available for order related event procedures. The third parameter is an array with the translations of the filter name. The name will be displayed in the UI and has to be provided in German and English. The fourth parameter that must be specified is a class name. The class name specifies the method that will be called when the procedure is executed. The class name must be fully qualified. A method name can be provided by separating it with an '@'. If no method is provided, the default method execute
will be used.
Define the procedures in the class that you specified at the registration.
ProcedurePlugin\EventProcedures\Procedures
<?php namespace ProcedurePlugin\EventProcedures; use Plenty\Modules\EventProcedures\Events\EventProceduresTriggered; use Plenty\Modules\Order\Contracts\OrderRepositoryContract; class Procedures { /** * @param EventProceduresTriggered $event * @return void */ public function setStatus(EventProceduresTriggered $event) { $order = $event->getOrder(); $orderRepository = pluginApp(OrderRepositoryContract::class); $orderRepository->updateOrder(['statusId' => 3], $order->id); } }
getOrder
method. You need to declare the type of the parameter that you add as EventProceduresTriggered
.
This chapter will show you how to add a custom filter. You can use filters to define under which circumstances an event procedure will be executed. The filter in our example will check if the order is locked.
The filter has to be registered in the boot
method of a service provider of the plugin by using EventProceduresService
.
ProcedurePlugin\Providers\ProcedurePluginServiceProvider
<?php namespace ProcedurePlugin\Providers; use Plenty\Plugin\ServiceProvider; use Plenty\Modules\EventProcedures\Services\EventProceduresService; use Plenty\Modules\EventProcedures\Services\Entries\ProcedureEntry; use ProcedurePlugin\EventProcedures\Filters; class ProcedurePluginServiceProvider extends ServiceProvider { /** * @param EventProceduresService $eventProceduresService * @return void */ public function boot(EventProceduresService $eventProceduresService) { $eventProceduresService->registerFilter( 'orderLocked', ProcedureEntry::EVENT_TYPE_ORDER, ['de' => 'Auftrag ist gesperrt', 'en' => 'Order is locked'], Filters::class . '@orderLocked' ); } }
accept
will be used.
Define the filter logic in the class that you specified at the registration.
ProcedurePlugin\EventProcedures\Filters
<?php namespace ProcedurePlugin\EventProcedures; use Plenty\Modules\EventProcedures\Events\EventProceduresTriggered; class Filters { /** * @param EventProceduresTriggered $event * @return boolean */ public function orderLocked(EventProceduresTriggered $event) { return $event->getOrder()->lockStatus != 'unlocked'; } }
EventProceduresTriggered
and call the getOrder
method.