tags:

views:

30

answers:

1

I am having a hard time using the $op variable when working with forms. It seems that the $op variable is generally part of node_api (which I haven't really used), but what about the $op variable in a form alter?

How can I make my form alter apply when the node is being edited vs. created?

+2  A: 

There is no $op variable in hook_form_alter(). The standard way to discern a node edit from a create form is to look if the node already has a nid (node id). If it has one, you know it is an already existing one, otherwise it is still in the process of being created:

function yourModule_form_alter(&$form, &$form_state, $form_id) {
  // Is this a node edit/create form at all?
  if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
    // Yes, is the node an already existing one?
    if (isset($form['#node']->nid)) {
      // Yes, existing node, add manipulation for node edit form
    }
    else {
      // No, new node, add manipulation for node create form
    }
  }
}
Henrik Opel