Drupal 7 – Check add or edit in node form alter

If we want to alter a node form of a specific content type using hook_form_alter(), we may want to know whether the form is for node creation or edit. In this case, we can add a checking as follow:

function <module>_form_<content-type>_node_form_alter(&$form, &$form_state, $form_id) {
  $node = $form_state['node'];
  
  if (isset($node ->nid)) {
    // Node edit
  } else {
    // Node add
  }
}

 

Done =)

4 thoughts on “Drupal 7 – Check add or edit in node form alter”

Leave a reply to dskanth Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.