Drupal表单自动完成

时间:2014-07-06 19:50:06

标签: drupal autocomplete drupal-7

我希望在自动填充数据库的表单中有一个文本字段。我一直在阅读教程并试图让它工作,但到目前为止运气不大。我对Drupal很新,所以我大多不确定我在做什么xD

到目前为止我的代码:

function my_module_menu() {
  $items = array();
  $items['my_module/form'] = array(
    'title' => t('Add List'),
    'page callback' => 'my_module_form',
    'access arguments' => array('access content'),
    'description' => t('My form'),
    'type' => MENU_CALLBACK,
  );

// path with autocomplete function for casters
  $items['caster/autocomplete'] = array(
    'title' => t('Autocomplete for casters'),
    'page callback' => '_caster_autocomplete',
    'access arguments' => array('use autocomplete'),
    'type' => MENU_CALLBACK
  );
  return $items;
}

function my_module_form() {
  return drupal_get_form('my_module_my_form');
}

function _caster_autocomplete($string) {
  $matches = array();
  $results = db_select('warnoun', 'w')
    ->fields('w', array('full_name'))
    ->condition('full_name', '%' . db_like($string) . '%', 'LIKE')
    ->execute();
  // save the query to matches
  foreach ($results as $result) {
    $matches[$result->full_name] = check_plain($result->full_name);
  }
  // Return the result to the form in json
  drupal_json_output($matches);
}

function my_module_my_form($form_state) {
  $form['points'] = array(
    '#type' => 'select',
    '#title' => t('Points'),
    '#required' => TRUE, // Added
    '#options' => array(
         0 => t('15'),
         1 => t('25'),
         2 => t('35'),
         3 => t('50'),
       ),
  );
  $form['caster'] = array(
    '#type' => 'textfield',
    '#title' => t('Caster'),
    '#maxlength' => 50,
    '#autocomplete_path' => 'caster/autocomplete',
    '#required' => TRUE,
  );

  $entries = db_query('SELECT id, short_name FROM factions ORDER BY id ASC');
  $options = array();
  foreach ($entries as $entry){
        $options[$entry->id] = t($entry->short_name);
  }



  $form['faction'] = array(
       '#type' => 'select',
       '#title' => t('Faction'),
       '#options' => $options,
   );

   // Simple Submit Button
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
    '#required' => TRUE, // Added
  );
  return $form;
}

所以基本上有一个名为 warnoun 的表,其中包含 full_name 列,而且我想要自动填充文本字段 caster 的内容。

知道我错过了什么/做错了吗?

干杯

0 个答案:

没有答案