I am using the Content Moderation control block, which adds a form at the top of a Draft node page to allow easily setting a new workflow state. This form has a single button built as:
$form['submit'] with a form submit handler as $form['#submit'] = [':submitForm']. I am trying to add a 2nd button which runs the form submit handler but also runs its own submit handler.
Adding this to a hook_form_alter seems as though it should work:
$form['apply_and_send'] = [ '#type' => 'submit', '#name' => 'apply-and-send', '#value' => t('Apply and send newsletter'), '#weight' => 4, '#submit' => '_send_newsletter_email', ]; I have tried many variations of this such as wrapping both buttons in an "actions" wrapper, setting this button's handler to both the form handler and my new handler and then removing the form handler and a couple others variations. The only option which causes my custom handler to get hit is to simply add it to the form handler like this:
$form['#submit'][] = '_send_newsletter_email'; but even then, everything in $form_state suggests it was still the original button that has been clicked (op, triggeringElement, etc.). The only way I can tell that my new button has been clicked is to look in $_POST.
Is there a way to properly add a 2nd button with its own handler?
--- EDIT --------------- From comments below, here is current version of full form_alter:
function ssc_newsletters_form_content_moderation_entity_moderation_form_alter(&$form, &$form_state): void { $form_handler = $form['#submit']; // Add form level handler to existing Apply button. $form['submit']['#submit'] = $form_handler; // Add 2nd button to "apply and send". $form['apply_and_send'] = [ '#type' => 'submit', '#name' => 'apply-and-send', '#value' => t('Apply and send newsletter'), '#weight' => 4, '#submit' => $form_handler, ]; // And add new submit handler to send email. $form['apply_and_send']['#submit'][] = '_send_newsletter_email'; unset($form['#submit']); } I have tried with/without #name and as well as unsetting or not the original $form['#submit']. Same result in all cases, :submitForm is run, but my added handler is never hit.