0

I had a rule of 'After saving new content' of type, redirect to url. I need to add second submit button to the content type so I decided to add the redirect to a custom module so I can have different actions depending on the button. The redirect works on the submit button but I only want this to happen when adding/inserting a new node. Right now it redirects every time the submit button is clicked.

In mymodule_form_alter:

$form['actions']['submit']['#submit'][] = 'submission_form_submit';

Then:

function mymodule_form_submit($form, &$form_state) {  
  $form_state['redirect'] = 'info-received';
}

Code suggestions needed. But if there is a way to have rules perform different actions based on which button is clicked I can use that advice also.

ETA: the code used for the second button

$form['actions']['submit_another'] = array(
  '#type' => 'submit',
  '#name' => 'save_another',
  '#value' => t('Save, and Add Another.'),
  '#submit' => array('node_form_submit','submission_form_create_duplicate_form_submit'),
  '#weight' => 40,  
);

ETA2: Trying is_new

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

  if(isset($node->type) && $node->type == 'submission') {
    dsm('Reached Node Type Check!'); // <- this works

    if (isset($node->is_new)) {
    dsm('Reached New Node Check!'); // <- this does not work
    $form_state['redirect'] = 'info-received';
    }
  } 
}

Changed the if statement checking for new node to

if ($node->is_new) {    

Results in 'Notice: Undefined property: stdClass::$is_new in mymodule_form_submit()'

It seems the value for is_new is not set.

1

You could use hook_node_insert() under this hook should use drupal_goto() for redirect. The hook_node_insert() only trigger when a new node inserted. If you also want to update. You could use hook_node_update().

  • Correct me if I am wrong, but wouldn't hook_node_insert() get triggered on the second button also since it happens every time? I have added the code for the second button so you can see what I am doing. On the second button I don't want the redirect to occur. – Venn May 12 '14 at 15:29
0

In your $form or in your $form_state, you have the node object, in which you can check if it's a new node by the key is_new, the value being a boolean.

You can base a condition for the redirection on this.

  • See above for results of your suggestion – Venn May 12 '14 at 17:42
  • This doesn't work for all nodes, including new ones ? Or only updating ? – Gregory Kapustin May 12 '14 at 21:32
  • Added else { dsm('Not New Node!'); } to the check for is_new and got 'Not New Node!' when updating and when inserting a new node. Is_new definitely is not being set. – Venn May 13 '14 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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