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?

Comments

drum5ormore’s picture

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.

alan d.’s picture

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

Alan Davison
shadowx’s picture

function yourmodulename_form_yournodetypename_node_form_alter(&$form, &$form_state) {
        if(!is_null($form['nid']['#value'])) { 
                $form['title']['#type'] = 'hidden';
        }
}

This could prevent anyone to modify title value.

picxelplay’s picture

/**
* 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';
    }
  }
}

----
Sudo Kill Cylons

alan d.’s picture

Even better :)

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
    $form['title']['#access'] = user_access('administer node titles');
  }
}

Alan Davison
skizzo’s picture

I succeeded setting automatic node titles via Rules in Drupal 6, now I would like to remove the required Node Title field from form submissions for a given Content Type (I don't care about 'administer node titles' permissions). Could you please suggest a mini-module to that purpose? I have no PHP knowledge, but I guess it wouldn't be much different from above... Thx.

alan d.’s picture

You are right :)

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
    $form['title']['#access'] = FALSE;
  }
}

This will eliminate all title fields from showing in the CMS.


Alan Davison
skizzo’s picture

Ok, the title setting has gone, as expected, but (with stock Garland) I am now getting "You don't have Javascript enabled" from Hierarchical Select form element (first position in form, right before title). If I disable the minimodule then JS is ok. It looks like the taxonomy drop-down selector and the node title are somehow interfering.

alan d.’s picture

You could use the FAPI #prefix/#suffix attributes to hide the title using a div that has the CSS style of display:none;. The JScript should still work with the hidden title. Remove the #access line as this prevents the HTML form being rendered. As title is required, you may need to set this to false or prepopulate it with a dummy value


Alan Davison
nealhene’s picture

Thanks for the code. I implemented this in Drupal 7 using hook_permission() instead of hook_perm(). Also targeted a specific content type by adding "&& $form['#node']->type == "MYCONTENTTYPE")". Here it is in full...

function MYMODULE_permission() {
  return array(
    'administer node titles' => array(
      'title' => t('Administer node titles'),
      'description' => t('Perform administration of node titles.'),
    ),
  );
}

function MYMODULE_form_alter(&$form, $form_state, $form_id) {
  // Check that the form is a content type edit/create form and that node is of type "core_condition"
  if (isset($form['#node']) && $form['#node']->type .'_node_form' == $form_id && $form['#node']->type == "MYCONTENTTYPE") {
    // If they do not have the title permission, hide the title field
    $form['title']['#access'] = user_access('administer node titles');
  }
}
ccarnnia’s picture

I printed out $form and looked through it and modefied the code to match my needs:

...
if (isset($form['#vocabulary']) && $form['#vocabulary']->machine_name == "my_unique_machine_name_for_a_taxonomy_term") {
...

This was very helpful
Thank you

Casey Carnnia (carnnia@gmail.com)

ofridagan’s picture

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

ipsocannibal’s picture

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.

chumoka’s picture

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.

drupalnesia’s picture

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'

alan d.’s picture

this is node/xxx/edit so it is arg(2) == edit

easier to go

<?php
if (arg(1) == 'add') {
// new
}
else {
// edit
}
?>

Alan Davison
not_Dries_Buytaert’s picture

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

shivams’s picture

That just saved me a lot of hassle. Awesome workaround. Thanks.

sparkymark’s picture

subscribe

markconroy’s picture

Just wondering if there is an update for this? We have 2,500 groups using OG and want the groups to remain titled as we set them, to make sure that the url stays uniform

============

Drupal Core Maintainer for "Out of the Box" Initiative.

pverrier’s picture

Take a look at http://drupal.org/node/419458#comment-7069916, may be useful for simple needs on Drupal 6.