tags:

views:

1992

answers:

3

I'm developing a module which alter display of add/edit node forms. I'm a beginner in module development.

I have written following code, it is not working properly. Please tell me what's wrong with this?

function hook_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'node_form') {    
    drupal_set_message(t('some message.'));
  }  
}

This is for drupal 6.

+1  A: 

you should not call your function "hook_form_alter" but rather "yourmodule_form_alter". yourmodule should be the name of your module file, so if your module is called "hello" the function name should be "hello_form_alter".

Nir Levy
+10  A: 

In addition, the node add/edit forms have content-type specific IDs. so story nodes would be:

function mymodule_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'story_node_form') {    
    drupal_set_message(t('Editing a story node!'));
  }  
}

If you're looking to catch every node edit form, regardless of type, try this:

function mymodule_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['#node']) && $form_id == $form['#node']->type .'_node_form') {
    drupal_set_message(t('Editing a node!'));
  }  
}
Eaton
Thanks you, I'm able to add new fields in the form but not able to alter or remove existing fields.
Sharique
You should be able to do all of those things -- you mgiht check to be sure that your module is running AFTER other modules that alter the story node form; if your module makes its changes, then CCK (for example) adds another field, there's not much you can do.If you need to get the very last crack at a form, after all other modules do their thing, check the http://drupal.org/project/util module. Among other things, it lets you change the order in which modules execute their hooks.
Eaton
you can also use mymodule_form_story_node_form_alter as the function name, for specific types
Jeremy French
+1  A: 

You need to understand the hook system that Drupal uses if you want to do module development. When a hook is implemented somewhere in Drupal, it's called hook_something. The way this works is that every time a module wants to implement the hook, you need to replace the 'hook' with the 'modelname'. Drupal will know it's an instance of that hook and call it as such. Drupal also uses the same system in the theming layer, with a twist. As it will look at the different theming functions and prioritize them based on a ranking system, that enabled themes to override the way modules output data.

googletorp

related questions