A
A
avshivtseva2018-05-08 16:57:03
1C-Bitrix
avshivtseva, 2018-05-08 16:57:03

How to create a custom infoblock property type?

How to create a custom infoblock property type that consists of three fields with string, file, HTML/text types.
So far I've been able to do this

AddEventHandler("iblock", "OnIBlockPropertyBuildList", array("CIBlockPropertyCustom", "GetUserTypeDescription"));

class CIBlockPropertyCustom
{
    public function GetUserTypeDescription()
    {
        return array(
            "PROPERTY_TYPE"        => "S", // тип свойства — строка
            "USER_TYPE"            => "SERVICE",
            "DESCRIPTION"          => "Услуга",
            "GetPropertyFieldHtml" => array("CIBlockPropertyCustom", "GetPropertyFieldHtml"),
        );
    }

    // вывод поля свойства на странице редактирования
    public function GetPropertyFieldHtml($arProperty, $value, $strHTMLControlName)
    {
        return '<input type="text" name="'.$strHTMLControlName["VALUE"].'" value="'.$value['VALUE'].'">
        <input type="file" name="'.$strHTMLControlName["VALUE"].'" value="'.$value['VALUE'].'">
        <textarea name="'.$strHTMLControlName["VALUE"].'" id="" cols="30" rows="10" value="'.$value['VALUE'].'"></textarea>


        ';
    }

    //сохраняем в базу
   function ConvertToDB($arProperty, $value){
       return $value;
   }
   //читаем из базы
   function ConvertFromDB($arProperty, $value){
       return $value;
   }
}

But only one value is stored for all fields

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Irina, 2018-05-09
@iitovka

1) because you have multiple fields, they cannot have the same name. everyone should set
name="'.$strHTMLControlName["VALUE"].'[1]"
name="'.$strHTMLControlName["VALUE"].'[2]"
name="'.$strHTMLControlName["VALUE" ].'[3]"
2) The base type of your property is a string. so saving the value to the base is just as a string. the file is not saved anywhere from the form and is not processed in any way.
Saving to the database you must write yourself here
function ConvertToDB($arProperty, $value){
return $value;
}
for example, from the field $value['VALUE'][2] - you will have a picture. it will first need to be saved, it is possible by means of Bitrix so that the file is written to the b_file table, and you only have the id of the file left, and this id is already written to the value of the custom property.
I didn’t do custom properties with file type fields, but here is an example of saving string + select with multiple choice + string
function ConvertToDB($arProperty, $value) {
if(!$value['VALUE'][1]) return false;
$value['VALUE'][2]= implode('+', $value['VALUE'][2]);
$value['VALUE']= implode('||', $value['VALUE']);
return $value;
}
i.e. Here I also check if the first input is not filled, then the property is not saved.
the value itself is eventually written from arrays to strings. you can also do serialization.
and in ConvertFromDB - parse everything back.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question