1

I have two form submission buttons: the normal one and one added via hook_form_alter.

I'm currently using the normal default one as a save button which updates the node content as normal.

The second one which is called submit does the same thing, but will be changing a checkbox via $form_state prior to saving the node content.

The code I'm using is the following one:

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {

  if ($form_id == 'example_node_form') {

    //dpm($form);
    $form['actions']['finishbutton'] = [
      '#type'   => 'submit',
      '#value'  => t('Submit'),
      '#submit' => array_merge($form['actions']['submit']['#submit'], ['custom_submit']),
      '#weight' => 99,
    ];
  }
}

function custom_submit($form, &$form_state) {

  drupal_set_message(t('This works, but shows up after the node submit message'));

  // Shows after the node save message.
  dpm($form_state);

  // Doesn't update.
  $form_state['values']['field_nomination_submitted'][LANGUAGE_NONE][0]['value'] = 1;
}

My submission works, but the custom handler appears to be firing after the standard submission handler. Do I need to switch the array_merge order? Do I need to break this into separate functions? Do I potentially need to add a submit function inside of custom_submit?

  • 2
    Given the arguments passed to array_merge(), custom_submit() is put after the other submission handlers. – kiamlaluno Nov 5 '18 at 21:16
  • 1
    Yes, your function would need to fire first – Kevin Nov 5 '18 at 21:20
  • Cool, apparently using [] syntax instead of Array() and switching the order works. Thank!. – Shawn Matthews Nov 5 '18 at 21:21
  • Maybe consider buying a PHPStorm license. And then configure it to follow Drupal coding standards. This will probably prevent you lots of headaches in the future. – leymannx Nov 5 '18 at 21:42
1

Use [] syntax instead of Array() and switch the order ... that should work.

Credits: Shawn Matthews.

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.