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.

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

<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>