Views: 11033
Last Modified: 19.09.2014

Modules can interact among them in two ways: explicitly (by direct query) and inwardly (through event system).

Explicit Interaction

Explicit interaction means:

  • Module connection using the function CModule::IncludeModule;
  • Direct query of a class method or a module function.

Example of an explicit interaction:

<?
// we connect the module mymodule
if (CModule::IncludeModule("mymodule"))
{
    // we execute its method
    CMyModuleClass::DoIt();
}
?>

Interaction through Events

Event - is an arbitrary action at the time of execution of which (before or after) all the handlers of such an event are collected and executed one by one.

The entity of an event permits making modules independent from one another to a maximum degree. The module “knows” nothing about the particulars of functioning of another module, but it can interact with it through the interface of events.

The events operating procedure is as follows. The module initializing an event must perform the following operations at the place within the code where this event occurs:

  • Collect all the registered handlers using the function GetModuleEvents.
  • Perform them one by one using the function ExecuteModuleEvent processing the values returned by the handlers accordingly.
In its turn, the module that “wants” to perform any actions to this event must:
  • Register its handler using the function RegisterModuleDependences at the time of installation.
  • Accordingly, this handler function must be available, and you have to make sure that the script where this function is located is connected in the file /bitrix/modules/module ID/include.php.

Example of Interaction

The perfect example of such an interaction is the interaction of system modules with the Search module. This module has no information about the data of other modules, their storage and processing particulars. It only provides an interface for data indexing. Any system model to be indexed registers a handler for the event OnReindex. upon installation. Each such handler, in its turn, returns data for the indexing to be used by the Search module for filling its base.

Sample codes of examples of interaction through events:

<?
// handler registration:
// when the event OnSomeEvent occurs in the module init_module
// the method CMyModuleClass::Handler of the module handler_module will be called

RegisterModuleDependences(
    "init_module", "OnSomeEvent",
    "handler_module", "CMyModuleClass", "Handler"
    );
?>
<?
// arbitrary function of the module init_module
// where the event is generated

function MyFunction()
{
    // the arbitrary code is located here
    // it represents the event

    // after that, registered handlers are collected
    $rsHandlers = GetModuleEvents("anothermodule", "OnSomeEvent");
    while($arHandler = $rsHandlers->Fetch())
    {
        // and executed one by one
        if(!ExecuteModuleEvent($arHandler, $param1, $param2))
        {
            // if the handler returns false,
            // then, for example, we return the phrase "I can't do it..."
            return "I can't do it...";
        }
    }
    return "I have done it!";
}
?>
<?
// handler

class CMyModuleClass
{
    function Handler($param1, $param2)
    {
        if($param1=="delete all")
            return false;
        return true;
    }
}
?>



Courses developed by Bitrix24