Prevent users edit a node's title
ofridagan - May 30, 2008 - 19:47
I want to let users edit only the body of a node but not the title (which the admin sets).
How can I do it?
I want to let users edit only the body of a node but not the title (which the admin sets).
How can I do it?
I can't imagine Drupal has this functionality out of the box
It seems like you'd probably have to write a module that hooks into the update process, then checks to see if the user who made the change is an administrator.
hook_form_alter
The hook_form_alter will allow you to remove this field unless the user has the required rights. Much better never to show the title field in the edit forms. This does require custom coding, something similar to:
<?php
/**
* Implementation of hook_perm
* This gives a new role in the admin section, just for this task
*/
function MYMODULE_perm() {
return array('administer node titles');
}
function MYMODULE_form_alter(&$form, $form_state, $form_id) {
// Check that the form is a content type edit/create form
if (isset($form['#node']) && $form['#node']->type .'_node_form' == $form_id) {
// If they do not have the title permission, hide the title field
if(!user_access('administer node titles') {
$form['title']['#type'] = 'value';
}
}
}
?>
I haven't tested/run this code, but it should guide you towards what you require.
Alan Davison
www.caignwebs.com.au
Thank you!
I hoped I'll find a way to hide the title in the 'edit' form.
Now, I just need to learn how to write a Drupal module... finally found a reason to do it :-)
Module Available
I've created a module to do this sort of thing. Check it out here. I developed it in response cck field permissions and token/auto nodetitle not playing nicely together but it should suit your purposes.
Drupal 6
Hi I'm trying to do the same thing in Drupal 6, is there any easy way to prevent users edit node title or do I have to go into hard coding?
What I want to achive is that once a node is published, the owner of the node would only be able to edit the body of the node but not the title. Any idea how to do it?
Thanks.
Title Coding in Drupal 6
Drupal URL will look like this when ADD node: node/add/account-company --> arg(0)/arg(1)/arg(2)/ ......
To prevent only for ADD node then check: arg(1)= 'add'
To prevent only for EDIT node then check: arg(1)= 'edit'
www.drupal-id.com - Komunitas Drupal Indonesia
+62 811 988 1247, +62 21 91776868
this is node/xxx/edit so it
this is node/xxx/edit so it is arg(2) == edit
easier to go
<?phpif (arg(1) == 'add') {
// new
}
else {
// edit
}
?>
Alan Davison
www.caignwebs.com.au
Workaround
I discovered a workaround (not a bug fix) to prevent non-authorized roles from editing the standard node Title field of certain content types:
1) install and enable the module "Automatic Nodetitles" (http://drupal.org/project/auto_nodetitle).
2) Add a CCK text field called "node_title" requiring (!) 1 (!) plain text (!) value.
3) Make sure that only admins have permission to edit and/or view aforementioned field (by default this should be like that).
4) Configure the "Automatic title generation"-section within the content type of your choice like so:
4.1) Check the radio-button: "Automatically generate the title and hide the title field", so the standard node Title field:
4.1.a) automatically copies the value from aforementioned CCK field upon saving (!) the node and
4.1.b) does not display on the node edit form.
4.2) Enter this PHP-pattern:
<?php return '[field_node_title-formatted]'; ?>4.3) Check this checkbox: "Evaluate PHP in pattern.".
5) For each node of the content type: edit the value of aformentioned CCK field and save the node.
BTW: This issue is also being discussed here: http://drupal.org/node/419458
Running Drupal 6.14 (or any newer stable version if available) with numerous modules for multi-sites.