Documentation

EventManager

EventManager class for short- and long-term event handler registration. This class implements Singleton pattern and it is queried via getInstance().

Event object (Bitrix\Main\Event) is passed as an argument in handlers, registered via \Bitrix\Main\EventManager::AddEventHandler. When old arguments must be passed, use \Bitrix\Main\EventManager::addEventHandlerCompatible. Similar to \Bitrix\Main\EventManager::registerEventHandler and \Bitrix\Main\EventManager::registerEventHandlerCompatible.

Similar to this class are the following functions:
RegisterModuleDependences,
UnRegisterModuleDependences,
AddEventHandler,
RemoveEventHandler,
GetModuleEvents.

Examples

//version 1 
$eventManager = \Bitrix\Main\EventManager::getInstance(); 
$eventManager->registerEventHandlerCompatible("module","event","module2","class","function");
//version 2 for events in DataManager, for example
$eventManager = \Bitrix\Main\EventManager::getInstance(); 
$eventManager->registerEventHandler("module","event","module2","class","function");

Custom handlers in modules

$arMacros["PRODUCTS"]  = "";  
$basketId = "10";
$event = new \Bitrix\Main\Event("mymodule", "OnMacrosProductCreate",array($basketId));
   $event->send();
   if ($event->getResults()){
      foreach($event->getResults() as $evenResult){
         if($evenResult->getResultType() == \Bitrix\Main\EventResult::SUCCESS){
         $arMacros["PRODUCTS"] = $evenResult->getParameters();
      }
   }
}
$eventManager = \Bitrix\Main\EventManager::getInstance();
$eventManager->addEventHandler("mymodule", "OnMacrosProductCreate", "OnMacrosProductCreate");

function OnMacrosProductCreate(\Bitrix\Main\Event $event){
   $arParam = $event->getParameters();
   $basketId = $arParam[0];
   $result = new \Bitrix\Main\EventResult(1,$basketId);
    return $result;
}
use Bitrix\Main\EventManager;

$handler = EventManager::getInstance()->addEventHandler(
    "main",
    "OnUserLoginExternal",
    array(
        "Intervolga\\Test\\EventHandlers\\Main",
        "onUserLoginExternal"
    )
);
EventManager::getInstance()->removeEventHandler(
    "main",
    "OnUserLoginExternal",
    $handler
);
EventManager::getInstance()->registerEventHandler(
    "main",
    "OnProlog",
    $this->MODULE_ID,
    "Intervolga\\Test\\EventHandlers",
    "onProlog"
);
EventManager::getInstance()->unRegisterEventHandler(
    "main",
    "OnProlog",
    $this->MODULE_ID,
    "Intervolga\\Test\\EventHandlers",
    "onProlog"
);
$handlers = EventManager::getInstance()->findEventHandlers("main", "OnProlog"); 


© «Bitrix24», 2001-2024