0

I am trying to accomplish the following functionality:

I have a profile2 registration form to which I have added a custom form element (just a submit button) using hook_form_alter().

The submit button has a #submit property set to a custom_callback function.

For debugging purposes, the callback function just displays a

drupal_set_message('hello world');

The general idea is that the button is used to gather information from a webservice (in the callback function) and then fill the form with the returned values.

The user can then check the values and submit the user registration form.

When I click the custom submit button, it fires the custom callback. But when I click the user register form submit button, it also fires the custom callback?

I have checked the dpm of $form and the #submit properties are as follows:

$form['#submit'][0] == de default (correct) user registration callback
$form['custom']['#submit'][0] == de custom submit handler

So, any clues on why the default submit handler no longer works?

EDIT:

My form alter implementation looks like this (just add an image button)

   $form['custom'] = array(
    '#name' => 'custom',
    '#type' => 'image_button',
    '#src' => drupal_get_path('module', 'custom') . '/logo.png',
    '#value' => t('get data'),
    '#prefix' => '<div class="button-wrapper">',
    '#attributes' => array('alt' => t('get data')),
    '#suffix' => '</div>',
    //'#weight' => -99,
    '#submit' => array('custom_callback'),
  );

Thanks in advance!

  • Can you also post your hook_form_alter code also in the question? – Sumit Madan Dec 19 '14 at 12:40
  • Of course no problem. – Guardian Dec 19 '14 at 12:52
2

Problem seems to be custom form element. Change it to :

$form['custom'] = array( 
    '#name' => 'custom', 
    '#type' => 'submit', 
    '#value' => t('get data'), 
    '#prefix' => '<div class="button-wrapper">', 
    '#attributes' => array('alt' => t('get data')), 
    '#suffix' => '</div>', 
    '#submit' => array('custom_callback'), 
  ); 
  • I have tried your suggested solution, but the result is the same. Which makes sense I guess, since it was just another syntax of adding something to the array #submit I want the 2 submit buttons to go to 2 different callbacks.. Thanks already though! – Guardian Dec 19 '14 at 12:53
  • Remove #submit from the custom and add it to $form array. – Sumit Madan Dec 19 '14 at 12:54
  • and no need to add extra submit button actually. – Sumit Madan Dec 19 '14 at 12:55
  • If I would add it to $form['#submit'], how would I be able to distinct between the 2 buttons? Could you also elaborate on the comment that I would not need 2 buttons? (I need to have access to $form_state in my custom callback function) – Guardian Dec 19 '14 at 12:57
  • You can call two submit callback from single existing button. – Sumit Madan Dec 19 '14 at 12:57

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.