Views: 12492
Last Modified: 17.09.2014

Filtering

Custom fields of sections can participate in filtering.

$sec_Filter= array(  
   "IBLOCK_ID" => $IBLOCK_ID, 
   "DEPTH_LEVEL" => "2", 
   "!UF_ARC_PAGES" => ""
);

Note: Filtering by custom fields work only provided that IBLOCK_ID filter is available.

All sections with the property UF_ARC_PAGES set will be selected.



Filtering by value of a custom property:

$arSFilter ['=UF_USERS_PROPERTY'] =$users_property_value;


Sorting

Sorting by custom fields of sections:

    $arSort = array(
       "UF_RATING"=>"asc",
       "sort"=>"asc"
      
   );


Obtaining Values

The method GetList of the appropriate class is used to obtain the value of a custom field.

The value of a custom field for the user with ID=2 can be obtained as follows:

$rsUser = CUser::GetByID($user);
$arUser = $rsUser->Fetch();
$required value = $arUser['custom field code']; 


In order to obtain a value of a custom field of a specific user, where the field type is a line, the method GetList of the class CUser should be used. In this case, an array with the key SELECT must be submitted as a fourth argument. The values of this key are the list of codes of the custom properties we are looking for.

global $USER;
$arFilter = array("ID" => $USER->GetID());
$arParams["SELECT"] = array("UF_USER_CARD_CODE");
$arRes = CUser::GetList($by,$desc,$arFilter,$arParams);
    if ($res = $arRes->Fetch()) {
        echo $res["UF_USER_CARD_CODE"];
    }


If the type of a custom field is a list, then the method GetList of the class CUserFieldEnum should be used to obtain the value (or values, if a multiple choice is possible).

global $USER;
$arFilter = array("ID" => $USER->GetID());
$arParams["SELECT"] = array("UF_LIST_TASK");
$arRes = CUser::GetList($by,$desc,$arFilter,$arParams);
    if ($res = $arRes->Fetch()) {
        foreach ($res["UF_LIST_TASK "] as $id) {
                $rsRes= CUserFieldEnum::GetList(array(), array(
                    "ID" => $id,
                ));
                if($arResult = $rsRes->GetNext())
                    echo $arGender["VALUE"];
            }   
}


If a list of all the values of the custom field of a list-type USER object, the following code should be used:

global $USER_FIELD_MANAGER;
$arFields = $USER_FIELD_MANAGER->GetUserFields("USER");
$obEnum = new CUserFieldEnum;
$rsEnum = $obEnum->GetList(array(), array("USER_FIELD_ID" => $arFields["UF_LIST_TASK "]["ID"]));
while($arEnum = $rsEnum->GetNext()){
   echo $arEnum["VALUE"];
}


To select a value of a custom field from a section of the information block, the method CIBlockSection:GetList can be used:

	
$aSection   = CIBlockSection::GetList( array(), array(
    'IBLOCK_ID'         => 3,
    'CODE'          => 'test_section',
), false, array( 'UF_DEV2DAY_FIELD' ) )->Fetch();

Note: Infoblock identifier (IBLOCK_ID) must be submitted without fail; otherwise, no custom properties will be selected.



Obtaining a value of a file-type custom field of a specific section of the infoblock:

$rsResult = CIBlockSection::GetList(array("SORT" => "ASC"), array("IBLOCK_ID" => "1"), false, $arSelect = array("UF_*"));
while ($arResult = $rsResult -> GetNext())
{
print "
" . print_r($arResult, true) . "
"; }


Since custom fields can be used with sections of an information block as well as any other entities, the class CUserTypeManager shall be used to select values by entity identifier. An instance of this class is already located in the global variable $USER_FIELD_MANAGER.

	
global $USER_FIELD_MANAGER;
 
$aSection   = CIBlockSection::GetList( array(), array(
    'IBLOCK_CODE'   => 'shop_news',
    'CODE'          => 'test_section',
) )->Fetch();
 
if( !$aSection ) {
    throw new Exception( 'The section is not found' );
}
 
$aUserField = $USER_FIELD_MANAGER->GetUserFields(
'IBLOCK_3_SECTION',
$aSection['ID']
); // array

As a result, we will obtain an array containing all the information about the field and its value for a specific object.


Note: In order to obtain all the values of custom fields, Array("UF_*") shall be indicated in the parameter arSelect.




Courses developed by Bitrix24