0 Replies - 386 Views - Last Post: 22 January 2012 - 06:03 AM

Topic Sponsor:

#1 VulcanDesign  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 98
  • Joined: 06-December 09

Drupal 7 custom widget module problems

Posted 22 January 2012 - 06:03 AM

I'm trying to write a module for Drupal 7 that adds adds two autocomplete widgets for text fields. When installed, it's not showing up in the list of textfield widgets. Here's what I have for my main .module file:

<?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


Is This A Good Question/Topic? 0
  • +

Page 1 of 1