Ajax loading in Drupal using Ctools breaks
I'm working on a custom module using CTools ajax and form. I tried to combine this example with the one from here. It goes like this: a user chooses from one of the radio buttons, on submit the map of US+Canada is going to load with links to another page with the maps. Depending on their choice, the map of the selected country is loaded. The last step is where I get ajax loading errors. No js works fine.
My code looks like this:
<?php
function eval_interface_menu() {
$items = array();
$items['eval'] = array(
'title' => 'Find your Sales Engineer',
'page callback' => 'eval_interface_form_page',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['eval/regions_map/%ctools_js/%'] = array(
'title' => 'Find your Sales Engineer',
'page callback' => 'eval_interface_ajax_regions',
'page_arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function eval_interface_form_page(){
eval_interface_redirect();
// Check if this request was sent via javascript so we can degrade.
$js = !empty($_REQUEST['js']);
$form_state = array(
'ajax' => $js,
'no_redirect' => TRUE,
'rerender' => TRUE,
);
ctools_include('form');
drupal_add_js('misc/jquery.form.js');
ctools_add_js('ajax-responder');
$output = ctools_build_form('eval_interface_map_form', $form_state);
if (!empty($form_state['executed']) && $js) {
ctools_include('ajax');
$commands = array();
$content = $form_state['countries_map'];
// Remove any pre-existing status messages, from a validation failure:
$commands[] = ctools_ajax_command_remove('.messages');
// Render the map to a specified element
$commands[] = ctools_ajax_command_html('#imagemap', $content);
ctools_ajax_render($commands);
}
if ($js) {
// The form is only submitted via .js. This means there was a validation
// and we have to rerender the form. This will go the extra mile:
ctools_include('ajax');
$commands = array();
// If there are messages for the form, render them.
if ($messages = theme('status_messages')) {
$output = $messages . $output;
}
// Remove any pre-existing status messages, from a validation failure:
$commands[] = ctools_ajax_command_remove('.messages');
// And replace the form.
$commands[] = ctools_ajax_command_replace('#eval-interface-map-form', $output);
ctools_ajax_render($commands);
} else {
// if js disabled
if (!empty($form_state['countries_map'])) {
$output .= $form_state['countries_map'];
}
}
return $output;
}
function eval_interface_map_form($form_state) {
$form = array();
$form['number_of_users'] = array(
'#type' => 'radios',
'#title' => t('Choose the number of user开发者_JAVA技巧s'),
'#required' => TRUE,
'#options' => array(
'smb1' => t('1-500 users'),
'smb2' => t('500 - 1500 users'),
'smb3' => t('1501+ users')),
);
// ajaxify submit button
$form['submit'] = array(
'#type' => 'submit',
'#attributes' => array('class' => 'ctools-use-ajax'),
'#value' => t('Submit'),
);
// Put a place to put the result so we don't just erase the form.
$form['result'] = array(
'#value' => '<div id="imagemap" class="clear-block"> </div>',
);
return $form;
}
function eval_interface_map_form_submit($form, &$form_state) {
$countries_map = theme('countries_map', $form_state['values']['number_of_users']);
$form_state['countries_map'] = $countries_map;
if (!$form_state['ajax']) {
$form_state['executed'] = TRUE;
$js = FALSE;
}
}
function eval_interface_ajax_regions($js = FALSE) {
$output = theme('regions_map');
if ($js) {
ctools_include('ajax');
$commands = array();
$commands[] = ctools_ajax_command_replace('#countries-map', $output);
ctools_ajax_render($commands);
// above command will exit().
}
else {
return $output;
}
}
function eval_interface_theme(){
$path = drupal_get_path('module', 'eval_interface');
return array(
'countries_map' => array(
'arguments' => array('number_of_users' => NULL),
'template' => 'countries-map',
'path' => "$path/theme",
),
'regions_map' => array(
'arguments' => array(),
'template' => "regions-map",
'path' => "$path/theme",
)
);
}
function eval_interface_preprocess_regions_map(&$vars) {
$vars['country'] = arg(3);
}
?>
countries-map.tpl.php
<h3>Choose your country</h3>
<div id="countries-map">
<?php
// to do something with users
print $number_of_users; ?>
<a href="eval/regions_map/nojs/ca" class="ctools-use-ajax">Canada Map</a>
<a href="eval/regions_map/nojs/us" class="ctools-use-ajax">US MAP</a>
</div>
regions-map.tpl.php
<h3>Choose Your Region</h3>
<div id="regions-map">
<?php
if ($country == 'ca') {
print t('Canada map');
} elseif ($country == 'us') {
print t('Map of US');
}
?>
</div>
Should have been 'page arguments'. It's always the small things.
精彩评论