Highload information blocks and work with them

Good day.

Today we’ll talk about Highload info blocks. More precisely how to work with them. If you look on the Internet, then everywhere there is how to get information from them, but how to write-down, delete-on the contrary - I did not find this (probably I just searched poorly), but I really needed to.
So I turned to LifeAPI and unearthed something. Let's start in order.
As you probably noticed, these info blocks contain custom fields. Those. each field starts with UF. But I will not chew, because whoever encodes on bitrix knows that.


before any action you need to connect the necessary module:
CModule::IncludeModule("highloadblock"); 


Then comes the fun part.

Making connection settings:

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


These are kind of symbolic links (those who work with multisite will understand). For the rest - connect not in functions, not in conditions, etc. (You can also connect result_modifier.php in components).

It needs to be connected before all conditions, it does not work in connecting functions, etc.

Next, you need to write the following code (select the necessary fields and connect the functions of the classes):

$hlbl = 1; - "ID  Highload инфоблока" (я его выношу в параметры компонента обычно).
$hlblock = HL\HighloadBlockTable::getById($hlbl)->fetch(); 
// get entity 
$entity = HL\HighloadBlockTable::compileEntity($hlblock); 
$entity_data_class = $entity->getDataClass(); 


Further, we can work directly with the Highload info block:

Adding data:
the function is responsible for adding data as always:
$result = $entity_data_class::add($data);

I think it’s not worth explaining much here: a class function.
the fact is, we don’t know how exactly the fields are given to the $ data array. I will explain. This, as we have already seen, "User fields", and the field code is transmitted (UF _ *******).
those.
data = array(
"UF_DATE_FROM"=>'$datefrom',
"UF_DATE_TO"=>'$dateto',
);


Well, after that we add an already superior function.

it is to select the ID after the addition (in case you need to check whether the addition has occurred).
$ID = $result->getId(); 


Deleting data:

to delete data, you just need to know the ID of the element. Everything is simple as usual:
$entity_data_class::Delete($ID); 


Data change:

$result = $entity_data_class::update($ID, $data); 

Feature:
change (you can pass only one parameter that needs to be changed). Those. only one property can be passed, and it will change. For simple information blocks, a separate function is used for this.
Well, $ ID is the ID of the Highload element of the information block.

PS:
To change a custom property of type Y / N, we must give Y / 0, not Y / N as usual. (this is in my experience).

You will find a selection of data from these infoblocks in the components:
highloadblock.list
highloadblock.view


If there is time, I’ll write about one bitrix function that selects trading offers with prices and all their functionality based on an array of elements. (Although you can look through live api).

The article is written for review. If someone knows more - write in the comments.

With uv., Encoder.

Also popular now: