0

I am altering a node form with hook_form_alter and adding a couple of submit handlers. When the form gets submitted they get picked up using:

foreach (array_keys($form['actions']) as $action) {
  if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
    array_unshift($form['actions'][$action]['#submit'], 'submit_handler_a');
    $form['actions'][$action]['#submit'][] = 'submit_handler_b';
  }
}

Problem is, if the form contains a field that can store multiple values, as soon as I click on the 'Add more' button for that field and a new value for it, when submitting the form my handlers are not being picked up.

What am I doing wrong?

0

Have a look on those form elements in the form array. They might have their own ['#submit'] in the array and you might need to add your custom handler on those too

0

I had to change hook_form_alter to hook_form_BASE_FORM_ID_alter to make it work. My code ended up looking like:

function mymodule_form_node_form_alter(&$form, &$form_state, $form_id) 
{
  if ($form['#form_id'] == 'node_mycontettype_form') {
    foreach (array_keys($form['actions']) as $action) {
      if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
        array_unshift($form['actions'][$action]['#submit'], 'submit_handler_a');
        $form['actions'][$action]['#submit'][] = 'submit_handler_b';
      }
    }
  }
}

My custom handlers are now being picked after a multiple value field submits multiple values. I don't know why this made the difference and can't find anything in the docs, but I hope this helps people that run into the same issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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