Skip to content

Instantly share code, notes, and snippets.

@keeprock
Last active August 29, 2015 14:16
Show Gist options
  • Save keeprock/6b8831eac71b957639d5 to your computer and use it in GitHub Desktop.
Save keeprock/6b8831eac71b957639d5 to your computer and use it in GitHub Desktop.
Loading a form using Ctools AJAX API
function custom_menu() {
$items['custom_module/some_title_describing_action/%ctools_js'] = array(
'title' => 'Subscribe',
'page callback' => 'custom_form__callback',
'page arguments' => array(2), // index starts with zero
'type' => MENU_CALLBACK,
'access callback' => TRUE
);
return $items;
}
function custom_form__callback($js = FALSE) {
ctools_include('modal'); // Mandatory
ctools_include('ajax'); // Mandatory
if (!js) {
return drupal_get_form('FORM_ID', $form_state);
}
$form_state = array(
'title' => t('TITLE'),
'ajax' => TRUE
);
$output = ctools_modal_form_wrapper('ID_OF_WEBFORM-FUNCTION_RETURNING_A_WEBFORM', $form_state);
if (!empty($form_state['executed'])) { // If form is submitted
$output = array();
$output[] = ajax_command_html('JQUERY_SELECTOR', t('SOME_TITLE'));
/*Here goes, basically, any Drupal.AJAX commands that operates the content on a page*/
}
print ajax_render($output);
exit();
}
// Hook into submitting form process
function FORM_ID_submit(&$form, &$form_state) {
if ($form_state['ajax'] == TRUE) {
// We are using AJAX powered form
drupal_set_message(t('LOOKS COOL, DUDE!'), 'status', FALSE);
} else {
/* We got a bareboned form without JS. Basically, JS is turned off or
form opened as a standalone node */
drupal_goto('/');
// Do whateva
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment