How to handle the 'Save and edit' form action? I'm copying code from a module that does more or less what I want. However, the module I'm copying has a bug... The form we're trying to extend has both a 'Save' button and a 'Save and edit' button. The following code only seems to work when the 'Save' button is pressed, not when the 'Save and edit' button is pressed...

function x_form_apachesolr_environment_edit_form_alter(&amp;$form, &amp;$form_state, $form_id) {

  $environment = reset($form_state['build_info']['args']);
  $is_external = apachesolr_environment_variable_get($environment['env_id'], 'is_external', FALSE);

  $form['make_external'] = array(
    '#type' =&gt; 'checkbox',
    '#title' =&gt; t('Make this environment external'),
    '#default_value' =&gt; $is_external,
  );

  $form['actions']['save']['#submit'][] = 'x_environment_edit_submit';
}

function x_environment_edit_submit($form, &amp;$form_state) {
  if ($form_state['values']['make_external']) {
    apachesolr_environment_variable_set($form_state['values']['env_id'], 'is_external', TRUE);
  }
  else {
    apachesolr_environment_variable_del($form_state['values']['env_id'], 'is_external');
  }
}
The way I do it is to add extra parameters, like:

$form['actions']['save']['#machine_name'] = 'save';
$form['actions']['save_edit']['#machine_name'] = 'save_edit';


Button position in $form found here.

Then in submit handler I can use:

switch($form_state['triggering_element']['#machine_name']) {
  case 'save_edit':
    // some code
  case 'save':
  default:
    // come other code
}


Alternative method:

Note that both buttons have submit handlers attached directly to them:

$form['actions']['save'] = array(
  '#type' =&gt; 'submit', 
  '#validate' =&gt; array('apachesolr_environment_edit_validate'), 
  '#submit' =&gt; array('apachesolr_environment_edit_submit'), 
  '#value' =&gt; t('Save'),
);
$form['actions']['save_edit'] = array(
  '#type' =&gt; 'submit', 
  '#validate' =&gt; array('apachesolr_environment_edit_validate'), 
  '#submit' =&gt; array('apachesolr_environment_edit_submit'), 
  '#value' =&gt; t('Save and edit'),
);


So, in your own code you can append submit functions separately. Assuming you wish to keep original ones executed, in your form alter use something like:

$form['actions']['save']['#submit'][] = 'x_environment_edit_save_submit';
$form['actions']['save_edit']['#submit'][] = 'x_environment_edit_save_edit_submit';


and of course create these functions.