Back

Disable Structure Fields in Back-end UI

Description

This javascript, when placed into a custom field, will disable fields listed in the array "fieldsToDisable." making them uneditable by back-end UI users. It is best to keep this custom field at the bottom of the list of fields, otherwise you'll see "can't set attribute on null" errors in the javascript console.

Additionally, you can use the following velocity code within the custom field to restrict editing to users with the role "CMS Administrator." Thanks to Chris Falzone for the velocity code.

#set($session = $request.getSession())
#set($user_id = $session.getAttribute('USER_ID'))
#set($user_obj = $cmsuser.getUserByUserId($user_id))
#set($user_map = $cmsuser.getUserProxy($user_obj).getMap())
#set($user_roles = $cmsuser.getUserRoles($user_obj))

#if(!($cmsuser.isUserRole($user_obj, 'CMS Administrator')))
<!-- put your javascript and css code here -->
#end

In addition, when fields are disabled, they can become a little unreadable in Google Chrome. To off-set this, and to also distinguish these fields from editable fields, I put some css above the javascript. Edit it to match your situation.

Code

## Disable Structure Fields in Back-end UI
<style type="text/css">
   #name { color: red; }
   #dateDate { color: red; }
   #dateTime { color: red; }
</style>

<script type="text/javascript">
/* The values in fieldsToDisable are the fields' IDs. You'll need to get these 
    via Firebug or Chrome's Inspect Element function. Most of the time they'll 
    match what's under the field's Variable name in the Structure, but some 
    may use the field's Index Name. Best to check the source. */
var fieldsToDisable = new Array("name","dateDate","dateTime");
var i;
for (i=0; i<fieldsToDisable.length; i++) {
 dojo.byId(fieldsToDisable[i]).setAttribute("disabled", true);
}
</script>