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 =)

Advertisements

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s