diff --git a/ctools.module b/ctools.module index 6f595683e039d3377af8b5601878e048573459a3..853ad87be502b30bb45eb9731a185fc9b0058c67 100644 --- a/ctools.module +++ b/ctools.module @@ -1065,3 +1065,20 @@ function ctools_flush_field_caches() { // Clear caches of 'Entity field' content type plugin. cache_clear_all('ctools_entity_field_content_type_content_types', 'cache'); } + +/** + * 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. + if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && + $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' && + $callback == 'drupal_deliver_html_page' + ) { + $callback = 'ajax_deliver'; + } +} diff --git a/ctools_ajax_sample/ctools_ajax_sample.module b/ctools_ajax_sample/ctools_ajax_sample.module index 2a30c2a5638f87db7e3bc826b81679e7384e6571..289f4776c00ae6fd32df8df71d977474b41f3cf9 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/includes/context-access-admin.inc b/includes/context-access-admin.inc index 76643cf62c76dc4a92ed5a42a406d977f5291188..35b8c82a17bfeff65e274fb0164c526228ca79c8 100644 --- a/includes/context-access-admin.inc +++ b/includes/context-access-admin.inc @@ -318,7 +318,10 @@ function ctools_access_ajax_add($fragment = NULL, $name = NULL) { $output[] = ctools_modal_command_dismiss(); } - print ajax_render($output); + return array( + '#type' => 'ajax', + '#commands' => $output + ); } /** @@ -385,7 +388,10 @@ function ctools_access_ajax_edit($fragment = NULL, $id = NULL) { $output[] = ctools_modal_command_dismiss(); } - print ajax_render($output); + return array( + '#type' => 'ajax', + '#commands' => $output + ); } /** @@ -482,5 +488,6 @@ function ctools_access_ajax_delete($fragment = NULL, $id = NULL) { $output = array(); $output[] = ajax_command_replace('table#ctools-access-table', $table); + drupal_add_http_header('Content-type', 'application/json; charset=utf-8'); print ajax_render($output); } diff --git a/includes/context-admin.inc b/includes/context-admin.inc index 821a5b32accedfa5ac65159008c099033e11069e..00e6848fd9c895d4e6854f5154c0c6aced0956a8 100644 --- a/includes/context-admin.inc +++ b/includes/context-admin.inc @@ -487,8 +487,11 @@ function ctools_context_ajax_item_add($mechanism = NULL, $type = NULL, $cache_ke else { $output = ctools_modal_form_render($form_state, $output); } - print ajax_render($output); - exit; + + return array( + '#type' => 'ajax', + '#commands' => $output + ); } /** @@ -628,8 +631,11 @@ function ctools_context_ajax_item_edit($mechanism = NULL, $type = NULL, $cache_k else { $output = ctools_modal_form_render($form_state, $output); } - print ajax_render($output); - exit; + + return array( + '#type' => 'ajax', + '#commands' => $output + ); } /** @@ -803,6 +809,8 @@ function ctools_context_ajax_item_delete($mechanism = NULL, $type = NULL, $cache $output = array(); $output[] = ajax_command_replace('#' . $type . '-row-' . $position, ''); $output[] = ajax_command_restripe("#$type-table"); + + drupal_add_http_header('Content-type', 'application/json; charset=utf-8'); print ajax_render($output); exit; } diff --git a/includes/modal.inc b/includes/modal.inc index fc9901594b29e4a08a7b878ae8b3ce0058d4dd5c..ab94018b2770ff4e86eb3000fbfcc1df3605e9e0 100644 --- a/includes/modal.inc +++ b/includes/modal.inc @@ -258,5 +258,8 @@ function ctools_modal_form_render($form_state, $output) { function ctools_modal_render($title, $output) { $commands = array(); $commands[] = ctools_modal_command_display($title, $output); - print ajax_render($commands); + return array( + '#type' => 'ajax', + '#commands' => $commands + ); } diff --git a/js/modal.js b/js/modal.js index a9b760f654054db955f1491d3c8bbbc6e8e6da90..82761f1b014c2dbd869c8df6385a0b0f6e069118 100644 --- a/js/modal.js +++ b/js/modal.js @@ -212,6 +212,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); @@ -232,6 +233,7 @@ } element_settings.event = 'click'; element_settings.setClick = true; + element_settings.submit = { 'js': true, 'ctools-use-modal': true }; var base = $this.attr('id'); Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); @@ -250,6 +252,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);