How to set Accounts field to read only in DetailView for Non-Admin users

I’m trying to set the Account Number field to read only for non Admins. I added the following code to \custom\modules\accounts\metadata\detailviewsdef.php but the Account Number field still shows as being editable in the Account’s detail view. I successfully changed the EditView to show the account number as read only for non-Admins but can’t figure out how to change the DetailView screen to read only.
This is how my detailviewdefs.php starts:

<?php
$viewdefs ['Accounts'] = 
array (
  'DetailView' => 
  array (
    'templateMeta' => 
    array (
      'form' => 
      array (
        'buttons' => 
        array (
          0 => 'EDIT',
          1 => 'DUPLICATE',
          2 => 'DELETE',
          3 => 'FIND_DUPLICATES',
          'AOS_GENLET' => 
          array (
            'customCode' => '<input type="button" class="button" onClick="showPopup();" value="{$APP.LBL_GENERATE_LETTER}">',
          ),
        ),
      ),
      'maxColumns' => '2',
      'widths' => 
      array (
        0 => 
        array (
          'label' => '10',
          'field' => '30',
        ),
        1 => 
        array (
          'label' => '10',
          'field' => '30',
        ),
      ),
      'includes' => 
      array (
        0 => 
        array (
          'file' => 'modules/Accounts/Account.js',
        ),
      ),
      'useTabs' => false,
      'tabDefs' => 
      array (
        'LBL_ACCOUNT_INFORMATION' => 
        array (
          'newTab' => false,
          'panelDefault' => 'expanded',
        ),
        'LBL_EDITVIEW_PANEL1' => 
        array (
          'newTab' => false,
          'panelDefault' => 'expanded',
        ),
        'LBL_PANEL_ADVANCED' => 
        array (
          'newTab' => false,
          'panelDefault' => 'collapsed',
        ),
      ),
      'syncDetailEditViews' => true,
    ),
    'panels' => 
    array (
      'lbl_account_information' => 
      array (
        0 => 
        array (
          0 => 
          array (
            'name' => 'name',
            'comment' => 'Name of the Company',
            'label' => 'LBL_NAME',
          ),
  		  1 =>
		    array(
            'name' => 'account_number',
            'comment' => 'The client account number',
            'label' => 'Account_Number',
			'type' => 'readonly',
			'customCode' => '{if $IS_ADMIN}@@FIELD@@{else}{$fields.account_number_c.value}{/if}',
          ),

		  
		  
/*		  
		  1 => 
          array (
            'name' => 'account_number_c',
            'label' => 'LBL_ACCOUNT_NUMBER',
          ),
*/		  

etc

Thanks for your help - greatly appreciated.

Hi, @David_iTeks!
You can switch off the function edit in line for this field:

'inline_edit' => false,

Hi P Konetskiy
that disables inline editing for the entire view - can I use the disable command just before the Account Number field then re-enable it to accomplish my task? how do I make it so Admins can inline edit but not just the user?

Thanks

Disable the Inline edit for Account Number that will solve Detail View problem.
For Edit view Add your Custom JS to editviewdefs, if the person logged in is non Admin, make the Account Number Input field read only.

@David_iTeks
I recommend to do it in backend. You can make custom function ‘preDisplay’ file: cuscom/modules/Accounts/views/view.edit.php . The function prepare metadata of the view for the module.

Problem is I’m not a programmer when it comes to SuiteCRM - I can look at code and change a function but that’s a s far as I can go
Can you direct me with a link to a site that has that preDisplay function?

@David_iTeks
Without problems. I started as you sometime before.

  1. Copy files: ‘view.detail.php’ and ‘view.list.php’ from:
    modules/Accounts/views/ to: custom/modules/Accounts/views
  2. Add function ‘preDisplay’ to: ‘custom/modules/Accounts/views/view.detail.php’
...
    public function preDisplay(){
        global $current_user;
        if (is_admin($current_user)) {
            $this->bean->field_name_map['account_number']['inline_edit']=true;
            $this->bean->field_defs['account_number']['inline_edit']=true;
        }
        parent::preDisplay();
    }
...
  1. Edit function ‘preDisplay’ in: ‘custom/modules/Accounts/views/view.list.php’
  • before:
...
    public function preDisplay()
    {
        require_once('modules/AOS_PDF_Templates/formLetter.php');
...
  • after:
...
    public function preDisplay()
    {
        global $current_user;
        if (is_admin($current_user)) {
            $this->bean->field_name_map['account_number']['inline_edit']=true;
            $this->bean->field_defs['account_number']['inline_edit']=true;
        }
        require_once('modules/AOS_PDF_Templates/formLetter.php');
...

Not sure why but after implementing in a test environment, a non-admin can still perform an inline edit of the field Account Number. I did a quick repair & rebuild but still allows editing in list view and detail view.
Edit view isn’t allowing an edit which is what I also wanted.

@David_iTeks

  1. I tested it. Do you do this?

You shouldn’t do ‘Quick Repair & Rebuild’

  1. For editview you can do it as in your the first post with some changes.
'customCode' => '{if $IS_ADMIN}@@FIELD@@{else}<span class="sugar_field" id="account_number_c"> {$fields.account_number_c.value} </span>{/if}',