0

I am experiencing a rather odd problem in Drupal 7.

I have a form in Ctools modal. It should contain 2 dependent select boxes (among other fields). I've done this dozen times on regular forms, but i can't make it work in the Ctools Modal form. Modal pops up, all the fields are there, all the counties are in the first select box, but it looks like it's not even reaching the ajax callback when a county is selected, so no dealer are returned. When I hard code the county id, dealers from that county show as expected.

My code is similar to the following:

menu item:

$items['modal-data/%ctools_js'] = array(
  'page callback' => 'core_modal_form',
  'page arguments' => array(1),
  'access arguments' => array('access content'),
  'delivery callback' => 'ajax_deliver',
  'type' => MENU_CALLBACK,

);

general modal handling:

function ni_core_modal_form($js) {

// Check whether js is enabled.
if ($js) {

  // Include ctools modal plugin.
  ctools_include('ajax');
  ctools_include('modal');

  // Create notify entity
  $form_state = array(
    'title' => 'Contact dealer',
    'ajax' => TRUE,
  );

  $commands = ctools_modal_form_wrapper('contact_dealer_modal_form', $form_state);

// If form was submitted.
  if (!empty($form_state['executed'])) {

    $commands[] = ajax_command_replace('#modal-content', theme_status_messages(['display' => 'status']));

  }

  print ajax_render($commands);
  drupal_exit();
 }
}

The form in ctools modal:

function contact_dealer_modal_form($form, &$form_state) {

   // get all counties
  $counties = get_dealer_counties_list();

  // Check for the selected county
  $county = isset($form_state['values']['county']) ? $form_state['values']['county'] : key($counties);

  // get dealer for selected county
  $dealers = get_dealer_select_list($county);

  $form['wrapper'] = [
    '#type' => 'container',
    '#attributes' => ['class' => ['contact-dealer-form-wrapper']],
  ];

  $form['wrapper']['county'] = [
    '#type' => 'select',
    '#title' => t('Select your county'),
    '#options' => $counties,
    '#ajax' => array(
      'callback' => '_county_dealer_callback',
      'wrapper' => 'dealer-replace',
    ),
  ];

  $form['wrapper']['dealer'] = [
    '#type' => 'select',
    '#title' => t('Select your dealer'),
    '#options' => $dealers,
    '#default_value' => 0,
    '#prefix' => '<div id="dealer-replace">',
    '#suffix' => '</div>',
  ];

  $form['submit'] = [
    '#type' => 'submit',
    '#attributes' => ['class' => ['btn btn-primary'] ],
    '#value' => t('Contact'),
  ];

  return $form;
}

ajax callback:

function _county_dealer_callback($form, &$form_state) {

  return $form['wrapper']['dealer'];
}

I will highly appreciate any suggestion!

0

Well, as it turns out, the problem was that the form definition was in an .inc file and Ctools couldn't find it. The solution was to simply put the form definition, submit handler and ajax callback for select in a .module file.

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.