3

What's the sanest way to present a Ctools modal to the user while bypassing the normal AJAX workflow? For example, how could I show a completely JavaScript-generated "Are you sure you want to log out?" confirmation prompt to the user in a Ctools modal after the user clicks the "log out" link?

I know I can probably fake a box which would get the same styling as the Ctools modal, but I'd rather avoid silly hacks if not strictly necessary.

8

This should be possible by invoking Drupal.CTools.Modal.show directly. The function takes one argument (the modal-style), which either can be a string pointing to an object on Drupal.settings or the style-object itself.

After showing the dialog, it is possible to populate title and content using jQuery:

Drupal.CTools.Modal.show('my-modal-style');
$('#modal-title').html('my-title');
$('#modal-content').html('some markup here').scrollTop(0);
Drupal.attachBehaviors();

In your PHP code, you need to ensure that ctools modal.js and also misc/ajax.js gets loaded:

drupal_add_js('misc/ajax.js');

ctools_add_js('modal');
ctools_add_css('modal');

// Add your modal style to the settings. You find the defaults in modal.js near the top:
drupal_add_js(array(
  'my-modal-style' => array(
    'modalSize' => array(
      'type' => 'fixed',
      'width' => 250,
      'height' => 250,
    ),
    'closeText' => 'Dismiss',
    'closeImage' => ...
  ),
), 'setting');
| improve this answer | |
  • Turns out I'm not having a chance to implement this after all, but it'll surely be useful if I run into this problem in the future. Thanks! – Garrett Albright Oct 30 '13 at 18:06

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.