Cách tạo 1 Textfield Ajax Autocomplete trong Drupal 7

Cách tạo 1 Textfield Ajax Autocomplete trong Drupal 7

How To Create an Ajax Autocomplete Textfield In Drupal 7

People are lazy by nature and love when something is done for them automatically. As web developers, we can always use this need in our website designs by creating different autocomplete fields to ease website visitor's experience. Being the best CMS platform outhere, Drupal allows us to create autocomplete fields within a blink of an eye. Here’s how to do this:

Let’s imagine, you’re building some form where you want to help user with an autocomplete. For example, this can be a "City" input textfield. You can have something similar to the following form:

function module_name_form() {
  $form = array();
 
  $form['city'] = array(
    '#title' => t('City'),
    '#type' => 'textfield',
    '#autocomplete_path' => 'example/autocomplete',
   );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Save',
  );
 
  return $form;
}

The most interesting part here is: '#autocomplete_path' => 'example/autocomplete' which defines an autocomplete callback for our textfield. When user types something into our textfield, Drupal sends a request via this url to send data you input and to receive suggestions via json if there are any. To make Drupal aware of this path, let’s add it to the menu system:

function module_name_menu() { 
  $items['example/autocomplete'] = array(
    'page callback' => '_module_name_autocomplete',
    'access arguments' => array('access example autocomplete'),
    'type' => MENU_CALLBACK
  );
  return $items;
}

The last step is to create a _module_name_autocomplete function which will look for a suggestion and return it to the user.

function _module_name_autocomplete($string) {
  $matches = array();

  // Some fantasy DB table which holds cities
  $query = db_select('cities', 'c');

  // Select rows that match the string
  $return = $query
    ->fields('c', array('city'))
    ->condition('c.city', '%' . db_like($string) . '%', 'LIKE')
    ->range(0, 10)
    ->execute();
 
  // add matches to $matches 
  foreach ($return as $row) {
    $matches[$row->city] = check_plain($row->city);
  }
 
  // return for JS
  drupal_json_output($matches);
}

In this function we query DB to find a match for a string typed in by a user. I use Dynamic queries (http://drupal.org/node/310075) to get data from a DB, if you feel yourself more comfortable with a SQL approach, feel free to use it as well. The current query is a fantasy one, yours can be a very different from this. If you want for a more advanced example, look in to the Drupal’s core taxonomy autocomplete function which can be found in ‘modules/taxonomy/taxonomy.pages.inc’, line 79: function taxonomy_autocomplete.

All in all, I guess now you have an idea about hot to create an autocomplete text field in your custom Drupal 7 module.

Happy Drupal Coding!

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Tìm kiếm bất động sản

 

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
HUTECH công bố điểm trúng tuyển của phương thức xét tuyển học bạ lớp 12

HUTECH công bố điểm trúng tuyển của phương thức xét tuyển học bạ lớp 12

Theo đó, mức điểm trúng tuyển từ 18 đến 24 điểm tùy ngành và tổ hợp xét tuyển. Đồng thời, HUTECH cũng thông báo tiếp tục nhận hồ sơ xét tuyển học bạ đến ngày 10-7 cho tất cả các ngành đào tạo tại trường, với mức điểm từ điểm trúng tuyển của đợt ngày 30-6 trở lên.

7 điều Social Media không thể làm được

7 điều Social Media không thể làm được

Social Media mang lại nhiều điều cho cá nhân và doanh nghiệp của bạn, nhưng vẫn có những điều mà Social Media không thể giúp bạn. Hãy nhận thức đúng đắn về vai trò và tầm quan trọng của Social Media để vận dụng hiệu quả nhất.

"Virus" Rihanna lây lan "chóng mặt" trên Facebook

Virus Rihanna lây lan chóng mặt trên Facebook

Trong những ngày gần đây, nhiều người sử dụng Facebook đã bị sập bẫy "virus" ca sĩ Rihanna thông qua các đường link chia sẻ về cô ca sĩ này trên trang cá nhân.

 

Công ty diệt chuột T&C

 

Diet con trung