A
A
Alexander2020-04-26 23:21:25
1C-Bitrix
Alexander, 2020-04-26 23:21:25

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');


But I have doubts that this code is correct. In the examples for handling events of highload blocks, the name of a specific highload block is specified, and the name of the module is not indicated at all.
$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');

But what if you need to create a universal handler for all highload blocks that are or will be created in the system?

Should I go through the list of highload blocks, get everything, and only then add a handler for each block? And in which module file should I make such a request?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
smilingcheater, 2020-04-27
@smilingcheater

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 question

Ask a Question

731 491 924 answers to any question