I already had success in ajax loading a form into a modal window with Ctools but now it fails and I do not why. Maybe someone can help me?! The problem: I create a link on a page which should open a form in a modal window. With another (Views created) page it did work fine: I click on the link and the form is correctly rendered in a modal window. In the following case it seems to be all the same - the only difference is that another form is used and the ajax links are not created by a Views page but a custom page callback. But when I click on the ajax link the modal window is opened - but the throbber keeps rotating and nothing happens. No form is shown in the window - just the throbber graphic.

As I can see when hacking into functions like ajax_render() that the output is correctly built into an array usable by ajax_render()... there are no error message anywhere, neither in Drupal database logs, reports or server error logs. Additionally the browser gives no JavaScript error or warnings.

This is the menu_hook defining the page callback for the ajax link:

 $items['foodlog/existing/%ctools_js/%foodlogentry/%'] = array(
    'title' => 'Edit your foodlog entry',
    'page callback' => 'foodlog_edit_form_ajax',
    'page arguments' => array(2, 3, 4),
    'access callback' =>  'foodlog_edit_form_access',
    'access arguments' => array(3),
    'type' => MENU_CALLBACK  

This is the ajax page callback:

function foodlog_edit_form_ajax($js = NULL, $foodlogentry, $action) {

  if ($js) {
    $form_state = array(
      'ajax' => TRUE,
    $form_state['build_info']['args'][] = $foodlogentry;
    $form_state['build_info']['args'][] = $action;

    $output = ctools_modal_form_wrapper('foodlog_edit_form', $form_state);    

    if (!empty($form_state['ajax_commands'])) {
      $output = $form_state['ajax_commands'];

    // Return the ajax instructions to the browser via ajax_render().
    print ajax_render($output);

  // go on: if no $js -> render the form
  else {    
    return drupal_get_form('foodlog_edit_form', $foodlogentry, $action);

And here is the code where the ajax link is rendered (just the part of the page callback, where it happens):


$actionlinks = l(t('edit'), 'foodlog/existing/nojs/'. $entry['flid'] .'/edit', array('attributes' => array('class' => array('ctools-use-modal', 'ctools-modal-foodlog-modal-style') ) ) );

The result is the correct link like

<a href="/foodlog/existing/nojs/9813/edit" class="ctools-use-modal ctools-modal-foodlog-modal-style ctools-use-modal-processed">Bearbeiten</a>

The JavaScript files are loaded, no caching is enabled, no aggregation. As I mentioned the same is working on another Views page where I create such a link by rewriting a views field. Just the form is another one. The form is rendered when I simply call the URL directly in the browser - there seems to be some trouble somewhere when Drupal sends all the stuff to the browser.

Does anyone has an idea?

  • I'm only idea is unfortunately not very helpful to your issue directly. But I see all that (good) custom code and just think this would have been a ton easier: drupal.org/project/ctools_automodal – tenken Jul 11 '14 at 15:16

I found the error: on my page I use the jQuery Flot plugin in order to create some charts. For one of the charts there was an error on the page where I wanted to open a form in a modal window. When I solved this error the modal window was working as expected... JavaScript is a weird thing!

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.