4

I would like to add a second button to an exposed view form.

I am able to get the button to display, but not to have it call the button submit handler. Instead, it behaves just like the standard search button that views provides automatically.

How can I have my second button call a separate handler?

Below is what I've tried to do.

(FWIW, my specific goal is to let the user use the exposed view form to search for an existing node, then if he doesn't find it, to go ahead and post a new node. So, I want a button that will call an action to post a new node.)


function my_module_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'views_exposed_form':
      if ($form['#id'] = 'views-exposed-form-my-module-page-1') {
        // Add a button
        $form['my_button']['#type'] = 'button';
        $form['my_button']['#value'] = t('My Extra Button');
        $form['my_button']['#submit'] = array('my_module_button_handler');
      }
      break;
  }
}

function my_module_button_handler($form, &$form_state) {
  // do something here
}
  • A little more information in case it helps. In trying to debug this, I noticed something odd. If I add a form submit handler and then look at $form_state['clicked_button']['#value'], it turns out that it doesn't matter what button is clicked. This always contains the value of view's original submit button even when 'My Extra Button' is the one clicked. – chipcleary Apr 16 '11 at 15:26
  • Even a bit more information. I had the view using AJAX. If I turn it off, I can get my form handler to be called. However, I cannot identify how to redirect the form. Instead, the form is called again with the menu path '.../search?keys=test+me&op=My+Button. (Note the '#name' of My Button gets automatically assigned to be 'op'). – chipcleary Apr 17 '11 at 2:41
  • How to find the view form id? – pal4life Oct 7 '14 at 20:50
2

The submit handler is not called by default on a form api element type "button". If you want to execute a submit handler on a button you will need to add

$form['my_button']['#executes_submit_callback'] = TRUE;

As a side note this will not be the case if the #type was 'submit' because the executes_submit_callback is by default equal to true on that element type.

Hope this helps.

|improve this answer|||||
  • Thanks, @ericduran. Unfortunately, this didn't work. FWIW, I tried both this and simply making it a 'submit' button. – chipcleary Apr 16 '11 at 15:22
  • Worked fine for me. I was doing $form['my_button']['#type'] = 'button'; and it didn't work without this. – Miloš Kroulík May 16 '16 at 9:59
2

OK, I got it to work.

Needed to do several things:

1) Turn off AJAX on the form. (When I had it on, it acted as if I clicked on the default button even when I clicked on My Button ($form_state['clicked_button']['#value'] was always the default button).

2) In my_module_button_handler, not only set $from_state['redirect'] but also set $form_state['no_redirect'] = FALSE.

If you would know how to have it work with AJAX on, I'd appreciate the tip.

|improve this answer|||||
  • Yea, I was going to say make sure to turn of ajax. Seems unfair that my answer wasn't chosen, :-( Lol but is ok are you sure the you didn't need to add the executes submit callback to true? feel that this answer is missing stuff. – ericduran Apr 18 '11 at 17:55

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.