I successfully used this module to submit a "webform" form with a minimum of custom coding.. GREAT!

It submits ok, but nothing happens after the submit.. I see the "ajax" spinner next to it, so it must be possible to make it aware when the submit is done..
Any idea which callback I should use?

All I want to do is close the form, or at least show a message that it has been submitted.
Thanks

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

wojtha’s picture

Component: Documentation » Code
Category: support » feature

Its not possible at the moment, so this is a feature request.

wojtha’s picture

I've extended the CTools Automodals so now they knows how to handle submitted forms. You can try my patch here: #1420534-1: Consider a new hook - hook_ctools_automodal_paths() rather than using hook_menu()

Or just replace ctools_automodal_get_form() with this code:

/**
 * Display a Drupal form using CTools modal or normal page display.
 */
function ctools_automodal_get_form() {
  $args = func_get_args();
  $form_id = array_shift($args);
  $js = $ajax = array_pop($args);

  ctools_automodal_fix_get_q();

  // Load modal options.
  $modal_options = ctools_automodal_get_path(current_path());

  if ($ajax) {
    ctools_include('modal');
    ctools_include('ajax');
    ctools_add_js('ajax-responder');

    $form_state = array(
      'ajax' => $ajax,
      'build_info' => array('args' => $args),
    );
    $commands = ctools_modal_form_wrapper($form_id, $form_state);

    if (empty($commands)) {
      $commands[] = ctools_modal_command_loading();
      if (!empty($_GET['destination'])) {
        $commands[] = ctools_ajax_command_redirect($_GET['destination']);
      }
    }

    // Handle submitted form.
    if (!empty($form_state['executed'])) {
      $commands = array();      
      $commands[] = ctools_modal_command_dismiss();
    }

    print ajax_render($commands);
    exit();
  }
  else {
    array_unshift($args, $form_id);
    return call_user_func_array('drupal_get_form', $args);
  }
}
douggreen’s picture

Category: feature » bug
FileSize
985 bytes

Here's the patch to just fix the bug.

wojtha’s picture

Status: Active » Needs work
+++ b/ctools_automodal.module
@@ -126,7 +126,19 @@ function ctools_automodal_get_form() {
+      $commands[] = ctools_modal_command_dismiss(t('Login Success'));

This doesn't seem like a general fix.

+++ b/ctools_automodal.module
@@ -126,7 +126,19 @@ function ctools_automodal_get_form() {
+      if (isset($_GET['destination'])) {
+        $commands[] = ctools_ajax_command_redirect($_GET['destination']);
+      }

Forms are usualy redirected using form_state[redirect] - this should be handled too imho.

douggreen’s picture

FileSize
1.08 KB

You're right on both accounts, sorry, I was copying code from another module.

douggreen’s picture

Status: Needs work » Needs review
wojtha’s picture

Status: Needs review » Needs work
+++ b/ctools_automodal.module
@@ -126,10 +126,21 @@ function ctools_automodal_get_form() {
+      if (isset($form_state['destination'])) {
+        $commands[] = ctools_ajax_command_redirect($form_state['destination']);
+      }

$form_state['destination'] should be $form_state['redirect']. And notice that $form_state['redirect'] can be both array or string.

wojtha’s picture

Status: Needs work » Needs review
FileSize
1.84 KB
wojtha’s picture

Title: Close modal after submit » Handle submitted forms

Better title

Xen’s picture

If $form_state['redirect'] is an array, it is used as parameters to drupal_goto, which means that the second argument is options.

Added support for rebuilding forms, and cleaned up a bit.

wojtha’s picture

Status: Needs review » Needs work
FileSize
1.59 KB

Nice cleanup @Xen!

+++ b/ctools_automodal.module
@@ -126,12 +127,31 @@ function ctools_automodal_get_form() {
+      elseif (is_array($form_state['redirect'])) {
+        list($path, $options) = array_shift($form_state['redirect']);
+      }

However, doesn't seem right to me.

In the drupal_redirect_form documentation is the following code:

$form_state['redirect'] = array(
  'node/123',
  array(
    'query' => array(
      'foo' => 'bar',
    ),
    'fragment' => 'baz',
  ),
);

So it should be:

list($path, $options) = $form_state['redirect'];

(However in this case we still silently suppose that non-mandatory $options array is present.)

wojtha’s picture

Status: Needs work » Needs review
Xen’s picture

You're right, that was a thinko.

Well, if options isn't set, $options should just be empty... Though the manual doesn't say.

Xen’s picture

Ok, make that:

list($path, $options) = $form_state['redirect'] + array(NULL, array();

Then options is always set, and an array (as expected by ctools_ajax_command_redirect) per default.

Xen’s picture

And patch...

rv0’s picture

Trying to get this to work with forward.module (http://drupal.org/project/forward), but no luck so far.
Submit works, but I get a huge js error (200) afterwards.
When I add an extra submit function to the form, that has nothing but exit(); in it, I don't get the error (but nothing closes)

Any ideas?

bdawg8569’s picture

I am having an issue with submitting a form as well. I want the form to redirect, however, i don't want it to redirect to the same place each time. Inside of the form submit, i am making a call to a function that takes the form and determines if the person should go to one of two places. Is it possible to do this type of thing? If i try to change the form_state['redirect'] i get ajax errors.

wojtha’s picture

Status: Needs review » Needs work

@rv0 with the patch in #15 it should work. May be you can try my experimental build of this module, see #1420534-4: Consider a new hook - hook_ctools_automodal_paths() rather than using hook_menu()

@bdawg8569 are you getting Ajax errors with the patch in #15 or without?

rv0’s picture

@wojtha
I tried the experimental build, but it doesn't work on PHP <5.3 https://github.com/wojtha/ctools_automodal/issues/1

Will also retry #15 on a fresh install.

Xen’s picture

@rv0:
Does the menu item that you're modalling have 'page callback' => 'drupal_get_form'? Automodal doesn't work well with page callbacks that call drupal_get_form themselves.

wojtha’s picture

@Xen good point

@rv0 if it is that case you can take some inspiration from #1450372: How to: How to handle two forms in one modal (unified login page). I've described there how to handle two forms in one modal window. For live example you can go to http://ecommera.com and click on "Register".

rv0’s picture

the menu item looks like this

    'page callback'    => 'forward_page',
    'type'             => MENU_CALLBACK

The forward_page function looks like it hasn't changed since D6 as it has some legacy modalframe code in it.
it applies some logic and then returns a result of drupal_get_form
http://drupalcode.org/project/forward.git/blob_plain/HEAD:/forward.module

Xen’s picture

Then it's really a separate issue. The code this issue deals with determines forms from the menu callback.

Whether there really is a general way of dealing with forms in page callbacks is the question.

Agence Web CoherActio’s picture

Hi,

I've tried patch #15 with a webform having confirmation message set a "No redirect (reload current page)".

The form submits correctly and I can see the throbber. However the confirmation message is not displayed in the modal window after the submission. It is displayed into the background window after a full page reload

Any idea?

Thanks

Laurent

wojtha’s picture

@Laurent, you may try my fork of this module https://github.com/wojtha/ctools_automodal, there you can handle confirmation messages (details here: #1420534: Consider a new hook - hook_ctools_automodal_paths() rather than using hook_menu())