I'd like to disable/remove the Delete button on nodes when they are being edited so the options are only Preview, Preview Changes and Submit.

From other posts it looks like I have to use the hook_form_alter function, but I don't understand how that works and where it goes. Do I have to copy the code somewhere? And if so, where?

Thanks in advance.

Comments

hook_form_alter

To call hook_form_alter and other functions, you'll need to create a small custom module for your site. I usually end up creating a little 'helper module' for this kind of thing on most sites.

Once you have an empty module, you can add a hook_form_alter function to it and then check for the form_id to see which form is currently being processed and, if it matches the one you want to modify, add whatever code you want to alter the form elements.

I normally start by doing a print_r() of the form object itself - which will fill your screen with junk, but if you look at the source, you'll see it's a big array with all of the form elements. From there, you can grab the ids for the various elements you want to modify.

http://drupal.org/node/508 - should get you started on module creation.

Thanks for the advice. I

Thanks for the advice. I made a module called Extras as you suggested based on the tutorial in the handbook and some code on this post here. Can you tell me if it is right? Or do I need to change the "TYPE_CONTENT" to "nodeform"? I want the function to apply to all nodes.

<?php
// $Id$

/**
* Display help and module information
* @return help text for section
*/
function extras_help($section='') {

$output = '';

switch ($section) {
case "admin/help#extras":
$output = '

'. t("Removes Delete button when editing nodes"). '

';
break;
}

return $output;
} // function extras_help

/**
* Valid permissions for this module
* @return array An array of valid permissions for the extras module
*/

function extras_perm() {
return array('access extras content');
} // function extras_perm()

/**
* Disable the Delete button on node edit
*/

function extras_form_alter($form_id, &$form) {

switch ($form_id) {

case 'TYPE_CONTENT_node_form':
$form['delete']['#disabled'] = true;
break;
}
} // function extras_form_alter()

TYPE_CONTENT

Hi,

yes, TYPE_CONTENT is a placeholder which you need to replace with your content type. For example, in one of mine I just looked at, it is case 'book_node_form':

So, I would guess that you want 'node_node_form', but if you do a dump of the $form array, or add a debug message like drupal_set_message('extras_form_alter called on form:'.$form_id, true); to your form_alter function, it will tell you the form id when you visit a page with a form.

If I replace TYPE_CONTENT

If I replace TYPE_CONTENT with a particular content type, the function works fine, but it doesn't work for 'node_node_form'. The Delete button is still there and operational.

I get that I should only apply the function to nodes which are forms, but since I'm a PHP newbie, I don't understand how to alter the code
or employ your snippet above to make that happen. If you could explain that, that would be a huge help. Thanks again.

Form Alter Id

Drop the message snippet into your function like this. Then, when you visit a page with the form you want to alter, it should tell you the $form_id that you need to add to your switch statement.

function extras_form_alter($form_id, &$form) {
  drupal_set_message('extras_form_alter called on form:'.$form_id, true); // this will give you the id of the form you want to alter
  switch ($form_id) {
    case 'TYPE_CONTENT_node_form':
      $form['delete']['#disabled'] = true;
    break;
  }
} // function extras_form_alter()

Alternatively, if you have the correct $form_id, and your code is removing the 'delete' button on some forms but not others, it is possible that another module is also altering the same form and adding the delete button back in again. In which case you might need to adjust the module 'weight' so that yours is called later, after the other modules are done with their changes.

Just one question - how do you intend to delete nodes?!

Just one question - how do

Just one question - how do you intend to delete nodes?!

Err...you got me there! :) Obviously I just want the button disabled for all users other than those with administrator role so I guess I need
an if statement for that. Thanks for alerting me to that and your help with the code.

nobody click here