3

I have added a #submit handler to the default submit action on a node edit form:

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

and it works correctly. But I then add a 2nd submit button as:

  $form['actions']['save'] = [
    '#type' => 'submit',
    '#name' => 'Standard Save',
    '#value' => t('Print and Mail Check'),
    '#button_type' => 'primary',
    '#weight' => 6,
  ];

and for some reason the submit handler for the submit button gets run for this button as well. I am sure in my handler I can act depending on which button was pressed; but wondering why I need to do that.

EDIT

Apparently I am asking the wrong question. From my code above it seems that regardless of which button i click it is still submitting as if the main button is pressed. So question is how can i add another button (I have done this dozens of times in D7 and I thought a few times in D8; so not sure why this isn't working).

1 Answer 1

4

Give your submit button a unique and valid #name attribute. I don't think it will work if you put spaces in it:

$form['actions']['save'] = [
  '#type' => 'submit',
  '#name' => 'standard-save',
  '#value' => t('Print and Mail Check'),
  '#button_type' => 'primary',
  '#submit' => [
    '_my_module_form_submit',
  ],
];

Submit handler

function _my_module_form_submit($form, \Drupal\Core\Form\FormStateInterface $form_state) {
  \Drupal::messenger()->addStatus(t('Standard Save button triggered.'));
}
1

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.