Views: 19937
Last Modified: 15.09.2014
The class CUserTypeEntity is in charge of work with custom fields.
An example of adding a String-type custom property
/**
* Adding a custom property
*/
$oUserTypeEntity = new CUserTypeEntity();
$aUserFields = array(
/*
* Identifier of an entity to which the property will be bound.
* The following format is used for a section - IBLOCK_{IBLOCK_ID}_SECTION
*/
'ENTITY_ID' => 'IBLOCK_3_SECTION',
/* Field code. It must always begin with UF_ */
'FIELD_NAME' => 'UF_DEV2DAY_FIELD',
/* Indicate that the type of the new custom property is string */
'USER_TYPE_ID' => 'string',
/*
* XML_ID of the custom property.
* To be used during uploading as a field name
*/
'XML_ID' => 'XML_ID_DEV2DAY_FIELD',
/* Sorting */
'SORT' => 500,
/* Whether the field is multiple or not */
'MULTIPLE' => 'N',
/* Whether the property is mandatory or not */
'MANDATORY' => 'N',
/*
* Show in the list filter. Possible values:
* Do not show = N, exact match = I,
* mask search = E, substring search = S
*/
'SHOW_FILTER' => 'N',
/*
* Do not show in the list.
*/
'SHOW_IN_LIST' => '',
/*
* Do not permit editing by users.
*/
'EDIT_IN_LIST' => '',
/* Field values participate in the search */
'IS_SEARCHABLE' => 'N',
/*
* Additional field settings (depend on the type).
* In our case, for the string type
*/
'SETTINGS' => array(
/* Default value */
'DEFAULT_VALUE' => '',
/* Entry field size for display */
'SIZE' => '20',
/* Number of rows in the entry field */
'ROWS' => '1',
/* Minimum length of the line (0 – do not verify) */
'MIN_LENGTH' => '0',
/* Maximum length of the line (0 – do not verify) */
'MAX_LENGTH' => '0',
/* A regular expression for verification */
'REGEXP' => '',
),
/* Edit form label */
'EDIT_FORM_LABEL' => array(
'en' => 'User field',
),
/* List label */
'LIST_COLUMN_LABEL' => array(
'en' => 'User field',
),
/* List filter label */
'LIST_FILTER_LABEL' => array(
'en' => 'User field',
),
/* Error message (optional) */
'ERROR_MESSAGE' => array(
'en' => 'An error in completing the user field',
),
/* Help */
'HELP_MESSAGE' => array(
'en' => '',
),
);
$iUserFieldId = $oUserTypeEntity->Add( $aUserFields ); // int
If a correct property is added to the variable $iUserFieldId, the identifier of the new custom property will be returned.
In order to create custom fields of other types, replace the value of USER_TYPE_ID:
- enumeration - List
- double - Number
- integer - Integral number
- boolean - Yes/No
- string - String
- file - File
- video - Video
- datetime - Date/Time
- iblock_section - Binding to infoblock sections
- iblock_element - Binding to infoblock elements
- string_formatted - Template
- crm - Binding to CRM elements
- crm_status - Binding to CRM reference tables
Custom Property Update
When updating a custom property, the changes in its type (USER_TYPE_ID
), object to be bound (ENTITY_ID), and field code (FIELD_NAME) are restricted. It is connected with possible errors in linking values and entities. If one of these fields is to be changed, first of all a new custom property must be created, with all the values bound to it. That done, the old property must be deleted.
An example of a custom property update:
$oUserTypeEntity->Update( $iUserFieldId, array(
'MANDATORY' => 'Y',
) ); // boolean;
The example establishes that the field is mandatory.
Custom Field Removal
The identifier of a custom field must be submitted:
$oUserTypeEntity->Delete( $iUserFieldId ); // CDBResult
Adding and Updating Values of Custom Fields
Adding and updating is also implemented through the CUserTypeManager and the Update method.
global $USER_FIELD_MANAGER;
$aSection = CIBlockSection::GetList( array(), array(
'IBLOCK_CODE' => 'shop_news',
'CODE' => 'test_section',
) )->Fetch();
if( !$aSection ) {
throw new Exception( 'No section is found' );
}
$USER_FIELD_MANAGER->Update( 'IBLOCK_3_SECTION', $aSection['ID'], array(
'UF_DEV2DAY_FIELD' => 'updated value'
) ); // boolean
If the update is successful, the method will return true.
Adding a Custom Property to an Infoblock Section
If a custom property is to be added to an infoblock section using the Bitrix-API mechanism, the following code with variations must be used:
$arFields = Array(
"ENTITY_ID" => "IBLOCK_2_SECTION",
"FIELD_NAME" => "UF_TITLE",
"USER_TYPE_ID" => "string",
"EDIT_FORM_LABEL" => Array("en"=>"title")
);
$obUserField = new CUserTypeEntity;
$obUserField->Add($arFields);