[development] "Subclassing" the standard #autocomplete form

Rob Thorne rob at torenware.com
Thu Jan 18 19:26:55 UTC 2007


user_autocomplete() takes the string returned by the JavaScript widget, 
does the search -- in this case, for possible Drupal user name matches 
-- and returns the result as a JavaScript-formatted array.

The one thing that's really interesting is the format of the array, 
which  I see is really an associative array (and not  a  simple indexed 
array).   I  don't know how the standard widget would behave if it the 
key and value did not match.   But at this point, I'd like to know as well.


Scott Reynolds wrote:
> I may not have this quiet right but isn't this what hook_autocomplete 
> is for? http://api.drupal.org/api/HEAD/function/user_autocomplete
> You can print drupal_to_js($my_array)? Mostly emailing a response 
> because I would like to know the answer too.
>     Date: Wed, 17 Jan 2007 17:43:10 -0800
>     From: Rob Thorne < rob at torenware.com <mailto:rob at torenware.com>>
>     Subject: [development] "Subclassing" the standard #autocomplete form
>             widget
>     To: development at drupal.org <mailto:development at drupal.org>
>     Message-ID: <45AED0AE.5060806 at torenware.com
>     <mailto:45AED0AE.5060806 at torenware.com>>
>     Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>     I need a widget for Forms API which *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