Often in the development process we are faced with the task of hiding certain fields depending on the value of the field. There are many modules on the market that implemented this feature without the need for programming, but they work in javascript. Those, the fields are hidden only after the page is fully loaded. This also causes some inconvenience to users and creates an additional load on the system.
Therefore, I decided to share a simple way to hide fields at the php level. Luckily, it's very easy and fast. All you need to do is create 1 new file and make small changes to two existing files.
For example, let's imagine such a task - we need to show a certain set of fields depending on the status of the deal at the moment.
First, let's create a configuration file where we will store an array of fields that we will allow to be displayed for the Deals module.
Let's create a vtiger_detailview_list.php file at the root with the following content
<?php
$detailview_list = array(
'Potentials' => [
'checkField' => 'cf_1269',
'checkValue' => 'Submission Received',
'allowedFields' => [
'potentialname', 'potential_no', 'cf_1269', 'cf_3709', 'cf_3713', 'cf_3867', 'cf_4368', 'cf_3711', 'cf_4421', 'forecast_amount', 'cf_1926'
]
]
);
In order for the configuration array to be available as a global variable, you need to import this file into the config.inc.php configuration file
require_once 'vtiger_detailview_list.php';
What is a configuration array? This is a multidimensional array divided by modules. The key of the first level is the name of the module. In it, we describe which field we are checking in the checkField key (its system name) and its value in the checkValue key. Further in allowedFields we describe the list of fields that we allow to be shown.
The hiding itself takes place in the modules/Vtiger/models/DetailRecordStructure.php file.
Here we need to add a function:
protected function isFieldAllowed(string $fieldName, ?Vtiger_Record_Model $record): bool
{
global $detailview_list;
if (!$record) {
return true;
}
if (!isset($detailview_list[$record->getModuleName()]['checkField'])) {
return true;
}
$fieldValue = $record->get($detailview_list[$record->getModuleName()]['checkField']);
if ($fieldValue === $detailview_list[$record->getModuleName()]['checkValue']) {
return in_array($fieldName, $detailview_list[$record->getModuleName()]['allowedFields'], true);
}
return true;
}
Next, in the getStructure function, near line 39, rewrite the condition. Instead of
if($fieldModel->isViewableInDetailView())
replace
if($fieldModel->isViewableInDetailView() && $this->isFieldAllowed($fieldName, $recordModel))
And that's enough. Now if the transaction status is "Submission Received", then in the Detail View we will see only those fields that are registered in the array. Of course, this method does not protect us from displaying fields in edit mode or displaying them in a general list, but I hope the general approach is clear.