From ac8e1d1657740597f9be793fb6ef869d3b8371ac Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Tue, 17 Sep 2019 13:43:49 +1000 Subject: [PATCH] test ecclesia autocomplete --- booking.module | 8 +++++++ booking.regn_form.inc | 53 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/booking.module b/booking.module index bc2c9bb..62d68df 100644 --- a/booking.module +++ b/booking.module @@ -321,6 +321,14 @@ function booking_menu() { 'type' => MENU_NORMAL_ITEM, ); + //callback for autocomplete lookup of ecclesia name + $items['booking/ecclesia/autocomplete'] = array( + 'title' => 'Autocomplete for ecclesia lookup on registration page', + 'page callback' => '_booking_ecclesia_name_autocomplete', + 'access arguments' => array('access booking form'), + 'type' => MENU_CALLBACK + ); + $items['waitinglist'] = array( 'title' => "Who's on the waiting list?", 'page callback' => 'booking_waitinglist_page', diff --git a/booking.regn_form.inc b/booking.regn_form.inc index 0f1b34f..4955983 100644 --- a/booking.regn_form.inc +++ b/booking.regn_form.inc @@ -63,6 +63,31 @@ function booking_register_page() return $return_array; } +/** + * autocomplete helper to look up ecclesia names + * based on https://www.drupal.org/node/854216 + * $string = string for search + */ +function _booking_ecclesia_name_autocomplete($string) { + $matches = array(); + $sanitised_string = preg_replace("/[^a-zA-Z0-9\s\.']/", '', $string); + $sanitised_string += "%"; + watchdog('booking_debug', "
Autocomplete checking for ecclesia matching:\n@info
", array('@info' => print_r($sanitised_string, true))); + + $query = db_select('booking_person', 'p') + ->fields('p', array('booking_ecclesia')) + ->condition('p.booking_ecclesia', $sanitised_string, 'LIKE'); + $result = $query->execute(); + + // save the query to matches + foreach ($result as $row) { + $matches[$row->booking_ecclesia] = $row->booking_ecclesia; + } + + // Return the result to the form in json + drupal_json_output($matches); +} + function booking_form($node, &$form_state, $inserting = FALSE) { global $event; @@ -289,14 +314,26 @@ function booking_form($node, &$form_state, $inserting = FALSE) '#options' => _get_tshirt_options() ); } //end enable tshirts check - - $form['your-details']['booking_ecclesia'] = array( - '#type' => 'textfield', - '#title' => t('Ecclesia'), - '#maxlength' => 100, - '#required' => FALSE, - '#default_value' => !empty($data->booking_ecclesia) ? $data->booking_ecclesia : '' - ); + + if ($inserting == FALSE) { + $form['your-details']['booking_ecclesia'] = array ( + '#type' => 'textfield', + '#title' => t('Ecclesia'), + '#size' => 100, + '#autocomplete_path' => 'booking/ecclesia/autocomplete', + '#value' => !empty($data->booking_ecclesia) ? $data->booking_ecclesia : '', + ); + } + else { + $form['your-details']['booking_ecclesia'] = array( + '#type' => 'textfield', + '#title' => t('Ecclesia'), + '#maxlength' => 100, + '#required' => FALSE, + '#default_value' => !empty($data->booking_ecclesia) ? $data->booking_ecclesia : '', + ); + } + $form['your-details']['booking_baptised'] = array( '#type' => 'checkbox', '#title' => t('I am baptised'),