diff --git a/ctools.module b/ctools.module index f014e74..ded501d 100644 --- a/ctools.module +++ b/ctools.module @@ -903,3 +903,19 @@ function ctools_ctools_entity_context_alter(&$plugin, &$entity, $plugin_id) { } } } + +/** + * Implements hook_page_delivery_callback_alter(). + * + * This allows for both nojs & ajax callbacks to be done from the same link. + */ +function ctools_page_delivery_callback_alter(&$callback) { + // jQuery sets a HTTP_X_REQUESTED_WITH header of 'XMLHttpRequest'. + // If a page would normally be delivered as an html page, and it is called + // from jQuery, deliver it instead as an Ajax response. + // We also check for ctools-use-modal to make sure only alter our request. + if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' && $callback == 'drupal_deliver_html_page' + && isset($_POST['ctools-use-modal']) && $_POST['ctools-use-modal'] == 'true') { + $callback = 'ajax_deliver'; + } +} diff --git a/ctools_ajax_sample/ctools_ajax_sample.module b/ctools_ajax_sample/ctools_ajax_sample.module index 2a30c2a..289f477 100644 --- a/ctools_ajax_sample/ctools_ajax_sample.module +++ b/ctools_ajax_sample/ctools_ajax_sample.module @@ -225,8 +225,10 @@ function ctools_ajax_sample_hello($js = NULL) { ctools_include('ajax'); $commands = array(); $commands[] = ajax_command_html('#ctools-sample', $output); - print ajax_render($commands); // this function exits. - exit; + return array( + '#type' => 'ajax', + '#commands' => $commands + ); } else { return $output; @@ -247,8 +249,11 @@ function ctools_ajax_sample_tablenix($js, $row) { $commands = array(); $commands[] = ajax_command_remove("tr.ajax-sample-row-$row"); $commands[] = ajax_command_restripe("table.ajax-sample-table"); - print ajax_render($commands); - exit; + + return array( + '#type' => 'ajax', + '#commands' => $commands + ); } /** @@ -266,16 +271,19 @@ function ctools_ajax_sample_login($js = NULL) { 'title' => t('Login'), 'ajax' => TRUE, ); - $output = ctools_modal_form_wrapper('user_login', $form_state); + $commands = ctools_modal_form_wrapper('user_login', $form_state); if (!empty($form_state['executed'])) { // We'll just overwrite the form output if it was successful. - $output = array(); + $commands = array(); $inplace = ctools_ajax_text_button(t('remain here'), 'ctools_ajax_sample/nojs/login/inplace', t('Go to your account')); $account = ctools_ajax_text_button(t('your account'), 'ctools_ajax_sample/nojs/login/user', t('Go to your account')); - $output[] = ctools_modal_command_display(t('Login Success'), ''); + $commands[] = ctools_modal_command_display(t('Login Success'), ''); } - print ajax_render($output); - exit; + + return array( + '#type' => 'ajax', + '#commands' => $commands + ); } /** @@ -298,8 +306,11 @@ function ctools_ajax_sample_login_success($js, $action) { // bounce bounce $commands[] = ctools_ajax_command_redirect('user'); } - print ajax_render($commands); - exit; + + return array( + '#type' => 'ajax', + '#commands' => $commands + ); } /** @@ -413,8 +424,11 @@ function ctools_ajax_sample_animal($js = NULL, $step = NULL) { else { $commands = ctools_modal_form_render($form_state, $output); } - print ajax_render($commands); - exit; + + return array( + '#type' => 'ajax', + '#commands' => $commands + ); } else { if ($output === FALSE || !empty($form_state['complete'])) { diff --git a/js/modal.js b/js/modal.js index 831649f..a2423eb 100644 --- a/js/modal.js +++ b/js/modal.js @@ -207,6 +207,7 @@ element_settings.url = $this.attr('href'); element_settings.event = 'click'; element_settings.progress = { type: 'throbber' }; + element_settings.submit = { 'js': true, 'ctools-use-modal': true }; } var base = $this.attr('href'); Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); @@ -223,6 +224,7 @@ // normal form action. element_settings.url = Drupal.CTools.Modal.findURL(this); element_settings.event = 'click'; + element_settings.submit = { 'js': true, 'ctools-use-modal': true }; var base = $this.attr('id'); Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); @@ -241,6 +243,7 @@ element_settings.url = $this.attr('action'); element_settings.event = 'submit'; element_settings.progress = { 'type': 'throbber' } + element_settings.submit = { 'js': true, 'ctools-use-modal': true }; var base = $this.attr('id'); Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);