As both answers above mention, there are two ways to hide a field on the node edit form:
- via css.
- via a form alter hook.
CSS Solution
On the node creation page the body tag has class "page-node-add", but on the edit node page - "page-node-edit", so you can hide the field with CSS in your theme like so:
.page-node-edit .field-name-field-myfieldname {
display: none;
}
The CSS-based solution has two main caveats:
- An astute user will be able to unhide the field, modify it, and submit the form. There is nothing on the server-side checking to prevent submitting a change.
- If you're using the admin theme on node edit, then adding custom css to the page is a bit more complicated than just adding this line of CSS to your front-end theme's stylesheet. You'll need to either add it via a custom module, or hack the admin theme (not recommended), or implement a custom child-theme of the admin theme.
For these reasons, I recommend the second option, use a form alter hook to hide the desired field:
Hook_Form_Alter Solution
You should add the following snippet in a custom module enabled on your site. You may either remove the form element entirely with '#access' => FALSE
or leave the form element visible but non-editable using '#disabled' => TRUE
.
/**
* Implements hook_form_alter().
*/
function MYMODULE_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'MYNODETTYPE_node_form') {
if (isset($form['#node']) && isset($form['#node']->nid)){
// Prevent editing a field once the node has been created by
// hiding the field entirely on the edit form.
$form['field_MYFIELDNAME']['#access'] = FALSE;
// Alternatively, if you want to display the field, but prevent
// it from being edited, you can disable the form element.
$form['field_MYFIELDNAME']['#disabled'] = TRUE;
}
}
}
This solution has no caveats really, and is probably the better solution.