Search code examples
custom-fieldsvtigerextension-modules

VTiger Extension Module create custom field for Accounts Module


I'm working on a VTiger 6.4.0 Extension Module that is used to get company data when entering a company name in the Accounts module.

The module is almost finished, i retrieve data from a API and enter them in the input fields using JQuery.

But the problem is that i have some data that is not relative to the existing fields in the account information, so i'm trying to create some new custom fields.

Only i can't seem to figure out how to create a custom field for the Accounts module from within my Extension module.

I googled around and watched some posts on stackoverflow.

I came up with the following part of code, but this doesn't seem to work.

public function addKvkfield(){

    $module = new Vtiger_Module();
    $module->name = 'Accounts';
    $module = $module->getInstance('Accounts');

    $blockInstance = new Vtiger_Block();
    $blockInstance->label = 'LBL_ACCOUNT_INFORMATION';
    $blockInstance = $blockInstance->getInstance($blockInstance->label,$module);

    $fieldInstance = new Vtiger_Field();
    $fieldInstance->name = 'KvKNummer';
    $fieldInstance->table = $module->basetable;
    $fieldInstance->column = 'kvknummer';
    $fieldInstance->columntype = 'VARCHAR(100)';
    $fieldInstance->uitype = 2;
    $fieldInstance->typeofdata = 'V~M';
    $blockInstance->addField($fieldInstance);
}

The addKvkfield function is being called in the vtlib_handler module.postinstall (Couldn't find any information if this is the right way of doing this within a Extenstion Module)

vtlibhandler:

function vtlib_handler($modulename, $event_type) {
    global $log;
    if($event_type == 'module.postinstall') {
        $this->addJSLinks();
        $this->createConfigTable();
        $this->addSettingsMenu();
        $this->addKvkfield();   
        $this->updateLabels();

        // TODO Handle post installation actions
    } else if($event_type == 'module.disabled') {
        // TODO Handle actions when this module is disabled.
    } else if($event_type == 'module.enabled') {
        // TODO Handle actions when this module is enabled.         
    } else if($event_type == 'module.preuninstall') {
        // TODO Handle actions when this module is about to be deleted.
    } else if($event_type == 'module.preupdate') {
        // TODO Handle actions before this module is updated.
    } else if($event_type == 'module.postupdate') {
        $this->updateLabels();
        // TODO Handle actions after this module is updated.
    }
}

Hopefully someone can give me a push in the right direction.

Thanks in advance :)


Solution

  • I managed to succeed in creating the custom fields that i needed in the Accounts Module.

    Thanks to the Vtiger Mailing List! :)

    What did the trick was a small alteration of the code I've written:

    public function addKvkfield(){
    
            $module = Vtiger_Module::getInstance('Accounts');
            $blockInstance = Vtiger_Block::getInstance('LBL_ACCOUNT_INFORMATION', $module);
    
            $fieldInstance = new Vtiger_Field();
            $fieldInstance->label = 'KvKNummer';
            $fieldInstance->name = 'kvknummer';
            $fieldInstance->column = $fieldInstance->name; // Good idea to keep name and columnname the same
            $fieldInstance->columntype = 'VARCHAR(100)';
            $fieldInstance->uitype = 1; // No need to use 2 anymore. Setting "M" below will introduce the Red asterisk
            $fieldInstance->typeofdata = 'V~O';
            $blockInstance->addField($fieldInstance);
    
    }
    

    The above code will create a (optional)Custom Field in the Account module.

    If your writing a new module and never installed this module before you can just call the function in the vtlib_handler as i did in my question.

    But in my case this did not work because I've already installed the plugin before adding the code to create the customfields.

    So what i needed to do is call the function above on the vtlib_handler module.postupdate (this will add the custom field on a module update)

    Only problem with this is that it'll get run every time the extenstion is updated.

    So i suggest creating a if statement in the function to check if the field already exists in the vtiger_field dbtable if not run the script.

    Hopefully i saved someone else some time by writing this all down :P

    Goodluck!