1

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 '';
}

2 Answers 2

2

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';
5
  • 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 :) Commented Jun 14, 2013 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
    Commented Jun 14, 2013 at 13:35
  • Of course I will test in a while! Commented Jun 14, 2013 at 13:37
  • Worked great!However, the function that I defined as a callback fails. Commented Jun 14, 2013 at 14:36
  • @PotneySwitters Good luck debuging it, and if you'll have problems with it, ask another question.
    – Mołot
    Commented Jun 14, 2013 at 14:39
1

There is a Drupal module called Content Type Extras that make it easy to add another "Save" button at the top of an edit page. It also includes other extra buttons, such as a "Save and New" button and a "Save and Edit" button.

This module provides some additional features and the ability to set default settings for content type options, such as 'Promoted to front page', 'Display author and date information', etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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