Loading...
Loading...
Covers Bitrix event system — new model (Bitrix\Main\Event, EventResult, EventManager::addEventHandler, make:event, make:eventhandler) and old model (OnBefore*/OnAfter* hooks in CIBlock, CUser, CSale and other classic APIs). Applied when integrating modules, entity lifecycle hooks, publishing custom events and subscribing to events of other modules. Key terms — Event, EventManager, EventResult, OnBefore, OnAfter, handler, subscriber, addEventHandler.
npx skill4agent add bxmaximum/bitrix-framework-skills bitrix-eventsEventEventResultOnBeforeUserAddOnPageStartphp bitrix/bitrix.php make:event PostCreated -m vendor.blogmake:*/local/modules/vendor.blog/lib/Public/Event/Post/PostCreatedEvent.php<?php declare(strict_types=1);
namespace Vendor\Blog\Public\Event\Post;
use Bitrix\Main\Event;
final class PostCreatedEvent extends Event
{
public function __construct(
public readonly int $postId,
public readonly int $authorId,
public readonly string $title,
) {
parent::__construct('vendor.blog', self::class, [
'postId' => $this->postId,
'authorId' => $this->authorId,
'title' => $this->title,
]);
}
}use Vendor\Blog\Public\Event\Post\PostCreatedEvent;
$event = new PostCreatedEvent($post->getId(), $post->getAuthorId(), $post->getTitle());
$event->send();
foreach ($event->getResults() as $result)
{
if ($result->getType() === \Bitrix\Main\EventResult::ERROR)
{
$this->logger->warning('Subscriber failed', ['errors' => $result->getParameters()]);
}
}php bitrix/bitrix.php make:eventhandler NotifyAuthor \
--event-module=vendor.blog --handler-module=vendor.notifycall_user_func_arrayServiceLocator<?php declare(strict_types=1);
namespace Vendor\Notify\Internals\Integration\Blog\EventHandler;
use Bitrix\Main\DI\ServiceLocator;
use Bitrix\Main\Event;
use Bitrix\Main\EventResult;
use Vendor\Blog\Public\Event\Post\PostCreatedEvent;
use Vendor\Notify\Application\Service\Notifier;
final class NotifyAuthorHandler
{
public static function handle(Event $event): EventResult
{
if (!$event instanceof PostCreatedEvent)
{
return new EventResult(EventResult::UNDEFINED);
}
/** @var Notifier $notifier */
$notifier = ServiceLocator::getInstance()->get(Notifier::class);
$result = $notifier->notifyAuthor($event->authorId, $event->title);
return new EventResult(
$result->isSuccess() ? EventResult::SUCCESS : EventResult::ERROR,
$result->getErrorMessages(),
);
}
}install/index.php\Bitrix\Main\EventManager::getInstance()->registerEventHandler(
fromModule: 'vendor.blog',
eventType: \Vendor\Blog\Public\Event\Post\PostCreatedEvent::class,
toModuleId: 'vendor.notify',
toClass: \Vendor\Notify\Internals\Integration\Blog\EventHandler\NotifyAuthorHandler::class,
toMethod: 'handle',
);DoUninstall()unRegisterEventHandlerOnBeforeUserAddOnAfterUserAddOnEpilogOnPageStarttruefalse$APPLICATION->ThrowException(...)'FIELDS' => [...]OnBefore*EventManager::getInstance()->registerEventHandlerCompatible(
'main',
'OnAfterUserAdd',
'vendor.blog',
\Vendor\Blog\Internals\Integration\Main\EventHandler\OnAfterUserAddHandler::class,
'handle',
);registerEventHandlerEvent $event$event->getParameter('fields')EventResult$sort100addEventHandler($fromModuleId, $eventType, $callback, $includeFile = false, $sort = 100)$sortregisterEventHandler($fromModuleId, $eventType, $toModuleId, $toClass = '', $toMethod = '', $sort = 100, ...)$sortEvent::send()ERRORfalseEventManager::getInstance()->addEventHandler(
'main',
'OnAfterUserAdd',
fn (array $fields) => /* ... */,
);/lib/Public/Event/<Aggregate>//lib/Internals/Integration/<OtherModule>/EventHandler/DoInstallDoUninstallBitrix\Main\EventEventResultServiceLocator::get()try/catchMessenger$message->send()