Skip to content

Instantly share code, notes, and snippets.

@damz
Created March 5, 2010 17:36
Show Gist options
  • Save damz/322948 to your computer and use it in GitHub Desktop.
Save damz/322948 to your computer and use it in GitHub Desktop.
/**
* Make a CCK field read-only on form edition.
*
* @param $form
* The form, from hook_form_alter().
* @param $field_name
* The name of the field to make read-only.
*/
function readonly_field(&$form, $field_name) {
if (empty($form[$field_name])) {
return;
}
// The original field structure.
$field_structure = $form[$field_name];
// Load the field definition.
$field = content_fields($field_structure['#field_name'], $field_structure['#type_name']);
$new_structure = array(
'#tree' => TRUE,
'#weight' => $field_structure['#weight'],
);
foreach (element_children($field_structure) as $sub_element) {
$element = $field_structure[$sub_element];
// Transform the field into an item.
$new_structure[$sub_element . '_display'] = array(
'#type' => 'item',
'#title' => $element['#title'],
'#value' => content_format($field, $element['#default_value'], $field['display_settings']['full']['format']),
);
$new_structure[$sub_element] = array(
'#type' => 'value',
'#value' => $element['#default_value'],
);
}
$form[$field_name] = $new_structure;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment