Hello,

I am trying to find the best way to reload the parent page when a modal has been closed.

There is a related issue, but it doesn't seem to work in this case. The related issue is here: http://drupal.org/node/561788

I have tried doing it in jQuery directly, but because the dialog modal is created by ctools, it is not working as expected. So I need to create a command to reload the page when the modal has just closed. Any one done this, or have any ideas of the best way to do it?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

blairski’s picture

Just to add to this, I have now achieved the desired effect in jQuery. Here is the code:

Drupal.behaviors.productClose = function(context) {
  var closeText = '<a href="#" class="ui-icon ui-icon-closethick ui-dialog-titlebar-close">Close</a>';
  var close = $(closeText).click(function(){

    // Remove the content
    $('.ui-dialog').remove();

    //Reload the page
    location.reload(true);
  });

  //Replace the existing Close button with the new one
  $('.ui-dialog-titlebar-close').replaceWith(close);

}

So essentially I am replacing the exisiting close button (the X in the top right corner of the modal) with my own which has an event handler attached. It works, but it would be better to do it in CTools as a command.

merlinofchaos’s picture

Status: Active » Fixed

I don't think you really want to do this when the modal is closed; don't you want to do this only when the modal has successfully completed? Using the close button usually indicates a cancel and it should return to the previous state.

The server can send ctools_ajax_command_reload() instead of a ctools_ajax_command_dismiss() to do a reload when whatever is in the modal is complete.

Also, the jquery you're using affects the jQuery UI dialog, not the CTools modal. Maybe you're actually using dialog.module?

blairski’s picture

We have a specfic reason why we need the page to reload on close. Basically the modal has a product form where the user can find and add a product, but they can keep adding more and more if they like in a loop, so the modal doesn't complete in a normal way. When the user has finshed, they close the modal, but need to see an update page based on what they added in the modal. I thought others might have a similar requirement.
But we have since decided that reloading the page is not the best experience, so we are now going to update the page via Ajax, so it seems this won't be necessary.

And yes, we are using the dialog module along with ctools. But I thought we could use a ctools command with dialog modal.

merlinofchaos’s picture

I definitely agree that using AJAX to add the items to the page is the better experience than reloading the page.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

travist’s picture

Just some documentation on what I consider the best way to do this...

  • In your finish callback put the following...
       $form_state['complete'] = TRUE;
    
  • In your cancel callback routine, but the following...
      $form_state['canceled'] = TRUE;
    
  • Now in your main ctools modal function ( the one that defines the $form_info for the modal ), you can put the following...
       ...
       ...
      $output = ctools_wizard_multistep_form($form_info, $step, $form_state);
    
      // If the wizard is finished, then we will want to close it out and then refresh the page.
      if($output===FALSE || $form_state['complete'] || $form_state['cancel']) {
        $commands = array();
        $commands[] = ctools_modal_command_dismiss();
        $commands[] = ctools_ajax_command_redirect();
        ctools_ajax_render($commands);
      }
      else {
        // Render the wizard page.
        ctools_ajax_render(ctools_modal_form_render($form_state, $output));
      }
    

That worked beautifully for me... Hope this helps someone in the future.

pfrenssen’s picture

Version: 6.x-1.6 » 7.x-1.x-dev
Issue summary: View changes
Status: Closed (fixed) » Active
FileSize
509 bytes

The above only works when the "Cancel" button is clicked in a multistep form made with the form wizard, not in regular modals. I'm reopening this as I am also looking for a way to react to a modal being closed.

Can't this be solved rather easily by triggering a behavior? Currently the 'CToolsDetachBehaviors' fire when the modal is closed, but unfortunately these also fire when the modal is dismissed due to a form being successfully submitted. This cannot be used if an action should only occur when closing, not when submitting.

Attached patch adds this behavior. You can then run whatever javascript you want by implementing the behavior, eg:

(function ($) {
  $(document).bind('CToolsCloseModalBehaviors', function(context) {
    alert('close button was clicked');
  });
})(jQuery);
pfrenssen’s picture

Status: Active » Needs review

firewaller’s picture

I know this is old, but the functionality would be very helpful. Patch #7 works for me, but fires a bunch of times.

ron_s’s picture

Aha! This patch is exactly what I needed.

Here is our issue: We're using Ajax Facets in a ctools modal window to allow users to filter information without needing to reload another page. Ajax Facets does its filtering by changing the URL of the originating page.

If Ajax Facets is being used on a standard search page, this would be ok, because it's changing the URL from something like /search to /search?f[]=type:blog. However, when using Ajax Facets within a modal, this becomes a problem because the underlying page's URL is changed from /blog/my-blog to /search-modal?f[]=type:blog. Once the modal is closed, the URL is still at "search-modal" even though the user is on the blog page.

An easy way to fix this is to reset the browser's URL path using window.history.pushState when the modal is closed. The patch in #7 lets me do exactly that.

The previous patch no longer applies since there have been some changes to ctools since it was first created. I've attached a new version for review.

Please set to RTBC if others agree with adding this. Thanks.

ron_s’s picture

@firewaller, you said it "fired a bunch of times". Can you provide some more details?

I am not seeing that behavior. When I close the modal window it fires once. It does not fire again until I close the modal again.

ron_s’s picture

Patch #11 still applies cleanly to 7.x-1.17.

joelpittet’s picture

Status: Needs review » Fixed
Parent issue: » #3178697: Plan for CTools 7.x-1.18 release

Thanks all! I've committed this to the dev branch for the next release.

  • joelpittet committed ceb8a80 on 7.x-1.x authored by pfrenssen
    Issue #826786 by ron_s, pfrenssen, blairski, merlinofchaos, travist,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.