2

Is there a way to detect if the node form being viewed is the "edit" or "add new node" form?

Laxman13
  • 5,226
  • 3
  • 23
  • 27
dorong123
  • 103
  • 1
  • 5

4 Answers4

4

Detect where? In a hook_alter? In a template? Somewhere else?

In general, the approach would be to get ahold of the $node object, and see if it's nid field is set. If it is, it's an edit.

Tyler Eaves
  • 12,879
  • 1
  • 32
  • 39
2

check these answers from drupal.stackexchange.com

for example:

function mymodule_form_node_form_alter(&$form, &$form_state) {
  $node = $form_state['node'];

  if (!isset($node->nid) || isset($node->is_new)) {
    // This is a new node.
  }
  else {
    // This is not a new node.
  }
}

or use the arg() function as previously has been indicated.

Community
  • 1
  • 1
moonw
  • 372
  • 2
  • 15
2

Also you can make use of URL, if you don't want to load entire node object. When it is new node addition, then in the URL arg(0) will be "node", arg(1) will be "add", arg(2) will be "content_type_name" whereas in the case of node viewing arg(0) will be node and arg(1) will be nid(i.e Numeric). This is just an alternative way to detect.

Sameer
  • 1,764
  • 4
  • 16
  • 21
0
if ($node->is_new) {do_something_for_new_node();}
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
Tom
  • 1,381
  • 3
  • 15
  • 26