I am writing a custom module that uses form_alter to add a custom field to an existing content type. Everything works great on existing nodes, however, when adding a new node my form submit handle does not work because $form_state['values']['nid'] does not have a value.

I have verified my submit handled is the last one in the submit handler array.

How can I get the node id of the newly create node in the form submit handler?

Comments

jaypan’s picture

You don't add a custom submit handler to content types, you use hook_insert() and hook_update().

D6: hook_nodeapi() (with $op as either 'insert' or 'update')
D7: hook_node_insert() and hook_node_update()

Contact me to contract me for D7 -> D10/11 migrations.

nevets’s picture

Actually hook_insert() and hook_update() only work if the content type is defined by the module.

To avoid the problem of adding a submit handler and nid not being set your module can define a hook_nodeapi() and handle the cases where $op equals 'insert' or 'update', $node->nid will be set in both these cases.

drooopalstars’s picture

Use $form_state['nid'] in submit handler. It works.

martin_q’s picture

$form_state['nid'] isn't set, whether you are editing or creating a node.

martin_q’s picture

It seems that this depends on how you have your submit_handler attached.

If you include the following within your implementation of hook_form() for your custom node type:

$form['#submit'][] = 'mynodetype_form_submit_handler';

then $form_state['nid'] is undefined.

 

However, if instead you include the following within your module's implementation of hook_form_alter() then it is available as you say:

$form['buttons']['submit']['#submit'][] = 'mynodetype_form_submit_handler';
stijntilleman’s picture

I found for drupal 7 it's:

$form['actions']['submit']['#submit'][] = 'mynodetype_form_submit_handler';
amit.drupal’s picture

use in drupal 7
function mymodule_custom_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'user_profile_form')
{
$form['#submit'][] = 'ekn_user_profile_form_submit';
}
}

2pha’s picture

I can confirm that when trying to access a node ID of a newly created node in a submit handler that using
$form['#submit'][] = 'my_node_submit'
$form_state['node']->nid is null

But using
$form['actions']['submit']['#submit'][] = 'my_node_submit'
$form_state['node']->nid is the id of the newley created node

chintan4u’s picture

In D7 my submit function is not getting called when i m using $form['actions']['submit']['#submit'][] = 'my_node_submit'
but its getting call when i m using $form["#submit"][] = 'my_node_submit'

-
Chintan Umarani
Drupal Developer
www.umarani.com

gdesmarais’s picture

Put in both?

I am going to put them in, and then test for the existence of nid. I'd rather hook into the form execution than the generic insert or update.

marysmech’s picture

You are right. If I use:
$form['actions']['submit']['#submit'][] = 'my_node_submit'
It actually works. Thank you.

gauravktomar’s picture

Good help!!