Answer the question
In order to leave comments, you need to log in
How to properly connect an event handler in your module?
Good afternoon, tell me please.
I am writing my module and now at the moment of connecting my event handler.
What I need: Register an event handler for order status changes in the system, and write a function for it in a separate file.
How I do now: I register like this:
$eventManager = \Bitrix\Main\EventManager::getInstance();
$eventManager->registerEventHandler('sale', 'OnSaleStatusOrder', $this->MODULE_ID, 'YoloEvents', 'My_OnSaleStatusOrder');
class YoloEvents
{
public static function My_OnSaleStatusOrder($order_id, $status_val){
if($status_val == 'F'){
$order = Sale\Order::load($order_id);
$order->setField("STATUS_ID", "qq");
$order->save();
}
}
}
Answer the question
In order to leave comments, you need to log in
In install/index.php of the module, you need to register the dependency using this function
https://dev.1c-bitrix.ru/api_help/main/functions/m...
And put the file in lib/ so that autoloading of Bitrix classes works
Example:
In install/index.php
function InstallEvents()
{
RegisterModuleDependences('search', 'BeforeIndex', 'mymodule.smartsearch', '\\MyModule\\SmartSearch\\EventHandler', 'BeforeIndex');
}
function UnInstallEvents()
{
UnRegisterModuleDependences('search', 'BeforeIndex', 'mymodule.smartsearch', '\\MyModule\\SmartSearch\\EventHandler', 'BeforeIndex');
}
namespace MyModule\SmartSearch;
class EventHandler
{
function BeforeIndex($arFields)
{
\CModule::IncludeModule("iblock");
if ($arFields["MODULE_ID"] == "iblock") {
$result = \CIBlockElement::GetByID($arFields["ITEM_ID"]);
if ($element = $result->Fetch()) {
$arFields['PARAM1'] = $element['IBLOCK_SECTION_ID'];
}
}
return $arFields;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question