Hi,
I was running into a problem yesterday when a client asked me to create a button who will send the result of a form to Drupal on server A, and then, with the result of that, it needs to be sent to server B.
I've been asked why the server A cannot communicate directly with the server B and for some reason, it's not possible.
I've created a small patch of 10 lines max for CTools to allows people to create callbacks.
Another solution would have been to create two recursive $.post(), but I wanted to see if I could do it with CTools before.
How does it works ?
First, you need to create a link, having the 'ctools-use-ajax' class.
Then, add the js corresponding:
ctools_add_js('ajax-responder');
Now let's see the callback code of that menu entry:
function ctoolscallback_callback($js = FALSE) {
if ($js) {
ctools_include('ajax');
$commands[] = ctools_ajax_command_callback('mycallback', time());
ctools_ajax_render($commands);
}
}
If you run this, nothing will happend because your callback 'mycallback' is not defined.
To do this, you have to define it in a JS file like this:
Drupal.CTools.AJAX.commands.callback.mycallback = function(data) {
alert("This is my custom callback: " + data.data);
}This will return a popup box with the text and the value of the function 'time()' previously runned in PHP.
Patch for CTools 6.x-1.x is following.
I've created a small module to test my patch, here it is:
function ctools_callback_menu() {
$items = array();
$items['ctoolscallback/%ctools_js/go'] = array(
'page callback' => 'ctoolscallback_callback',
'page arguments' => array(1),
'access arguments' => array('access content')
);
return $items;
}
function ctools_callback_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case "list":
$block = array();
$block[0]['info'] = t('Ctools Callback');
break;
case "view":
$block['subject'] = t('Ctools Callback');
$block['content'] = _get_block_content();
break;
}
return $block;
}
function _get_block_content() {
$output = l('Ctools callback', 'ctoolscallback/nojs/go', array('attributes' => array('class'=>'ctools-use-ajax')));
ctools_add_js('ajax-responder');
return $output;
}
function ctoolscallback_callback($js = FALSE) {
if ($js) {
ctools_include('ajax');
$commands = array();
$commands[] = ctools_ajax_command_callback('moncallback1', time());
$commands[] = ctools_ajax_command_callback('moncallback2', time());
$commands[] = ctools_ajax_command_callback('moncallback3', time());
ctools_ajax_render($commands);
}
}
Don't forget to create those JS functions in a JS file:
Drupal.CTools.AJAX.commands.callback.moncallback1 = function(data) {
alert("Message1: " + data.data);
}
Drupal.CTools.AJAX.commands.callback.moncallback2 = function(data) {
console.log("Message2: " + data.data);
}
Drupal.CTools.AJAX.commands.callback.moncallback3 = function(data) {
alert("Message3: " + data.data);
}
| Comment | File | Size | Author |
|---|---|---|---|
| #1 | 0001-1547192-Run-custom-callback-with-ctools.patch | 2.06 KB | pol |
Comments
Comment #1
polComment #1.0
polAdding code.
Comment #2
merlinofchaos commentedI don't understand the point. Why have this extra layer of callback business? Just create custom commands that do what you want?
Comment #3
merlinofchaos commentedI guess I should clarify.
What you're showing appears, to me, to be exactly what CTools already does, only you're wrapping it in a later that already does this. You're adding an extra layer of abstraction that doesn't, to me, make a lot of sense, unless there's something I"m missing.
Comment #4
polYes that's it, custom commands that do anything with data replied from the server.
In my case, the server A needs to produce a XML and the client needs to send back the XML produced to another server B.
Comment #5
merlinofchaos commentedRe #4: What I'm saying is that you can already do this...without patching CTools.
Comment #6
polClosing this as this is already possible to do in CTools.
We need to define our own function, then, create the JS accordingly.
Thanks for the fast reaction Merlin...
Comment #7
merlinofchaos commentedAs an addendum, modal.inc and modal.js have an example of this.
Comment #8
FooZee commentedJust in case someone hit this page later on :)
modal.inc and modal.js are great examples of how to add your own ajax_commands .. however, I needed a little more-detailed tutorial on how to do this :) https://isovera.com/blog/create-custom-commands-drupal-7-ajax-framework was a great help
basically what you need to do is
Comment #8.0
FooZee commentedAdding more details.