[development] "Subclassing" the standard #autocomplete form widget

Rob Thorne rob at torenware.com
Thu Jan 18 01:43:10 UTC 2007


I need a widget for Forms API which is *almost* identical to the 
textfield autocomplete widget in 5.0.  I want the same user experience 
-- the user types and chooses an item.  But I want the underlying AJAX 
and Drupal behavior to differ a bit.

An example of the type of widget I'd want would be a "Country Selection" 
widget where what I really want is the two-character country code (US, 
CA, GB, FR, and so on),  but I want the user to think of typing "United 
States",  "Canada", "Great Britain", "France" or so on in order to get 
the popup  to get set on this code.

Looking through the 5.0 sources, the way the current autocompletion 
widget works is via includes/form.inc,  where the theming function 
theme_textfield($element)  special cases textfields with the 
'#autocompletion_path' ,  by making sure that autocomplete.js gets added 
to the page going out,  and by adding a hidden <item> to store state 
information that the code in autocomplete.js needs to do its work.

I really don't need to change behavior very much;  I just need to send 
back an array of array(key, value), where the standard widget sends back 
an array of string values.  But pretty much everything else that 
Unconed's code does exactly what I want, and while I can copy if if 
that's easiest, I don't want or need to change what he did.

What's the best way to create my modified Forms API widget?  It looks 
like I'd want to "override" or "subclass" theme_textfield, although only 
to add behavior:  I don't want standard autocomplete widgets to behave 
any different than usual (although I'd not want both sets of code to 
compete on the same widget).  I'd then copy autocomplete.js and make 
very small changes in it, changing function names where required.

Is there a better way to do this?  Or even better, has someone else 
already done this?

Thanks,
Rob

Rob Thorne
Torenware Networks


More information about the development mailing list