Passing a client value back to Drupal
It probably seems that I am using this list as my first line of defense, since it's been fairly inactive between my posts! But honestly, I have spent hours and hours and hours in issue queue, IRC (including ##javascript #jquery), Google, source code, handbook pages... I feel like the guy amongst the trees looking for a forest. Part of this is documented at http://drupal.org/node/1106358, for background...the last 2 or 3 responses being most applicable. Basically, I am doing a hook_form_alter and adding a #suffix to the node edit form body to provide a ctools link beneath the body box that results in a modal form being loaded during the creation of a node, if the link is clicked. This works fine, including putting a form in the modal window inside a table (yes, tabular data). The issue is that the contents of the body text area at that point are needed by my module in order to send back the proper content in the modal window, and I can't seem to get that information back to Drupal. I do -have- the data...I get it via js from the CKEDITOR object with a getdata() call. I suspect there is some way to then give it to ctools to pass back with its ajax call, but cannot find an example. What I've tried, in lieu of that, is to add it onto the url that the link is calling. I discovered that even though ctools is using a click handler to process the link, "onclick" still works, executes first, and does not interfere with or prevent ctools from performing its stuff once finished. So, I changed the link to have an onclick that simply puts the body text into this.sort, making the url be appended by ?body=xxxx I can't seem to discover if that's viable, and if so, what to do at that point, as it doesn't appear to be in $_GET, $_POST, or available by changing the ctools callback page arguments from array(1) to array(1,2). Jeff -- /I am a non sequitur. Beware, the contents were packaged where peanuts are processed./ Ayen Designs 388 Bullsboro Drive #105 · Newnan, Georgia 30263 404-271-9734 Web:ayendesigns.com <http://ayendesigns.com/> Blog: theAccidentalCoder.com <http://theaccidentalcoder.com/> Drupal: j. ayen green <http://drupal.org/user/367108> (367108) IRQ: j_ayen_green IM (Yahoo) baalwww (MSN) baalwww@yahoo.com Skype: ayendesigns | Facebook: ayendesigns | Twitter: @ayendesigns Ayen Designs is the computer services division of
I don't use CTools, but how I normally do this is define in the module a hook_menu item that is purely callback (i.e. 'type' => MENU_CALLBACK) and use that. Tabular data I push using a jQuery $.ajax call and just serialize() the whole data HTML element. To pass something back in the callback function (in module) just encode it using json_encode and in jQuery $.ajax call have a success handler that does something with it. Malks. On 6:59 AM, jeff@ayendesigns.com wrote:
It probably seems that I am using this list as my first line of defense, since it's been fairly inactive between my posts! But honestly, I have spent hours and hours and hours in issue queue, IRC (including ##javascript #jquery), Google, source code, handbook pages... I feel like the guy amongst the trees looking for a forest.
Part of this is documented at http://drupal.org/node/1106358, for background...the last 2 or 3 responses being most applicable. Basically, I am doing a hook_form_alter and adding a #suffix to the node edit form body to provide a ctools link beneath the body box that results in a modal form being loaded during the creation of a node, if the link is clicked. This works fine, including putting a form in the modal window inside a table (yes, tabular data).
The issue is that the contents of the body text area at that point are needed by my module in order to send back the proper content in the modal window, and I can't seem to get that information back to Drupal. I do -have- the data...I get it via js from the CKEDITOR object with a getdata() call. I suspect there is some way to then give it to ctools to pass back with its ajax call, but cannot find an example. What I've tried, in lieu of that, is to add it onto the url that the link is calling. I discovered that even though ctools is using a click handler to process the link, "onclick" still works, executes first, and does not interfere with or prevent ctools from performing its stuff once finished. So, I changed the link to have an onclick that simply puts the body text into this.sort, making the url be appended by ?body=xxxx I can't seem to discover if that's viable, and if so, what to do at that point, as it doesn't appear to be in $_GET, $_POST, or available by changing the ctools callback page arguments from array(1) to array(1,2).
Jeff -- //
That's the clean way to do it, and I would if I were in control of the handshaking, but because Ctools is doing the handling of the link click and the construction of the AJAX handshaking, I can't just package it up and send it...I need to find the right 'box and forms' to get it to accept, carry and deliver my package for me. On 03/29/2011 07:49 PM, Anthony Malkoun wrote:
I don't use CTools, but how I normally do this is define in the module a hook_menu item that is purely callback (i.e. 'type' => MENU_CALLBACK) and use that. Tabular data I push using a jQuery $.ajax call and just serialize() the whole data HTML element. To pass something back in the callback function (in module) just encode it using json_encode and in jQuery $.ajax call have a success handler that does something with it.
Malks.
On 3/29/2011 5:12 PM, jeff@ayendesigns.com wrote:
That's the clean way to do it, and I would if I were in control of the handshaking, but because Ctools is doing the handling of the link click and the construction of the AJAX handshaking, I can't just package it up and send it...I need to find the right 'box and forms' to get it to accept, carry and deliver my package for me.
This is how CTools binds to links, normally: (There's a couple of other methods as well, but they're all similar). $('a.ctools-use-ajax:not(.ctools-use-ajax-processed)', context) .addClass('ctools-use-ajax-processed') .click(Drupal.CTools.AJAX.clickAJAXLink); If you need special handling of the link, what you probably need to do is bind yourself, and then use your own version of clickAJAXLink that gets your data and does what it needs. That function isn't very long: /** * Generic replacement click handler to open the modal with the destination * specified by the href of the link. */ Drupal.CTools.AJAX.clickAJAXLink = function() { if ($(this).hasClass('ctools-ajaxing')) { return false; } var url = $(this).attr('href'); var object = $(this); $(this).addClass('ctools-ajaxing'); try { url = url.replace(/\/nojs(\/|$)/g, '/ajax$1'); $.ajax({ type: "POST", url: url, data: { 'js': 1, 'ctools_ajax': 1}, global: true, success: Drupal.CTools.AJAX.respond, error: function(xhr) { Drupal.CTools.AJAX.handleErrors(xhr, url); }, complete: function() { $('.ctools-ajaxing').removeClass('ctools-ajaxing'); }, dataType: 'json' }); } catch (err) { alert("An error occurred while attempting to process " + url); $('.ctools-ajaxing').removeClass('ctools-ajaxing'); return false; } return false; }; You can either modify the href and then call through to it, or just replace it as you need.
Getting warm. Since I'm replacing the click handler, I decided not to mess with url at all and to just add another data pair to the data being passed, and treat it as 3 parms in the module. I note that both 'modal' and 'ajax' are being loaded, and that the binding is actually do modal (from what I see in firebug). Looking at the code, modal link click is just a small lead-in to ajax link click, so I think I need to unbind the modal click handler and bind my own, having it call my own ajax link click function. The unbind works. The bind does not. I start the bind with an alert, and when I actually click the link, it attempts to make a page change (no ajax bind, and no alert). Here is what I have: /** * Unbind the CTools click event handler then add our own */ $(document).ready(function() { $('a.ctools-use-modal').unbind('click'); $('a.ctools-use-modal-processed').unbind('click'); $('a.ctools-use-modal:not(.ctools-use-modal-processed-My)', context) .addClass('ctools-use-modal-processed-My') .click(Drupal.CTools.Modal.clickAjaxMyLink); }); Drupal.CTools.Modal.clickAjaxMyLink = function () { // show the empty dialog right away. Drupal.CTools.Modal.show(Drupal.CTools.Modal.getSettings(this)); Drupal.CTools.AJAX.clickAJAXMyLink.apply(this); if (!$(this).hasClass('ctools-ajaxing')) { Drupal.CTools.Modal.dismiss(); } return false; }; /** * Generic replacement click handler to open the modal with the destination * specified by the href of the link. */ Drupal.CTools.AJAX.clickAJAXMyLink = function() { alert('made it'); if ($(this).hasClass('ctools-ajaxing')) { return false; } var url = $(this).attr('href'); var object = $(this); $(this).addClass('ctools-ajaxing'); try { url = url.replace(/\/nojs(\/|$)/g, '/ajax$1'); $.ajax({ type: "POST", url: url, data: { 'js': 1, 'ctools_ajax': 1, 'body': CKEDITOR.instances['edit-body'].getData()}, global: true, success: Drupal.CTools.AJAX.respond, error: function(xhr) { Drupal.CTools.AJAX.handleErrors(xhr, url); }, complete: function() { $('.ctools-ajaxing').removeClass('ctools-ajaxing'); }, dataType: 'json' }); } catch (err) { alert("An error occurred while attempting to process " + url); $('.ctools-ajaxing').removeClass('ctools-ajaxing'); return false; } return false; }; On 03/30/2011 01:43 AM, Earl Miles wrote:
This is how CTools binds to links, normally: (There's a couple of other methods as well, but they're all similar).
$('a.ctools-use-ajax:not(.ctools-use-ajax-processed)', context) .addClass('ctools-use-ajax-processed') .click(Drupal.CTools.AJAX.clickAJAXLink);
If you need special handling of the link, what you probably need to do is bind yourself, and then use your own version of clickAJAXLink that gets your data and does what it needs.
You can either modify the href and then call through to it, or just replace it as you need.
participants (3)
-
Anthony Malkoun -
Earl Miles -
jeff@ayendesigns.com