V
V
vrusy2021-05-30 17:53:31
1C-Bitrix
vrusy, 2021-05-30 17:53:31

Why is the event handler not registered in its module?

Here is the file /install/index.php
I want to register an event when creating an IB element
In InstallDB() I try to register it, but after installation the logic from OnAfterIBlockElementAddHandler3 does not work

<?php
IncludeModuleLangFile(__FILE__);

use Bitrix\Main\Loader;
use Bitrix\Main\Localization\Loc;
use Bitrix\Main\ModuleManager;
use Bitrix\Main\Config\Option;
use Bitrix\Main\EventManager;

\CModule::IncludeModule('iblock');
\CModule::IncludeModule('main');

class MyClass
{
    // создаем обработчик события "OnAfterIBlockElementAdd"
    function OnAfterIBlockElementAddHandler3(&$arFields)
    {

        file_put_contents($_SERVER['DOCUMENT_ROOT'].'/log_dev.txt', FILE.':'.LINE.PHP_EOL.print_r($arFields, true)."\n", FILE_APPEND);

    }
}


class ulrusy_log extends CModule
{
  var $MODULE_ID = 'ulrusy.log';

  function __construct()
  {
    $arModuleVersion = array();

    include(__DIR__.'/version.php');

    $this->MODULE_ID = 'ulrusy.log';
    $this->MODULE_VERSION = $arModuleVersion["VERSION"];
    $this->MODULE_VERSION_DATE = $arModuleVersion["VERSION_DATE"];
    $this->MODULE_NAME = GetMessage("ULRUSY_LOG_MODULE_NAME");
    $this->MODULE_DESCRIPTION = GetMessage("ULRUSY_LOG_MODULE_DESCRIPTION");
    $this->SHOW_SUPER_ADMIN_GROUP_RIGHTS = 'Y';
    $this->MODULE_GROUP_RIGHTS = 'Y';
  }

  function DoInstall()
  {
    $this->InstallDB();

    RegisterModule($this->MODULE_ID);
    Loader::includeModule($this->MODULE_ID);

    $GLOBALS['APPLICATION']->IncludeAdminFile(GetMessage("ULRUSY_LOG_INSTALL_TITLE"), $_SERVER["DOCUMENT_ROOT"]."/local/modules/ulrusy.log/install/step.php");
  }

  function DoUninstall()
  {
    global $DOCUMENT_ROOT, $APPLICATION;

    if($_REQUEST["step"]<2)
    {
      $APPLICATION->IncludeAdminFile(GetMessage("ULRUSY_LOG_UNINSTALL_TITLE"), $DOCUMENT_ROOT."/local/modules/ulrusy.log/install/unstep1.php");
    }
    elseif($_REQUEST["step"]==2)
    {
      $this->UnInstallDB(array(
        "savedata" => $_REQUEST["savedata"],
      ));

      UnRegisterModule($this->MODULE_ID);

      $APPLICATION->IncludeAdminFile(GetMessage("ULRUSY_LOG_UNINSTALL_TITLE"), $DOCUMENT_ROOT."/local/modules/ulrusy.log/install/unstep2.php");
    }
  }

  function InstallDB() 
  {
    global $APPLICATION, $DB;

    if(!$DB->Query("SELECT '*' FROM b_ulrusy_log", true))
      $DB->RunSQLBatch($_SERVER['DOCUMENT_ROOT']."/local/modules/ulrusy.log/install/db/".mb_strtolower($DB->type)."/install.sql");

    $eventManager = EventManager::getInstance();
    $eventManager->registerEventHandlerCompatible('iblock', 'OnAfterIBlockElementAdd', 'ulrusy.log', 'MyClass', 'OnAfterIBlockElementAddHandler3');

    unset($eventManager);

    return true;
  }


  function UnInstallDB($arParams = Array()) 
  {
    global $APPLICATION, $DB, $errors;

    if ($arParams['savedata'])
    {
      $res = $DB->RunSQLBatch($_SERVER['DOCUMENT_ROOT']."/local/modules/ulrusy.log/install/db/mysql/uninstall.sql");
      Option::delete($this->MODULE_ID); 
    } 

    $eventManager = EventManager::getInstance();
    $eventManager->unRegisterEventHandler('iblock', 'OnAfterIBlockElementAdd', 'ulrusy.log', 'MyClass', 'OnAfterIBlockElementAddHandler3');

    unset($eventManager);

    return true;
  }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question