BeforeIndex
handler function( array arFields );
"BeforeIndex" event is called prior to indexing of an element by the CSearch::Index method.
Parameters
Parameter | Description |
---|---|
arFields | Array of the following content:
|
This handler can modify fields of the arFields parameter and must return it as the result.
Important
- If SITE_ID field is modified in any way within the handler, it's important to remember that indexes in the array must start from zero and with a sequence order. Otherwise the array will be deemed as associative and the elements will associate with sites incorrectly (digits will replace SITE_ID and elements will disappear from the search).
- To avoid adding an index entry (for example, if a subsection of information block must not be indexed), the following must be executed in the handler function:
unset($arFields["BODY"]); unset($arFields["TITLE"]);
- If the following types of checks are executed:
$bTitle = array_key_exists("TITLE", $arFields); $bBody = array_key_exists("BODY", $arFields); if($bTitle && $bBody && strlen($arFields["BODY"])<=0 && strlen($arFields["TITLE"])<=0)
then to exclude an element from the index, the following must be executed:
$arFields["BODY"]=''; $arFields["TITLE"]='';
See Also
Example of handler function:
<?
// file /bitrix/php_interface/init.php
// register the handler
AddEventHandler("search", "BeforeIndex", Array("MyClass", "BeforeIndexHandler"));
class MyClass { // create event handler "BeforeIndex" function BeforeIndexHandler($arFields) { if($arFields["MODULE_ID"] == "iblock" && $arFields["PARAM2"] == 33) { if(array_key_exists("BODY", $arFields)) { $arFields["BODY"] .= " the hottest news"; } } return $arFields; } } ?>
Example of handler function use:
Example of using handler function, for the search.title component to execute search not only by titles, but by some property:
// register the handler AddEventHandler("search", "BeforeIndex", "BeforeIndexHandler"); // create event handler "BeforeIndex" public static function BeforeIndexHandler($arFields) { if(!CModule::IncludeModule("iblock")) // connect the module return $arFields; if($arFields["MODULE_ID"] == "iblock") { $db_props = CIBlockElement::GetProperty( // Request properties of indexed element $arFields["PARAM2"], // BLOCK_ID of indexed property $arFields["ITEM_ID"], // ID of indexed property array("sort" => "asc"), // Sorting (can be skipped) Array("CODE"=>"CML2_ARTICLE")); // property CODE (in this case, article) if($ar_props = $db_props->Fetch()) $arFields["TITLE"] .= " ".$ar_props["VALUE"]; // add a property to the end of a indexed element title } return $arFields; // return the implemented modifications }
© «Bitrix24», 2001-2024