开发者

How can I get the node id within the submit handler defined in hook form alter

开发者 https://www.devze.com 2023-04-12 00:27 出处:网络
Is there a different hook I can use to get the node_id of a NEW node that is submitted? function dc_project_management_form_bug_request_node_form_alter(&$form, &$form_state, $form_id)

Is there a different hook I can use to get the node_id of a NEW node that is submitted?

function dc_project_management_form_bug_request_node_form_alter(&$form, &$form_state, $form_id)
{
    $form['#submit'][] = 'dc_project_management_process_bug_request_milestone_submit';
}

开发者_Python百科function dc_project_management_process_bug_request_milestone_submit($form, &$form_state)
{
    //NULL when submitting new node
    $form_state['values']['nid'];
}


The node hasn't actually been saved at that point, you need to implement hook_node_insert:

function dc_project_management_node_insert($node) {
  $nid = $node->nid;
}


The only way to retrieve the node id is to use hook_node_insert. However, if you wish to make modifications to the node object from inside this hook, you must notify Drupal of the alteration, otherwise the changes won't make it into the database transaction and will be lost.

After you're done modifying the node, call field_attach_updates('node', $node). For example:

function mymodule_node_insert($node){
    $node->field_myfield['und'][0]['value'] = 'a new value';

    field_attach_update('node', $node);
}

See http://timonweb.com/how-save-yourself-some-hair-when-manipulating-node-fields for more information.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号