Cannot create a Custom Field Types based on the Relate Field Type

I’ve been experimenting with creating a new Field Type based on a Relate field but with some extra functionality. The documentation on anything more complex than a Colour Picker Field is rather difficult to find or is out of date. So my approach was based on reading code, guess work and some debugging. Which means that I may have got this wrong but what I found suggests that a custom version of a Relate field cannot work. This is because Studio puts the custom Field’s type in the “type” vardef entry when one of the Fields is added to a module. There seems to be a mass of code that treats Relate fields as a bit special in terms of setting the vardef value from the related module and this only seems to work when the Field type is “relate”. The net effect is that the new Field shows in the Detail and Edit views, allows selection of a related record and saves the ID of the related record to the database table. However, when subsequently trying to show the data, the Field is empty. If you edit the Studio created vardef and change the “type” to “relate” then it work BUT the code that is used to get the data and display it is the original Relate Field code not the custom code so bypassing any addition functionality in the custom code.

Has anyone else tried this?

For anyone who may be interested…

I got this to work by adding a function to custom/include/SugarFields/fields//SugarField.php that returns the Name of the Related Module record and is called in the getDetailViewSmarty() and getEditViewSmarty() functions;

    function _getValue($vardef) {
        
         $myID = $_GET['record'];
         if ($myID == '') $myID = $_POST['record'];
         $myModule = $_GET['module'];
         if ($myModule == '') $myModule = $_POST['module'];
         $myBean = BeanFactory::getBean($myModule, $myID);
         
         $otherIDName = $vardef['id_name'];
         $otherID = $myBean->{$otherIDName};
         $otherModule = $vardef['module'];
         $otherBean = BeanFactory::getBean($otherModule, $otherID);
         $otherName = $otherBean->name;

         return $otherName;
         
    }