I'm working on using ctools modal and ajax as a replacement for popups api. For the first part of the replacement path I'm working on the user login and register. I've got it working so far thanks to a little guidance from Roger Lopez and, of course, thanks to Earl for the awesome code in the first place.

Here is the code I have going for me so far:

/**
 * Implementation of hook_menu().
 */
function mc_menu() {  
  $items['user/login/%ctools_js'] = array(
    'page callback' => 'mc_ajax',
    'page arguments' => array(1, 2),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  
  $items['user/register/%ctools_js'] = array(
    'page callback' => 'mc_ajax',
    'page arguments' => array(1, 2),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  
  return $items;
}

/**
 * Menu callback for our ajax links.
 */
function mc_ajax($type, $js) {
  switch ($type) {
    case 'login':
      $title = t('Login');
      $form_id = 'user_login';
      $msg = 'You are now logged in.';
      break;
    case 'register':
      $title = t('Register');
      $form_id = 'user_register';
      $msg = 'You are now registered.';
      break;
  }
  if ($js) {
    ctools_include('modal');
    ctools_include('ajax');
    $form_state = array(
      'ajax' => TRUE,
      'title' => $title,
    );
    $output = ctools_modal_form_wrapper($form_id, $form_state);
    if (empty($output)) {
      $output[] = ctools_modal_command_display('Welcome', $msg);
    }
    ctools_ajax_render($output);
  }
  else {
    return drupal_get_form($form_id);
  }
}

The problem I'm having is that I would like to be able to refresh the page after a successful login or registration, but I couldn't find a way to send a command through ctools_ajax_render() that would do this. Is there cause for a new command within ajax_responder.js:Drupal.CTools.AJAX.commands or is there an existing way to do this?

Thank you again for this library! It has banished my recent stagnation and got my gears in motion again.

Comments

merlinofchaos’s picture

I think you're right in that you'll need to add a custom command. Note that in your own .js file you can add commands to the commands array quite easily, so you can set this up without too much trouble. If it's a broadly useful enough command I can possibly add it to CTools so everyone can use it.

sirkitree’s picture

Assigned: Unassigned » sirkitree
Category: support » feature

Great, I'll work on it as a feature request then. Thank you for the feedback.

sirkitree’s picture

StatusFileSize
new539 bytes

Uh, yah, so that was pretty gosh darn simple. Patch attached. This allows you to do the following:

using the code example above:

...
function mc_ajax($type, $js) {
  switch ($type) {
    case 'login':
      $title = t('Login');
      $form_id = 'user_login';
      $msg = 'You are now logged in.';
      break;
    case 'register':
      $title = t('Register');
      $form_id = 'user_register';
      $msg = 'You are now registered.';
      break;
  }
  if ($js) {
    ctools_include('modal');
    ctools_include('ajax');
    $form_state = array(
      'ajax' => TRUE,
      'title' => $title,
    );
    $commands = array();
    $commands = ctools_modal_form_wrapper($form_id, $form_state);
    if (empty($commands)) {
      $commands[] = array('command' => 'reload');
    }
    ctools_ajax_render($commands);
  }
  else {
    return drupal_get_form($form_id);
  }
}

The important part being within the if (empty(....

Should I write an actual php function within ajax.inc that is similar to ctools_ajax_command_restripe()?

sirkitree’s picture

Status: Active » Needs review

marking for review.

zroger’s picture

Wow, this is exactly what i was working on today. I'd vote yes for a convenience function like ctools_ajax_command_reload().

merlinofchaos’s picture

Yeah, any new commands should come with the appropriate convenience function.

sirkitree’s picture

StatusFileSize
new1.16 KB

How does this one look? It adds ctools_ajax_command_reload()

sirkitree’s picture

StatusFileSize
new1.44 KB

Here's an updated patch with the reload added to the main comment section where it lists built in commands.

zroger’s picture

Status: Needs review » Reviewed & tested by the community

Looks great. Applies cleanly and works as expected.

merlinofchaos’s picture

Status: Reviewed & tested by the community » Fixed

Committed. Thanks!

Status: Fixed » Closed (fixed)

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

travist’s picture

Here is another way to do this that I find more elegant...

  • 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.

firdous.ali86’s picture

Version: 6.x-1.0 » 7.x-1.0-rc1

UPDATE!

- All calls to ctools_ajax_render() should be replaced with calls to core
ajax_render(). Note that ctools_ajax_render() printed the json object and
exited, ajax_render() gives you this responsibility.

ctools_ajax_render()

becomes

print ajax_render();
exit;