<?php
/**
* Implements hook_field_widget_info().
*
* Two widgets are provided.
* - An autocomplete text field referencing speaker names.
* - An autocomplete text field referencing venue names.
*/
function autocomplete_jsb_users_field_widget_info() {
return array(
'autocomplete_jsb_users_speaker' => array(
'label' => t('Speaker names autocomplete'),
'field types' => array('textfield'),
),
'autocomplete_jsb_users_venue' => array(
'label' => t('Venue names autocomplete'),
'field types' => array('textfield'),
),
);
}
/**
* Implements hook_menu().
*/
function autocomplete_venue_menu() {
$items['ajsbuspeaker/autocomplete'] = array(
'title' => 'Speaker Autocomplete',
'file' => 'autocomplete_jsb_users_speaker.inc',
'type' => MENU_CALLBACK,
);
$items['ajsbuvenue/autocomplete'] = array(
'title' => 'Venue Autocomplete',
'file' => 'autocomplete_jsb_users_venue.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter() for speaker widgets.
*/
function autocomplete_jsb_users_field_widget_autocomplete_jsb_users_speaker_form_alter(&$element, &$form_state, $context) {
if ($context['field']['type'] == 'textfield') {
$element['#autocomplete_path'] = 'ajsbuspeaker/autocomplete';
}
return $element;
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter() for venue widgets.
*/
function autocomplete_jsb_users_field_widget_autocomplete_jsb_users_venue_form_alter(&$element, &$form_state, $context) {
if ($context['field']['type'] == 'textfield') {
$element['#autocomplete_path'] = 'ajsbuvenue/autocomplete';
}
return $element;
}
And here's one of my .inc files that implements hook_autocomplete() (the other .inc is basically the same with a couple of words in the query changed):
<?php
/**
* Implements hook_autocomplete() for speakers.
*/
function autocomplete_jsb_users_speaker_autocomplete($string) {
$matches = array();
// Query the database
$query = db_select('users', 'u');
// Select rows that match the string
$query
->condition('u.role', 'speaker', '=')
->fields('u', array('field_speaker_name'));
$result = $query.execute();
// add matches to $matches
foreach ($result as $row) {
$matches[$row->field_speaker_name] = check_plain($row->field_speaker_name);
}
// return for JS
drupal_json_output($matches);
}
Any ideas on what I'm doing wrong? Thanks!
This post has been edited by VulcanDesign: 22 January 2012 - 06:18 AM

New Topic/Question
Reply



MultiQuote


|