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.
| Comment | File | Size | Author |
|---|---|---|---|
| #8 | ctools.ajax-responder.js_.patch | 1.44 KB | sirkitree |
| #7 | ajax_reload.patch | 1.16 KB | sirkitree |
| #3 | ctools.ajax-responder.js_.patch | 539 bytes | sirkitree |
Comments
Comment #1
merlinofchaos commentedI 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.
Comment #2
sirkitree commentedGreat, I'll work on it as a feature request then. Thank you for the feedback.
Comment #3
sirkitree commentedUh, yah, so that was pretty gosh darn simple. Patch attached. This allows you to do the following:
using the code example above:
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()?Comment #4
sirkitree commentedmarking for review.
Comment #5
zroger commentedWow, this is exactly what i was working on today. I'd vote yes for a convenience function like
ctools_ajax_command_reload().Comment #6
merlinofchaos commentedYeah, any new commands should come with the appropriate convenience function.
Comment #7
sirkitree commentedHow does this one look? It adds ctools_ajax_command_reload()
Comment #8
sirkitree commentedHere's an updated patch with the reload added to the main comment section where it lists built in commands.
Comment #9
zroger commentedLooks great. Applies cleanly and works as expected.
Comment #10
merlinofchaos commentedCommitted. Thanks!
Comment #12
travist commentedHere is another way to do this that I find more elegant...
That worked beautifully for me... Hope this helps someone in the future.
Comment #13
firdous.ali86 commentedUPDATE!
- 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;