Answer the question
In order to leave comments, you need to log in
How to correctly connect an event handler for highload blocks in your module?
I am writing my own module and it needs to add the functionality of logging events that occurred with the elements of highload blocks during the operation of the site, it should fix the number of added/changed/deleted elements.
How to register a handler correctly?
In the install/index.php file, in the InstallEvents method, I wrote the following code similar to event handling for other Bitrix modules:
$eventManager = \Bitrix\Main\EventManager::getInstance();
$eventManager->registerEventHandler('highloadblock','OnBeforeAdd', 'my_module', 'MyModuleClass', 'Handler');
$hlblock = \Bitrix\Highloadblock\HighloadBlockTable::getById(12)->fetch();
$entity = \Bitrix\Highloadblock\HighloadBlockTable::compileEntity($hlblock);
$eventManager = \Bitrix\Main\EventManager::getInstance();
$eventManager->addEventHandler('', $entity->getName().'OnAdd', '\Partner\Myentity\OnAdd');
Answer the question
In order to leave comments, you need to log in
Yes, you first need to get a list of all infoblocks present on the site, and hang up handlers in the loop
use Bitrix\Highloadblock\HighloadBlockTable;
use Bitrix\Main\EventManager;
$eventManager = EventManager::getInstance();
$items = HighloadBlockTable::getList([])->fetchAll();
foreach ($items as $item) {
$eventManager->addEventHandler('', $item['NAME'] . 'OnAfterAdd', [MyModuleClass::class, 'handlerFenction']);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question