Views: 12879
Last Modified: 09.09.2014

Composite component serves to organize a website section as a whole (forum, catalog, etc.). It connects simple components to the display data. It actually operates as a controller (manager) of simple components. Composite component determines a page to be displayed to the visitor based on HTTP and connects the template of such a page.

An example of a composite component:

<?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();?><?

$arDefaultUrlTemplates404 = array(
   "list" => "index.php",
   "element" => "#ELEMENT_ID#.php"
);

$arDefaultVariableAliases404 = array();

$arDefaultVariableAliases = array();

$arComponentVariables = array("IBLOCK_ID", "ELEMENT_ID");


$SEF_FOLDER = "";
$arUrlTemplates = array();

if ($arParams["SEF_MODE"] == "Y")
{
   $arVariables = array();

    $arUrlTemplates = 
        CComponentEngine::MakeComponentUrlTemplates($arDefaultUrlTemplates404, 
                                                    $arParams["SEF_URL_TEMPLATES"]);
    $arVariableAliases = 
        CComponentEngine::MakeComponentVariableAliases($arDefaultVariableAliases404, 
                                                       $arParams["VARIABLE_ALIASES"]);

   $componentPage = CComponentEngine::ParseComponentPath(
      $arParams["SEF_FOLDER"],
      $arUrlTemplates,
      $arVariables
   );

   if (StrLen($componentPage) <= 0)
      $componentPage = "list";

   CComponentEngine::InitComponentVariables($componentPage, 
                                            $arComponentVariables, 
                                            $arVariableAliases, 
                                            $arVariables);

   $SEF_FOLDER = $arParams["SEF_FOLDER"];
}
else
{
   $arVariables = array();

    $arVariableAliases = 
       CComponentEngine::MakeComponentVariableAliases($arDefaultVariableAliases, 
                                                      $arParams["VARIABLE_ALIASES"]);
    CComponentEngine::InitComponentVariables(false, 
                                             $arComponentVariables, 
                                             $arVariableAliases, $arVariables);

   $componentPage = "";
   if (IntVal($arVariables["ELEMENT_ID"]) > 0)
      $componentPage = "element";
   else
      $componentPage = "list";

}

$arResult = array(
   "FOLDER" => $SEF_FOLDER,
   "URL_TEMPLATES" => $arUrlTemplates,
   "VARIABLES" => $arVariables,
   "ALIASES" => $arVariableAliases
);

$this->IncludeComponentTemplate($componentPage);
?>

The following arrays are defined in the beginning of the code:

  • $arDefaultUrlTemplates404 - is used to set up paths by default in order to work in a Search Engine Friendly URL (SEF) mode. Each element of the array is a path template and is to be set up as follows:
    "path template code" => "path template"
    Structure of the type "#word#" may be used in the path template. These structures are replaced with the values of the relevant variables when the actual path is being generated. For example, for a path template:
    "element" => "#ELEMENT_ID#.php"
    The actual path will look like 195.php or 7453.php. Path templates may have parameters, for example:
    "element" => "#IBLOCK_ID#/#ELEMENT_ID#.php?SECTION_ID=#SECTION_ID#"
    All path templates used by the component must be set up.
  • $arDefaultVariableAliases404 - is used to set up default aliases for variables in a SEF mode. As a rule, this array is empty (real names of variables are used). In case a variable must have a different name in an HTTP request (address), an alias can be set up for this variable and the variable value can be recovered from the alias during component operation. In case an alias must be set for one or more variables of any path templates, an element similar to that indicated below must appear in the array:
    "path template code" => array(
        "variable 1 name" => "variable 1 alias",
        "variable 2 name" => "variable 2 alias",
        * * *
        )
    For example, if a link to the page containing detailed information about an infoblock element (e.g., a product card) must be similar to the following:
    "/<infoblock mnemonic code>/.php?SID=<element group code>"
    then the path template can be set up as follows:
    "element" => "#IBLOCK_ID#/#ELEMENT_ID#.php?SID=#SECTION_ID#"
    and an alias for the variable SECTION_ID in the array $arDefaultVariableAliases404 can be set up as:
    "element" => array(
        "SECTION_ID" => "SID"
        )
    In this case, links (addresses) will be generated using the SID parameter and the variable SECTION_ID will be set up in the components.
  • $arDefaultVariableAliases - is used to set up default aliases for variables not in a CNC mode. As a rule, this array is empty, i.e. real names of variables are used. In case a variable must have a different name in an HTTP request (address), an alias can be set up for this variable and the variable value can be recovered from the alias during component operation. In case an alias must be set for any variable, an element similar to that indicated below must appear in the array:
    "variable name" => "variable alia"
    For example, if the variable name is defined in the component SECTION_ID, but the SID variable is to be used in the links, an alias for SECTION_ID can be set up as follows:
    "SECTION_ID" => "SID"
    In this case, links (addresses) will be generated using the SID parameter, and the components will contain the SECTION_ID variable. All of these arrays or their parts can be redefined using the input parameters of the component (upon component retrieval). For example, the following array can be set up in the input parameter SEF_URL_TEMPLATES in a SEF mode:
    "SEF_URL_TEMPLATES" => array(
        "element" => "#IBLOCK_CODE#/#ELEMENT_ID#.php?GID=#SECTION_ID#"
        )
    and the following parameter can be set up in the input parameter VARIABLE_ALIASES :
    "VARIABLE_ALIASES" => array(
        "element" => array(
        "SECTION_ID" => "GID",
        ),
    )
    Then, the paths in addresses (links) will have the type similar to /phone/3425.php?GID=28and the following variables will be recovered in the component out of these path: IBLOCK_CODE = phone, ELEMENT_ID = 3425 and SECTION_ID = 28.
  • $arComponentVariables - is used to set up a list of variables wherein the component can accept in an HTTP request and admit aliases. Each element of an array is the name of a variable.

An input parameter with the preset name SEF_MODE can have two values: Y and N. If $arParams["SEF_MODE"] is equal to Y, the component works in a SEF mode; otherwise, it does not.

An input parameter with the preset name SEF_FOLDER makes sense if the component works in a SEF mode. In this case, it contains the path along which the component works. The path may be virtual (i.e. it might not exist physically). For example, the component from the example can be located in the file /fld/n.php, at the same time, it works in a SEF mode, and the input parameter SEF_FOLDER is equal to /company/news/. In this case, the component will respond to queries at the addresses /company/news/index.php, /company/news/25.php etc.

The following methods are used in order to determine the page to be shown by the composite component and also to recover component variables from the path and aliases:

  • CComponentEngine::MakeComponentUrlTemplates
    array
    CComponentEngine::MakeComponentUrlTemplates($arDefaultUrlTemplates404, 
                                                $arParams["SEF_URL_TEMPLATES"]);
    The method combines into a single array the default array of path templates and path templates submitted in input parameters of a component. If a template of any path is defined in $arParams["SEF_URL_TEMPLATES"], it will redefine the default template for such path.
  • CComponentEngine::MakeComponentVariableAliases
    array
    CComponentEngine::MakeComponentVariableAliases($arDefaultVariableAliases404, 
                                                   $arParams["VARIABLE_ALIASES"]);
    The method combines into a single array the default array of variable aliases and the variable aliases submitted in the input parameters of a component. If an alias of a variable is also defined both in the default array and in input parameters, the alias from the input parameters is returned.
  • CComponentEngine::ParseComponentPath
    string
    CComponentEngine::ParseComponentPath($arParams["SEF_FOLDER"],
                                         $arUrlTemplates, $arVariables);
    The method based on the parameter $arParams["SEF_FOLDER"] and path template array (returned by the method MakeComponentUrlTemplates) determines the path template to which the requested address corresponds. If the template was found, its code is returned. Otherwise, an empty line is returned. In addition, an array of component variables recovered from the path template without parameters is returned in the $arVariables variable. For example, if a path template array (resulted from the array $arDefaultUrlTemplates404 after the redefinition of all or a part of templates through input parameters of the component) looks like:
    $arUrlTemplates = array(
        "list" => "index.php",
        "element" => "#IBLOCK_ID#/#ELEMENT_ID#.php?SID=#SECTION_ID#"
    );
    If the input parameter SEF_FOLDER is equal to /company/news/, and the requested address is equal to /company/news/15/7653.php?SID=28, the method ParseComponentPath will return the line "element" (code of the relevant template), and the array $arVariables will look as follows:
    $arVariables = array(
        "IBLOCK_ID" => 15,
        "ELEMENT_ID" => 7653
    )
  • CComponentEngine::InitComponentVariables
    CComponentEngine::InitComponentVariables($componentPage, 
                                             $arComponentVariables, 
                                             $arVariableAliases, 
                                             $arVariables);
    where:
    • $componentPage - is the code of the template that returned the ParseComponentPath method and to which the requested address corresponds;
    • $arComponentVariables - is an array of variables that a component may admit in a HTTP request and that may have aliases;
    • $arVariableAliases - is an alias array (returned by the method MakeComponentVariableAliases).

    The method recovers variables from $_REQUEST taking into account their possible aliases and returns them in the variable $arVariables. For example, if for the code above the array $arVariableAliases contains an entry

    "element" => array(
        "SECTION_ID" => "SID",
    )
    the method InitComponentVariables will return an array in the $arVariables parameter
    $arVariables = array(
        "IBLOCK_ID" => 15,
        "ELEMENT_ID" => 7653,
        "SECTION_ID" => 28
    )
    Here, the method InitComponentVariables initialized the third element of the array. The first two were initialized by the method ParseComponentPath in the example above. In case the component works in a non-SEF mode, the first parameter submits the value False to the method InitComponentVariables.

If the component works in a SEF mode based on the path template code and variables from HTTP request (and in case of non-SEF addresses only based on variables from HTTP request) the component determines which page from the template is to be connected and forwards its name to the method invocation:

$this->IncludeComponentTemplate($componentPage);

Simple components are connected and their input parameters are set up on pages of a composite component template based on the input parameters of the composite component and certain calculated values and constants. For example, the page "element" of the component template from the example (file of the type /templates/.default/list.php with respect of the component folder) may look as follows:

<?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();?>
<?$APPLICATION->IncludeComponent(
   "bitrix:news.detail",
   "",
   Array(
      "IBLOCK_ID" => $arParams["IBLOCK_ID"],
      "ELEMENT_ID" => $arResult["VARIABLES"]["ELEMENT_ID"],
      "SECTION_ID" => $arResult["VARIABLES"]["SECTION_ID"],
      "CACHE_TIME" => $arParams["CACHE_TIME"],
   ),
   $component
);?>

The last parameter $component in the component connection is an object representing the current component. It is forwarded to the component connection call. Thus, the component to be connected will “know” that it is connected from the composite component. Accordingly, it will be able to use the resources of the composite component, invoke its methods, etc.



Courses developed by Bitrix24