S
S
Sergey Volkov2018-08-08 13:58:24
1C-Bitrix
Sergey Volkov, 2018-08-08 13:58:24

Problem with registerEventHandler?

I am writing my module. Register an event handler:

$eventManager = \Bitrix\Main\EventManager::getInstance();
        $eventManager->registerEventHandler(
            "iblock",
            "OnAfterIBlockElementUpdate",
             $this->MODULE_ID,
            '\foo_spammer\events\SpammerIBlockElementUpdateHandler',
            "run"
        );

Removed all the code in the handler.
class SpammerIBlockElementUpdateHandler
{
    public function run(Bitrix\Main\Entity\Event $event)
    {
          return true;
    }
}

When you try to edit an infoblock element, it hangs endlessly.
If the parameter (Bitrix\Main\Entity\Event $event) is removed, everything returns to normal. But I need this option! I tried registerEventHandlerCompatible and, accordingly, it was (&$arFields) in the parameters - it still hangs.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Volkov, 2018-08-08
@srvr4vr

The called function must be static. Did it like this:

class SpammerIBlockElementUpdateHandler
{
    public static function runStatic(&$fields)
    {
        $self = new self();
        return $self->run($fields);
    }

    public  function run(&$arFields)
    { 
         //some code
    }
}

/*****************************/
$eventManager = \Bitrix\Main\EventManager::getInstance();
$eventManager->registerEventHandlerCompatible(
    "iblock",
    "OnAfterIBlockElementUpdate",
     $this->MODULE_ID,
     '\foo_spammer\events\SpammerIBlockElementUpdateHandler',
     "runStatic"
);

A
Andrey Nikolaev, 2018-08-08
@gromdron

You are using the old property (there are no new infoblocks yet).
Accordingly, you need to:
1) Execute registerEventHandlerCompatible
2) Use the code from the documentation and catch not an Event, but a reference to an array
public function run( &$arFields )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question