Views: 9667
Last Modified: 22.09.2014
Creating a Custom Field to Nonstandard Objects
Sometimes custom fields have to be created for the objects which do not support custom fields by default. In this case, a custom property for such object must be created independently.
Let us consider this situation by taking blog comments as an example. For example, each comment must have the Raiting property. We create a custom property of the required type in the administrative part (Control Panel >Settings > System settings > Custom fields) and complete all the fields. In the field Entity we indicate any name for the object. The most important thing is that the name must be unique. In our case, let us write BLOG_RATING. The following functions must be used to read and write values of custom properties:
function SetUserField ($entity_id, $value_id, $uf_id, $uf_value) //value write
{
return $GLOBALS["USER_FIELD_MANAGER"]->Update ($entity_id, $value_id,
Array ($uf_id => $uf_value));
}
function GetUserField ($entity_id, $value_id, $uf_id) //value read
{
$arUF = $GLOBALS["USER_FIELD_MANAGER"]->GetUserFields ($entity_id, $value_id);
return $arUF[$uf_id]["VALUE"];
}
// $entity_id - object name (in our case, "BLOG_RATING")
// $value_id - element identifier which property we save or obtain. In our case, it is a comment ID
// $uf_id - customer property name (in our case, UF_RATING)
// $uf_value - a value we save
Example:
SetUserField ("BLOG_RATING", $CommentID, "UF_RATING", $Rating);
echo "Comment’s rating: ".GetUserField ("BLOG_RATING", $CommentID, "UF_RATING");
Creating custom fields manually is not very convenient compared to the use of GetList functions for objects supporting custom properties by default. However, it permits you to use custom properties for arbitrary objects fast and easy.
Creating Own Object
It is possible to create any object and work with it as you like. Example:
$GLOBALS["USER_FIELD_MANAGER"]->Update("GRADEBOOK_RESULT", $ID, Array("UF_TEACHERS"=>$arValue));
$arUserFields = $GLOBALS["USER_FIELD_MANAGER"]->GetUserFields("GRADEBOOK_RESULT", $ID);