SugarCRM readonly custom field based on user role
A few days ago I responded to a question on stackoverflow.com about creating a custom field that is rendered read-only to users who have a particular role. The realization of this behavior on the commercial versions of SugarCRM is almost trivial but it is not on the Community Edition.
We want to add a custom field of type checkbox called Processor SignOff on the Task module of SugarCRM and want to make read-only access to this field to members of the role ITOperation. The field must be available on the layout of edit-view. To easily reach the goal, proceed as follows:
- Step 1: Create the custom field of type checkbox from the Studio environment;
- Step 2: Create or edit the metadata of the edit view of where editviewdefs.php indicate how to render the new custom field;
- Step 3: Create or edit SugarView::display() to make sure that the smarty template is possible to know when to display the custom field read-only or not.
Listing 1 shows the code snippet of editviewdefs.php responsible for rendering the control on the form editing Task module, and Listing 2 shows the extent of the method SugarView::display() that retrieves the user's role and sets the variables for the Smarty Template.
array ( 'name' => 'processor_signoff_c', 'label' => 'LBL_PROCESSOR_SIGNOFF', 'customCode' => '', ),
Listing 1. Code fragment of the definition of the custom field.
/** * @see SugarView::display() */ public function display() { if($this->ev->isDuplicate){ $this->bean->status = $this->bean->getDefaultStatus(); } //if global $current_user; // check if current user is in specific role $isEnabledRole = in_array("ITOperation", ACLRole::getUserRoleNames($current_user->id)); if($isEnabledRole) $this->ev->ss->assign('readOnly', 'readonly = "readonly"'); $this->ev->ss->assign('disabled', 'disabled'); else $this->ev->ss->assign('readOnly', ''); $this->ev->ss->assign('disabled', 'disabled'); parent::display(); }
Listing 2. Implementation of the method SugarView::display()
This customization should be done in safe mode so the two files affected by the change will be on the custom directory and exactly:
- File metadata edit view in custom/modules/Tasks/metadata/editviewdefs.php;
- Class file SugarView view.edit.php in custom/modules/Tasks/views/view.edit.php
To apply the new customization just make a nice and Quick Repair and your new custom field based on role is done.