0

我正在尝试为特定角色类型用户名创建一个带有自动完成文本字段的表单。但它不起作用。虽然当我使用“用户/自动完成”,这是在 drupal.org 示例中给出的默认路径时,它可以工作......

这是 hook_menu ---

function scout_bucks_menu(){
  $items = array();
  $items['scouts/autocomplete'] = array(
    'page callback' => 'scout_bucks_autocomplete',
    'type' => MENU_CALLBACK,
    'access arguments' => array('administer scout bucks'),
  ); 
  return $items;
}

表单的这个 id --

function scoutbucks_add_bucks_form(&$form_state){
  $form = array();
  $form['scout_name'] = array(
    '#type' => 'textfield',
    '#title'  => t('Scout Name'),
    '#maxlength' => 60,
    '#autocomplete_path' => 'scouts/autocomplete',
    '#reguired' => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#title'  => t('ADD BUCKS TO SCOUT PROFILE'),
    '#prefix' => '<div class="submit-button-bucks">',
    '#suffix'   => '</div>',
  );
  return $form;
}

这是自动完成的功能---

function scout_bucks_autocomplete($string = '') {
  $matches = array();
  $userobj = db_fetch_object(db_query("SELECT users.name, users.uid FROM {users}, {users_roles} WHERE users.uid =  users_roles.uid AND users_roles.rid = 7")); 
  while ($row = db_fetch_array($userobj)) {
    $matches[$row['users.uid']] = $row['users.name'];
  }
  drupal_json($matches);
}

可能是什么原因?

4

2 回答 2

0

在我的一个网站上工作的自动完成示例:function module_menu() {

  $items['app/autocomplete/%'] = array(
    'title'            => 'Some title',
    'description'      => '',
    'page callback'    => '_module_autocomplete',
    'page arguments'   => array(2),
    'type'             => MENU_CALLBACK,
  );
 }

function _module_autocomplete($param) {
    $result = db_query('SELECT id,stuff FROM {table} WHERE a=%d', $param);
    while ($row = db_fetch_object($result)) {
      $ret[$row->id] = $row->stuff;
    }

    drupal_json($ret);
}

我已经尽可能地减少了,但我认为最好在菜单回调中定义 %。

于 2013-02-01T18:13:54.620 回答
0

您需要更改drupal_jsondrupal_to_js

function scout_bucks_autocomplete($string = '') {
  $matches = array();
  $userobj = db_fetch_object(db_query("SELECT users.name, users.uid FROM {users}, {users_roles} WHERE users.uid =  users_roles.uid AND users_roles.rid = 7")); 
  while ($row = db_fetch_array($userobj)) {
    $matches[$row['users.uid']] = $row['users.name'];
  }
  drupal_to_js($matches); // the edited line.
}

希望这有效...

于 2013-01-31T12:03:42.373 回答