S
S
Stanislav2018-03-13 14:26:31
1C-Bitrix
Stanislav, 2018-03-13 14:26:31

How and where to embed event listening in 1C-Bitrix for their processing and further transmission to remote services?

There is a site on 1C-Bitrix, in which it is necessary to send each new transaction when it is created to one or another CRM. I plan to use listening for so-called events for this , but it's not too clear where exactly to listen for them. How is it usually implemented, a certain mini-plugin is written, a certain engine file is corrected, a certain file of the current theme (template) of the site is corrected?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Nikolaev, 2018-03-13
@lamo4ok

In general, the official documentation on the init.php file provides comprehensive information on this issue .
About ways to organize code, this is a rather narrow topic.
If you are 100% sure that they will still be useful on other projects (and you already have similar orders), then it is preferable to make a module.
If it's a hardcore integration for an existing project only, then init.php(*).
For example, we stick to the last option:
1) Create the following files in the /local/php_interface/ directory:
- init.php (Contains connection of other files + autoloader)
- constants.php (Contains only constants relevant for this project)
- events. php (contains subscriptions to events, but the logic for their processing is not in this file)
2) In the directory /local/php_interface/classes/ by PSR we place the class for the project.
For example:
init.php:

/**
 * - /local/classes/{Path|raw}/{*|raw}.php
 * - /local/classes/{Path|ucfirst,lowercase}/{*|ucfirst,lowercase}.php
 */
spl_autoload_register(function($sClassName)
{

  $sClassFile = __DIR__.'/classes';

  if ( file_exists($sClassFile.'/'.str_replace('\\', '/', $sClassName).'.php') )
  {
    require_once($sClassFile.'/'.str_replace('\\', '/', $sClassName).'.php');
  }

  $arClass = explode('\\', strtolower($sClassName));
  foreach($arClass as $sPath )
  {
      $sClassFile .= '/'.ucfirst($sPath);
  }
  $sClassFile .= '.php';
  if (file_exists($sClassFile))
  {
    require_once($sClassFile);
  }
});

/**
 * File with event handlers
 */
require_once(__DIR__.'/events.php');

events.php:
$eventManager = \Bitrix\Main\EventManager::getInstance();

/**
 * For new core of bitrix use
 *     $eventManager->addEventHandler( #module#, #handler#, [#namespace#, #function#]);
 * 
 * For old core of bitrix use
 *     $eventManager->addEventHandlerCompatible( #module#, #handler#, [#namespace#, #function#]);
 */

$eventManager->addEventHandlerCompatible("module", "event", ['\\Project\\Module\\Event\\Handler', 'onEvent']);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question