Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a alter section about particular content type.i need to 2 method in alter form. first in create new and second for edit node. in form alter how i can find out this create new from or edit from

with dsm($form) in from_alter i get result with serveral difference between those,what is the popular and the best way to distinguish those from each other? is this a good way?

    if(isset($form['nid']['#value']))
     'means in edit form'
    else 
     'means in create new from'
share|improve this question

2 Answers

up vote 3 down vote accepted

Yes, you have to check if the node ID exists or not.

share|improve this answer

If you look at the code of node_object_prepare(), which is called from node_form() (the form builder for the node edit/create form), it contains the following code.

  // If this is a new node, fill in the default values.
  if (!isset($node->nid) || isset($node->is_new)) {
    foreach (array('status', 'promote', 'sticky') as $key) {
      // Multistep node forms might have filled in something already.
      if (!isset($node->$key)) {
        $node->$key = (int) in_array($key, $node_options);
      }
    }
    global $user;
    $node->uid = $user->uid;
    $node->created = REQUEST_TIME;
  }

In an implementation of hook_form_BASE_FORM_ID_alter(), it is enough to use code similar to the following one.

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.
  }
}

If the node is new, then the form is creating a node; if the node is not new, then the form is editing an existing node.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.