Problem/Motivation

Redirecting a modal form that is overlayed on path 'node/1' to path 'node/1#comment-37' using the ctools_ajax_command_redirect() function leaves the modal up.

Proposed resolution

No workarounds or proposed solutions yet.

Remaining tasks

Workarounds and solutions to be submitted and reviewed.

Comments

merlinofchaos’s picture

Status: Active » Fixed

You need to be more specific. How are you redirecting?

Note that browsers, when you give the SAME URL and only change the fragment, do not actually change locations. So if you're using the ctools_ajax_command_redirect command, and this is the behavior you get, that is standard browser behavior. If that's what you're doing, what you'll actually need to do is a reload, or to rerender what you need and dismiss the modal and then redirect.

jantoine’s picture

@merlinofchaos,

I was indeed using the ctools_ajax_command_redirect command. I figured it was defaulting to browser behavior, but with the modal being present, wasn't sure if ctools should be responsible for cleaning that up first. I have used a reload, but that doesn't include a hash to the latest comment that has been added. I will try the re-render approach.

Status: Fixed » Closed (fixed)

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

j0rd’s picture

Status: Closed (fixed) » Active

I have the same "feature".

Here's why it looks like a bug. IMHO it should force a reload to avoid problems.

Load a comment form in a modal, while browsing a node which lists is comments. Then submit a comment from the modal and ctools_ajax_commend_redirect() to /node/NID#comment-1234 . Normally in Drupal, this would be done via a 301 header redirect and things would work as expected, but with ctools redirect it's just closing the modal and trying to bump the browser down to #comment-1324, which doesn't exist until the page reloads.

I believe most devs (correct me if I'm wrong) would expect ctools_ajax_command_redirect() to work more like a PHP header 301, instead of mimicking "hitting a link", which is currently what it's doing.

Feel free to re-close this if you think it's still not a bug.

merlinofchaos’s picture

Status: Active » Fixed

I don't know why you'd expect that. It's simply browser behavior, and it acts the way it does. Your code probably needs to actually render the new comment before issuing the redirect, and then all will be well.

Status: Fixed » Closed (fixed)

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

kle’s picture

My workaround/solution to redirect and jump to an anchor:
The redirect I've put in a function:

 /**
 * c_tools_ajax_redirect()
 * 
 * @param $redirect    url
 * @param $anchor      CSS-ID in destination-page
 *
 *  redirect on AJAX-Form is a pain
 *  $form_state['redirect'] = 'admin';  // can not work.
 *  This solution uses ctools.  [http://tagmycode.com/snippet/452/d7-ajax-redirection]
 */
function my_tools_ajax_redirect($redirect, $anchor = NULL) {
  if ($anchor) {
    // loads page again - and jumps to ID -> see my_tools_init()
    $_SESSION['go_anchor'] = $anchor; 
  }
  ctools_include('ajax');
  ctools_add_js('ajax-responder');
  $commands = array();
  $commands[] = ctools_ajax_command_redirect($redirect);
  print ajax_render($commands);
  exit;
} 

and hook_init redirects again - this time to the anchor:

function my_tools_init() {
  // $_SESSION['go_anchor']  redirects to url#[go_anchor-value]
  if (isset($_SESSION['go_anchor'])) {
    $anchor = $_SESSION['go_anchor'];
    unset($_SESSION['go_anchor']);
    drupal_goto($_GET['q'], array('fragment' => $anchor));
    exit;
  }
} 
4alldigital’s picture

Issue summary: View changes

to anyone that comes across this like me... useful info above but wanted to share my workaround also:

  $now = time();
  $options = array(
    'fragment' => 'my-fragment',
    'query' => array('updated' => $now)
  );
  $commands[] = ctools_ajax_command_redirect('my-url', 0, $options);

with a changing 'timestamp' seems to trick the browser into refreshing current page with fragment.

shabana.navas’s picture

I still agree with the others and think this is a bug. I've had the same problem as j0rd in comment #4, where I have a feature to add comments through a ctools modal. I wanted to reload the page and then jump the user to the newly created comment. Obviously, with using, ctools_ajax_command_reload();, it'll just reload the page. But the behavior I expected from ctools_ajax_command_redirect(), was that it would go the url that I specify and then, after including a fragment, jump to the newly created comment. But it's not reloading the page.

Redirect, in essence means, go to a certain page right? So, after a comment is saved, we're saying go to a certain page, and jump to a particular fragment. It's not reloading/redirecting, just jumping to the fragment, so that's essentially a bug.