From be42147a9a44849797d93862de70c12e16635d90 Mon Sep 17 00:00:00 2001 From: Sam Bonner Date: Thu, 13 Feb 2014 15:37:04 +1300 Subject: [PATCH] #1710710 rerolled patch from #8 to work on latest stable release --- ctools.module | 16 +++++++++++ ctools_ajax_sample/ctools_ajax_sample.module | 40 +++++++++++++++++--------- includes/context-access-admin.inc | 11 +++++-- includes/context-admin.inc | 16 ++++++++--- includes/modal.inc | 5 +++- js/modal.js | 3 ++ 6 files changed, 71 insertions(+), 20 deletions(-) diff --git a/ctools.module b/ctools.module index b5ab626..d8b32b1 100644 --- a/ctools.module +++ b/ctools.module @@ -956,3 +956,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. + 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 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/includes/context-access-admin.inc b/includes/context-access-admin.inc index 76643cf..35b8c82 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 c774bf4..e2021ff 100644 --- a/includes/context-admin.inc +++ b/includes/context-admin.inc @@ -486,8 +486,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 + ); } /** @@ -619,8 +622,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 + ); } /** @@ -782,6 +788,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 dfde0ac..5538777 100644 --- a/includes/modal.inc +++ b/includes/modal.inc @@ -243,5 +243,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 33f071f..e02c594 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); -- 1.7.9.5