0

There is a way to prevent the editing of my custom contet type from UI? I have created my custom content type programmatically and I added a few fields and field groups. The content type and fields must not be editable by UI but only from code. I have tried to use the property 'locked' in hook_node_info() but without results, the content type is editable yet.

1 Answer 1

0

For ordinary users, you can just disable the "Edit any content" permission for the type in the Node section of Administration » People » Permission GUI:

However if you also need the to prevent user #1 (the default Administrator role) from editing, this will not work, since access controls are not enforced for that user. Instead, you can implement hook_menu_alter() to override the access callback for the node edit page, and provide your own instead:

function MYMODULE_menu_alter(&$items) {
  $items['node/%node/edit']['access callback'] = 'MYMODULE_node_edit_form_access';
}

function MYMODULE_node_edit_form_access($node) {
  $type = is_string($node) ? $node : $node->type;
  if ($type == 'my_type') {
    return FALSE;
  } 
  return node_access('update', $node);
}

(Please note that user #1 should normally be trusted to not do anything stupid, so you should not usually have to use access control tricks to restrain user #1.)

Alternatively, you prevent anyone (including user #1) from editing any field (e.g. the body field) by disabling access the field hook_form_alter. You can do this with any field that makes up the content type.

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'my_form_id') {
    $form['body_field']['body']['#disabled'] = TRUE;
    $form['body_field']['body']['#value'] = $form['body_field']['body']['#default_value'];
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.