Working with Highload Bitrix blocks via API D7
Rus
Eng
Работа с Highload блоками Битрикс через API D7

All English-language materials have been translated fully automatically using the Google service

Module connection

  // Old way
CModule :: IncludeModule ("highloadblock");

// D7
use Bitrix \ Main \ Loader;

Loader :: includeModule ("highloadblock");  

Adding elements to highload block

  use Bitrix \ Main \ Loader,
Bitrix \ Highloadblock as HL,
Bitrix \ Main \ Entity;

Loader :: includeModule ("highloadblock");

$ hlblock = HL \ HighloadBlockTable :: getById ($ ID) -> fetch (); // where ID is the id of the highloadblock block to which we will add elements

$ entity = HL \ HighloadBlockTable :: compileEntity ($ hlblock);
$ entity_data_class = $ entity-> getDataClass ();

// Array of added parameters
$ data = array (
  "UF_FIELD_1" => 'Value 1',
  "UF_FIELD_2" => 'Value 2',
  "UF_FIELD_3" => 'Value 3'
);

$ result = $ entity_data_class :: add ($ data);

if ($ result-> isSuccess ()) {
echo 'added successfully';
} else {
echo 'Error:'. implode (',', $ otvet-> getErrors ()). "";
}
 

Getting elements from highload block

  use Bitrix \ Main \ Loader,
Bitrix \ Highloadblock as HL,
Bitrix \ Main \ Entity

Loader :: includeModule ("highloadblock");

$ hlblock = HL \ HighloadBlockTable :: getById ($ ID) -> fetch (); // where ID is the id of the highloadblock block from which we will receive data

$ entity = HL \ HighloadBlockTable :: compileEntity ($ hlblock);
$ entity_data_class = $ entity-> getDataClass ();

$ data = $ entity_data_class :: getList (array (
   "select" => array ("*"),
   "order" => array ("ID" => "DESC"),
   "filter" => array ("UF_FIELD_1" => "Value 1", "UF_FIELD_2" => 'Value 2', "UF_FIELD_3" => 'Value 3') // Filtering the selection
));

while ($ arData = $ data-> Fetch ()) {
        // Get the value of the list type
$ rsType = CUserFieldEnum :: GetList (array (), array (
'USER_FIELD_NAME' => 'UF_TYPE',
// 'USER_FIELD_ID' => 'ID'
));

foreach ($ rsType-> arResult as $ arType) {
print_r (arType);
}
   // Process the result
}  

Updating elements in the highload block

  use Bitrix \ Main \ Loader,
Bitrix \ Highloadblock as HL,
Bitrix \ Main \ Entity;

Loader :: includeModule ("highloadblock");

$ hlblock = HL \ HighloadBlockTable :: getById ($ ID) -> fetch (); // where ID is the id of the highloadblock block in which we will update the data

$ entity = HL \ HighloadBlockTable :: compileEntity ($ hlblock);
$ entity_data_class = $ entity-> getDataClass ();

// Array of fields to update
$ data = array (
  "UF_FIELD_1" => 'Value 1',
  "UF_FIELD_2" => 'Value 2',
  "UF_FIELD_3" => 'Value 3'
);

$ result = $ entity_data_class :: update ($ ELEMENT_ID, $ data); // where $ ELEMENT_ID is the id of the record being updated  

Removing elements from highload block

  use Bitrix \ Main \ Loader,
Bitrix \ Highloadblock as HL,
Bitrix \ Main \ Entity;

Loader :: includeModule ("highloadblock");

$ hlblock = HL \ HighloadBlockTable :: getById ($ ID) -> fetch (); // where ID is the id of the highloadblock block from which we will delete data

$ entity = HL \ HighloadBlockTable :: compileEntity ($ hlblock);
$ entity_data_class = $ entity-> getDataClass ();

$ entity_data_class :: Delete ($ ELEMENT_ID); // where $ ELEMENT_ID is the id of the record to be deleted  

Getting highload fields of a block

  use Bitrix \ Highloadblock \ HighloadBlockTable as HL;

Loader :: includeModule ("highloadblock");

$ hlblock = HL :: getById ($ ID) -> fetch (); // where ID is the id of the highloadblock block
$ entity = HL :: compileEntity ($ hlblock);

$ fields = $ entity-> getFields ();  

Getting the number of highload block elements

  CModule :: IncludeModule ('highloadblock');

$ entity_data_class = GetEntityDataClass ($ ID); // where ID is the id of the highloadblock block

$ count = $ entity_data_class :: getCount ();  

Highload block filtering by field Yes \ No

  'filter' => array (
'UF_BOOLEAN_FIELD' => '1'
)  

Highload block filtering with logic

  $ arFilter = Array (
   Array (
      "LOGIC" => "AND", // Logical AND
      Array (
         "UF_TITLE" => 'a%' // Get records starting with a
      ),
      Array (
         'UF_BOOLEAN_FIELD' => '1' // Yes \ No field is set
      )
   )
);  

Highload block complex filtering

We select user 20, as well as users with the value of the custom field UF_SHMS = 770 , which are in group 6

  $ arFilter = Array (
   Array (
      "LOGIC" => "OR",
      Array (
         "ID" => 20
      ),
      Array (
         "UF_SHMS" => 770,
         "Bitrix \ Main \ UserGroupTable: USER.GROUP_ID" => 6
      )
   )
);  

Due to the fact that a user can belong to several groups. We get several identical records. This is solved by adding the parameter "data_doubling" => false to getList

  $ res = Bitrix \ Main \ UserTable :: getList (Array (
"select" => Array ("ID", "NAME"),
"filter" => $ arFilter,
"data_doubling" => false // Exclude duplicate records from the selection
));  

Types of filtration from office. documentation

  "!" - for strings, an expression that does not match the mask, or not equal (for other types of fields).
"?" - using logic, works only for string properties.
"<" - less;
"<=" - less or equal;
">" - more;
"> =" - more or equal.
"=" - equal;
"! =" - not equal.
"%" is a substring;
"!%" is not a substring.
"> <" - between;
"!> <" - not between.
 

Private filtering rules from office. documentation

  $ arFilter = array ("PROPERTY_CML2_SCAN_CODE" => false) - used to select all elements with an empty property;
$ arFilter = array ("PROPERTY_CML2_SCAN_CODE" => "") - used to select all items;
$ arFilter = array ("PROPERTY_CML2_SCAN_CODE" => "qwe") - when filtering elements, the exact match of the property with the specified string is checked;
$ arFilter = array ("? PROPERTY_CML2_SCAN_CODE" => "qwe") - when filtering elements, the presence of the specified substring in the property is checked.
$ arFilter = array ("! PROPERTY_CML2_SCAN_CODE" => false) - used to select only elements with a filled property;
$ arFilter = array ("! PROPERTY_CML2_SCAN_CODE" => "qwe") - when filtering elements, the absence of an exact match with the given string is checked;
$ arFilter = array ("!? PROPERTY_CML2_SCAN_CODE" => "qwe") - when filtering elements, the absence of the specified substring in the property is checked.  

Highload block events

  OnBeforeAdd - The event is called before adding the highload (HL) element of the block and validating the fields. You can modify the data, add your own validation, return an error if the validation fails.
OnAdd - The event is called before adding to the database, after the fields are validated. Data modification is not available.
OnAfterAdd - The event is called after adding an HL block element. You can add additional logic that will run after adding a new element of this HL block.
OnBeforeUpdate - The event is called before the highload (HL) element of the block is updated and the fields are validated. You can modify the data, add your own validation, return an error if the validation fails.
OnUpdate - The event is called before updating to the database, after the fields are validated. Data modification is not available.
OnAfterUpdate - The event is called after updating the HL element of the block. You can add additional logic that will run after the element of this HL block is updated.
OnBeforeDelete - The event is called before the block's highload (HL) element is deleted. You can undo the deletion.
OnDelete - The event is called before deleting from the database. Cancellation is not available.
OnAfterDelete - The event is called after deleting an HL block element. You can add additional logic that will run after you remove an element of this HL block.  

Catching the block highload event

  $ eventManager = \ Bitrix \ Main \ EventManager :: getInstance ();
$ eventManager-> addEventHandler ('', 'WebinarOptionsOnAfterAdd', 'OnAfterAdd'); // where "WebinarOptions" is the name of the highload block

function OnAfterAdd (\ Bitrix \ Main \ Entity \ Event $ event) {}
 

Block Highload Event - OnBeforeAdd

The event is called before the block highload (HL) element is added and the fields are validated.

You can modify data, add your own validation, return an error if the validation fails.

  $ eventManager = \ Bitrix \ Main \ EventManager :: getInstance ();
$ eventManager-> addEventHandler ('', 'WebinarOptionsBeforeAdd', 'OnBeforeAdd');

function OnBeforeAdd (\ Bitrix \ Main \ Entity \ Event $ event) {
    $ entity = $ event-> getEntity ();
    $ entityDataClass = $ entity-> GetDataClass ();

    $ eventType = $ event-> getEventType (); // event type. will return WebinarOptionsBeforeAdd

    $ arFields = $ event-> getParameter ("fields"); // get an array of fields for the highload block

    $ arParameters = $ event-> getParameters (); // get all data available in this event
 
    $ result = new \ Bitrix \ Main \ Entity \ EventResult ();
 
// data modification
    if (empty ($ arFields ['UF_FULL_DESCRIPTION'])) {
        $ arFields ['UF_FULL_DESCRIPTION'] = $ arFields ['UF_DESCRIPTION'];
        $ result-> modifyFields ($ arFields);
    }
 
// to test data modification comment out this block
    if (empty ($ arFields ['UF_DESCRIPTION'])) {
        $ arErrors = Array ();
        $ arErrors [] = new \ Bitrix \ Main \ Entity \ FieldError ($ entity-> getField ("UF_DESCRIPTION"), "Error in the UF_DESCRIPTION field. The field must not be empty!");
        $ result-> setErrors ($ arErrors);
    }
 
    return $ result;
}  

Block Highload Event - OnAdd

The event is called before adding to the database, after the fields are validated.

Data modification is not available.

  $ eventManager = \ Bitrix \ Main \ EventManager :: getInstance ();
$ eventManager-> addEventHandler ('', 'ColorsOnAdd', 'OnAdd');
 
function OnAdd (\ Bitrix \ Main \ Entity \ Event $ event) {
    $ entity = $ event-> getEntity ();
    $ entityDataClass = $ entity-> GetDataClass ();
// event type. will return ColorsOnAdd
    $ eventType = $ event-> getEventType ();
// get an array of fields for the highload block
    $ arFields = $ event-> getParameter ("fields");
}  

Block Highload Event - OnAfterAdd

The event is called after adding an HL block element. You can add additional logic that will run after adding a new element of this HL block.

  $ eventManager = \ Bitrix \ Main \ EventManager :: getInstance ();
$ eventManager-> addEventHandler ('', 'ColorsOnAfterAdd', 'OnAfterAdd');
 
function OnAfterAdd (\ Bitrix \ Main \ Entity \ Event $ event) {
// id of the added element
    $ id = $ event-> getParameter ("id");
 
    $ entity = $ event-> getEntity ();
    $ entityDataClass = $ entity-> GetDataClass ();
// event type. will return ColorsOnAfterAdd
    $ eventType = $ event-> getEventType ();
// get an array of fields for the highload block
    $ arFields = $ event-> getParameter ("fields");
}  

Block Highload Event - OnBeforeUpdate

The event is called before the highload (HL) element of the block is updated and the fields are validated.

You can modify data, add your own validation, return an error if the validation fails.

  $ eventManager = \ Bitrix \ Main \ EventManager :: getInstance ();
$ eventManager-> addEventHandler ('', 'ColorsOnBeforeUpdate', 'OnBeforeUpdate');
 
function OnBeforeUpdate (\ Bitrix \ Main \ Entity \ Event $ event) {
    $ id = $ event-> getParameter ("id");
// id of the updated element
    $ id = $ id ["ID"];
 
    $ entity = $ event-> getEntity ();
    $ entityDataClass = $ entity-> GetDataClass ();
// event type. will return ColorsOnBeforeUpdate
    $ eventType = $ event-> getEventType ();
// get an array of fields for the highload block
    $ arFields = $ event-> getParameter ("fields");
 
    $ result = new \ Bitrix \ Main \ Entity \ EventResult ();
 
// data modification
    if (empty ($ arFields ['UF_FULL_DESCRIPTION'])) {
        $ arFields ['UF_FULL_DESCRIPTION'] = $ arFields ['UF_DESCRIPTION'];
        $ result-> modifyFields ($ arFields);
    }
 
// to test data modification comment out this block
    if (empty ($ arFields ['UF_DESCRIPTION'])) {
        $ arErrors = Array ();
        $ arErrors [] = new \ Bitrix \ Main \ Entity \ FieldError ($ entity-> getField ("UF_DESCRIPTION"), "Error in the UF_DESCRIPTION field. The field must not be empty!");
        $ result-> setErrors ($ arErrors);
    }
 
    return $ result;
}  

Block Highload Event - OnUpdate

The event is called before updating to the database, after validating the fields.

Data modification is not available.

  $ eventManager = \ Bitrix \ Main \ EventManager :: getInstance ();
$ eventManager-> addEventHandler ('', 'ColorsOnUpdate', 'OnUpdate');
 
function OnUpdate (\ Bitrix \ Main \ Entity \ Event $ event) {
    $ id = $ event-> getParameter ("id");
// id of the updated element
    $ id = $ id ["ID"];
 
    $ entity = $ event-> getEntity ();
    $ entityDataClass = $ entity-> GetDataClass ();
// event type. will return ColorsOnUpdate
    $ eventType = $ event-> getEventType ();
// get an array of fields for the highload block
    $ arFields = $ event-> getParameter ("fields");
}  

Block Highload Event - OnAfterUpdate

The event is called after updating the HL element of the block. You can add additional logic that will run after the element of this HL block is updated.

  $ eventManager = \ Bitrix \ Main \ EventManager :: getInstance ();
$ eventManager-> addEventHandler ('', 'ColorsOnAfterUpdate', 'OnAfterUpdate');
 
function OnAfterUpdate (\ Bitrix \ Main \ Entity \ Event $ event) {
    $ id = $ event-> getParameter ("id");
// id of the updated element
    $ id = $ id ["ID"];
 
    $ entity = $ event-> getEntity ();
    $ entityDataClass = $ entity-> GetDataClass ();
// event type. will return ColorsOnAfterUpdate
    $ eventType = $ event-> getEventType ();
// get an array of fields for the highload block
    $ arFields = $ event-> getParameter ("fields");
}  

Block Highload Event - OnBeforeDelete

The event is called before the highload (HL) element of the block is removed.

You can undo the deletion.

  $ eventManager = \ Bitrix \ Main \ EventManager :: getInstance ();
$ eventManager-> addEventHandler ('', 'ColorsOnBeforeDelete', 'OnBeforeDelete');
 
function OnBeforeDelete (\ Bitrix \ Main \ Entity \ Event $ event) {
// fields in this event are not available, only id
    $ id = $ event-> getParameter ("id");
// id of the element to remove
    $ id = $ id ["ID"];
 
    $ entity = $ event-> getEntity ();
    $ entityDataClass = $ entity-> GetDataClass ();
// event type. will return ColorsOnBeforeDelete
    $ eventType = $ event-> getEventType ();
 
    $ result = new \ Bitrix \ Main \ Entity \ EventResult ();
    if ($ id <= 20) {
        $ arErrors = Array ();
        $ arErrors [] = new \ Bitrix \ Main \ Entity \ EntityError ("Error! You cannot delete the first 20 elements!");
        $ result-> setErrors ($ arErrors);
    }
 
    return $ result;
}  

Block Highload Event - OnDelete

The event is called before deleting from the database. Cancellation is not available.

  $ eventManager = \ Bitrix \ Main \ EventManager :: getInstance ();
$ eventManager-> addEventHandler ('', 'ColorsOnDelete', 'OnDelete');
 
function OnDelete (\ Bitrix \ Main \ Entity \ Event $ event) {
// fields in this event are not available, only id
    $ id = $ event-> getParameter ("id");
// id of the element to remove
    $ id = $ id ["ID"];
    $ entity = $ event-> getEntity ();
    $ entityDataClass = $ entity-> GetDataClass ();
// event type. will return ColorsOnDelete
    $ eventType = $ event-> getEventType ();
}  

Block Highload Event - OnAfterDelete

The event is called after removing an HL block element. You can add additional logic that will run after an element of this HL block is deleted.

  $ eventManager = \ Bitrix \ Main \ EventManager :: getInstance ();
$ eventManager-> addEventHandler ('', 'ColorsOnAfterDelete', 'OnAfterDelete');
 
function OnAfterDelete (\ Bitrix \ Main \ Entity \ Event $ event) {
    $ id = $ event-> getParameter ("id");

// id of the element to remove
    $ id = $ id ["ID"];
 
    $ entity = $ event-> getEntity ();
    $ entityDataClass = $ entity-> GetDataClass ();
// event type. will return ColorsOnAfterAdd
    $ eventType = $ event-> getEventType ();
    
if ($ id> 30 && $ id <1000) {
// your logic ....
    }
}  

Materials used:

Comments

There are no comments yet, you can be the first to leave it

Leave a comment

The site uses a comment pre-moderation system, so your message will be published only after approval by the moderator

You are replying to a user's comment

Send

FEEDBACK

Email me

Are you developing a new service, making improvements to the existing one and want to be better than your competitors? You have come to the right place. I offer you a comprehensive studio-level website development. From me you can order design, layout, programming, development of non-traditional functionality, implementation of communication between CMS, CRM and Data Analitics, as well as everything else related to sites, except for promotion.

Contact, I will always advise on all questions and help you find the most effective solution for your business. I am engaged in the creation of sites in Novosibirsk and in other regions of Russia, I also work with the CIS countries. You will be satisfied with our cooperation

An error occurred while sending, please try again after a while
Message sent successfully

Phones

+7(993) 007-18-96

Email

info@tichiy.ru

Address

Россия, г. Москва

By submitting the form, you automatically confirm that you have read and accept the Privacy Policy site

Contact with me
Send message
By submitting the form, you automatically confirm that you have read and accept Privacy policy of site
Sending successful!
Thank you for contacting :) I will contact you as soon as possible
Sending failed
An error occurred while sending the request. Please wait and try again after a while or call my phone number