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

I want to add an extra button on the node/edit drupal 7 form. That can be done by hooking into the form_alter:

$form['actions']['push'] = array(
 '#type' => 'submit',
 '#value' => t('Save && Push'),
 '#callback' => 'my_callback',
);

In my callback I want to run my custom Push function and save the node.

Any ideas how to achieve this?

function my_callback_action(&$form, &$form_state) {
  dpm('my_callback_action');
  return '';
}
share|improve this question

1 Answer

up vote 2 down vote accepted

You need to add your own extra submit function to your form. The big question is - where is default one?

If default function is attached to form itself, all you need to do is:

$form['#submit'][] = 'my_callback';

Then in callback test if it was your button that triggered it.

If it's attached to button, you need to copy original button's submit array to your button, and attach your callback:

$form['yourbutton']['#submit'] =  $form['originalbutton']['#submit'];
$form['yourbutton']['#submit'][] ='my_callback';

If it's attached to form, but you want yours attached to button, the way is of course:

$form['yourbutton']['#submit'] = $form['#submit'];
$form['yourbutton']['#submit'][] = 'my_callback';
share|improve this answer
It's the default edit node form of drupal so I guess the save is the default functionality. That being said, the second should work :) – Potney Switters Jun 14 at 13:32
@PotneySwitters both ways are used in Drupal, I can't tell from memory which one is in default node edit. And can't guarantee it'll stay that way ;) Actually I find first way more common - that's why I posted it first. But you need to test. Testing it should be simple. – Mołot Jun 14 at 13:35
Of course I will test in a while! – Potney Switters Jun 14 at 13:37
Worked great!However, the function that I defined as a callback fails. – Potney Switters Jun 14 at 14:36
@PotneySwitters Good luck debuging it, and if you'll have problems with it, ask another question. – Mołot Jun 14 at 14:39
show 1 more comment

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.