1

我正在使用 CTools ajax 和表单开发自定义模块。我尝试将此示例此处的示例结合起来。它是这样的:用户从其中一个单选按钮中进行选择,提交时,美国+加拿大的地图将加载到带有地图的另一个页面的链接。根据他们的选择,加载所选国家的地图。最后一步是我得到 ajax 加载错误的地方。没有js可以正常工作。

我的代码如下所示:

<?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 users'),
        '#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">&nbsp;</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);  
}
?>

国家-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>

地区-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>
4

1 回答 1

2

应该是“页面参数”。总是小事。

于 2011-03-21T17:20:42.537 回答