Documentation

InitComponentVariables

void
InitComponentVariables(
 string componentPage ,
 array arComponentVariables ,
 array arVariableAliases ,
 array & arVariables
);

The method supports variable aliases in complex components (both in CNC and standard modes). This method restores true variables from $_REQUEST based in their aliases. Static method.

Parameters

ParameterDescription
componentPage For CNC mode: path template code, retrieved by the method CComponentEngine::ParseComponentPath. If the component does not work in CNC mode, this parameter must be equal to False.
arComponentVariables Array of variable names which can be received via query. This parameter has the array("variable name 1", "variable name 2", ...).
arVariableAliases Array of variable aliases.

The format for CNC mode is as follows:
array(
    "path template code 1" => array(
        "variable name 11" => "variable alias 11",
        "variable name 12" => "variable alias 12",
        * * *
    ), 
    "path template code 2" => array(
        "variable name 21" => "variable alias 21",
        "variable name 22" => "variable alias 22",
        * * *
    ),
    * * *
)    
For non-CNC mode:
array(
    "variable name" => "variable alias",
    * * *
)    
arVariables Variable to which the restored data is returned.

Example

// Non-CNC mode

// This variable will accumulate true variable values
$arVariables = array();

// Collect array of path templates from path template array
// "default" $arDefaultUrlTemplates404
// and the array, passed in the input parameters $arParams["SEF_URL_TEMPLATES"]
$arUrlTemplates = 
    CComponentEngine::MakeComponentUrlTemplates($arDefaultUrlTemplates404, 
                                                $arParams["SEF_URL_TEMPLATES"]);

// Collect array of variable aliases from aliases array
// "default" $arDefaultVariableAliases404
// and the array, passed in the input parameters $arParams["VARIABLE_ALIASES"]
$arVariableAliases = 
    CComponentEngine::MakeComponentVariableAliases($arDefaultVariableAliases404, 
                                                   $arParams["VARIABLE_ALIASES"]);

// Determine template code that match with the current request page
$componentPage = CComponentEngine::ParseComponentPath(
    $arParams["SEF_FOLDER"],
    $arUrlTemplates,
    $arVariables
);

// If requested page does not match with any template, 
// specify a visibly displayed elements
if (StrLen($componentPage) <= 0)
    $componentPage = "list";

// Now template code and variables are specified, 
// passed as the paths to the requested page  
// variables recorded in $arVariables

// Restore variables passed in the request parameters 
// and write them in $arVariables
CComponentEngine::InitComponentVariables($componentPage, 
                                         $arComponentVariables,
                                         $arVariableAliases,
                                         $arVariables);

// Now based on the path template code $componentPage 
// and true variables $arVariables,
// determine which component template page must be displayed

if ($componentPage == "element")
    $componentPage = "element";
elseif ($componentPage == "section1" || $componentPage == "section2")
    $componentPage = "section";
else
    $componentPage = "list";

$this->IncludeComponentTemplate(
    array(
        "FOLDER" => $arParams["SEF_FOLDER"],
        "URL_TEMPLATES" => $arUrlTemplates,
        "VARIABLES" => $arVariables,
        "ALIASES" => $arVariableAliases
    ),
    $componentPage
);
// For non-CNC mode

// This variable will accumulate values of true variables
$arVariables = array();

// Collect an array of variable aliases from the alias array
// "default" $arDefaultVariableAliases
// and the array, passed in the input parameters $arParams["VARIABLE_ALIASES"]
$arVariableAliases = 
    CComponentEngine::MakeComponentVariableAliases($arDefaultVariableAliases, 
                                                   $arParams["VARIABLE_ALIASES"]);

// Restore variables, retrieved in the request parameters 
// and write them in $arVariables
CComponentEngine::InitComponentVariables(False, $arComponentVariables, 
                                         $arVariableAliases, $arVariables);

// Now based on the true variables $arVariables
// you can determine which component template page must be displayed

$componentPage = "";
if (IntVal($arVariables["ELEMENT_ID"]) > 0)
    $componentPage = "element";
elseif (IntVal($arVariables["IBLOCK_ID"]) > 0 || 
        IntVal($arVariables["SECTION"]) > 0)
    $componentPage = "section";
else
    $componentPage = "list";

$this->IncludeComponentTemplate(
    array(
        "VARIABLES" => $arVariables,
        "ALIASES" => $arVariableAliases
    ),
    $componentPage
);

See Also

  • Complex component structure
  • Input variable aliases
  • CComponentEngine::MakeComponentVariableAliases
  • CComponentEngine::MakeComponentUrlTemplates


  • © «Bitrix24», 2001-2024
    Up