Vtiger CRM gives us an excellent opportunity to create fields quickly and easily in any module. But there is a catch with this approach.
In the field editor, you can set the field title, even its length, but when creating a field, the system assigns a name to the newly created field automatically, and the name will be completely unreadable, such as cf_997. Where cf is abbreviated "Custom Field", and 997 is the number of the new field.
And this is not at all convenient for development or for API integration. If you create only one field, then that's okay. And if there are a lot of custom fields, then such a variety of numbers turns into a nightmare.
You will simply be forced to check the base of what your field is called to insert its number into the code. But what if instead of a number there will be some clear text. For example, cf_friend_name. Agree, this is much more convenient and easier to support?
Below I offer some recommendations for changing the field editor so that you can set your own name for the field. To do this, you will need basic programming knowledge and make adjustments to 4 files.
At first we need to change file layouts/v7/modules/Settings/LayoutEditor/FieldCreate.tpl, near 77.
Under this line:
{if !$IS_FIELD_EDIT_MODE}
Add following:
<div class="form-group">
<label class="control-label fieldLabel col-sm-5">
{vtranslate('Field Name', $QUALIFIED_MODULE)}
<span class="redColor">*</span>
</label>
<div class="controls col-sm-7">
<input type="text" class='inputElement col-sm-9' maxlength="50" {if $IS_FIELD_EDIT_MODE}disabled="disabled"{/if} name="fieldName" value="{vtranslate($FIELD_MODEL->get('label'), $SELECTED_MODULE_NAME)}" data-rule-alphanumeric='true' style='width: 75%' />
</div>
</div>
Next change file modules/Settings/LayoutEditor/models/Module.php near str 146
Change lines:
$max_fieldid = $db->getUniqueID("vtiger_field");
$columnName = 'cf_'.$max_fieldid;
$custfld_fieldid = $max_fieldid;
To this lines:
if(!empty($params['fieldName'])){
$columnName = 'cf_'.$params['fieldName'];
}
else
{
$max_fieldid = $db->getUniqueID("vtiger_field");
$columnName = 'cf_'.$max_fieldid;
$custfld_fieldid = $max_fieldid;
}
Then we need to add some kind of validation so that the system accepts only numbers and letters, as well as a lower-case character. Columns in the database are created with the same name as the field name. Therefore, without validation there is nowhere.
layouts/v7/modules/Settings/LayoutEditor/resources/LayoutEditor.js on line 854,
Under this line:
form.find('[name="fieldLabel"]').attr('data-rule-illegal', "true");
add this lines:
form.find('[name="fieldName"]').attr('data-rule-alphanumeric', "true");
layouts/v7/modules/Vtiger/resources/validation.js на строке 22 добавляем:
jQuery.validator.addMethod("alphanumeric", function(value, element, params) {
if(value){
var regex = /^[a-z0-9_]+$/g;
if(!value.match(regex)){
return false;
}
}
return true;
}, jQuery.validator.format("Small Letters, numbers, and underscores only please"));
So, now when you write in the friendname field, the system will create a new field called cf_friendname. Those. prefix cf_ will be added before the field name
This is a system prefix that says the field was created manually and can be safely removed from CRM.
If you do not fill out the field, then everything will go as usual and the name will be created automatically from the numbers.