3

I am displaying a Drupal Commerce node with AJAX add to cart functionality in a popup, with the Colorbox Node module. In my custom form callback, I have the following code.

// Form callback function
function ajax_module_form($form, &$form_state){
  drupal_add_js(drupal_get_path('module','colorbox' . '/colorbox.js'));
  $commands = array();

  $commands[] = ajax_command_invoke('colorbox-node', 'colorbox.close');

  return array('#type' => 'ajax', '#commands' => $commands);
}

I am trying to use Drupal's ajax_command_invoke to close the Colorbox window after AJAX form submission is complete, but cant seem get it to work. I tried 'parent.colorbox.close' as well but doesnt seem to work. The colorbox link has iframe and sample link is -

example.com/node/45434345?width=500&height=500&iframe=true and link class is .colorbox-node

Also the Colorbox module setting - invoke on all pages is also active.

Any lead/help is greatly appreciated.

1

I'm editing my answer to directly address your question first:

ajax_command_invoke takes a css selector as its first argument.

 node-colorbox

isn't a css selector (or at the very least, not a valid one).

In addition, ajax_command_invoke does not allow for the execution of arbitrary javascript, but only allows for triggering registered jQuery events. If you want to create a custom jQuery event for colorbox, you will have to manually create it and attach it to the colorbox object, using Drupal.behaviors.

http://www.sitepoint.com/jquery-custom-events/ should get your started.

Colorbox seems like it might be a good solution for your use-case, but it really isn't. If you want a good floating window with ajax forms and automatic closure, using the chaos tools modal system is a better bet. It supports what you're looking for right out of the box and makes working with forms easy. In addition, almost everyone uses ctools, as it is a dependency for many different modules.

http://deeson-online.co.uk/labs/insert-form-pop-modal-ctools-and-drupal-7 should get you started.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.