I made a form with a customized autocomplete field. I wanted it to only get results for books, so I found the part of the asin.module that decided this around line 396:
/**
* Autocomplete callback for the asin_autocomplete widget.
*/
function asin_autocomplete_callback($string = '') {
$items = $matches = array();
// Search Amazon.
$parameters = array(
'ResponseGroup' => 'Small',
'SearchIndex' => 'Blended',
'Keywords' => urlencode($string)
);
$results = amazon_http_request('ItemSearch', $parameters);
// Process the results.
foreach($results->Items->Item as $xml) {
$items[(string) $xml->ASIN] = (string) $xml->ItemAttributes->Title . ' (' . $xml->ItemAttributes->ProductGroup . ')';
}
// Create our response.
foreach ($items as $asin => $title) {
// Add a class wrapper for a few required CSS overrides.
$matches[$title . ' [asin:' . $asin . ']'] = '<div class="reference-autocomplete">'. $title . '</div>';
}
drupal_json($matches);
}
All I did was change mine to:
/**
* Autocomplete callback for the asin_autocomplete widget.
*/
function asin_autocomplete_callback($string = '') {
$items = $matches = array();
// Search Amazon.
$parameters = array(
'ResponseGroup' => 'Medium',
'SearchIndex' => 'Books',
'Keywords' => urlencode($string)
);
$results = amazon_http_request('ItemSearch', $parameters);
// Process the results.
foreach($results->Items->Item as $xml) {
$items[(string) $xml->ASIN] = (string) $xml->ItemAttributes->Title . ' (' . $xml->ItemAttributes->ProductGroup . ')';
}
// Create our response.
foreach ($items as $asin => $title) {
// Add a class wrapper for a few required CSS overrides.
$matches[$title . ' [asin:' . $asin . ']'] = '<div class="reference-autocomplete">'. $title . '</div>';
}
drupal_json($matches);
}
It might help others if somehow that could be set somehow when setting up the field on a form.
Comments
Comment #1
rfayIf you want to post a patch, it will be considered. Please look at the 7.x implementation before you do. I thought both had a variable deciding the search; maybe not.
Comment #2
willvincent commentedComment #3
willvincent commentedWorking on a bit more robust implementation of this.
Comment #4
willvincent commentedAnd heeere we go.. (depends on and expands on functionality from changes commit in #1083892: Store Amazon associate ID per locale).
Just commit this. It updates the autocomplete callback path, and provides additional widget settings (all existing autocomplete widgets are updated when update.php is run). New widget settings allow for configuration of a specific locale and product group/search index to use for a specific instance of the widget.
Existing fields using the auto complete widget will be updated to whatever is configured as the default locale, with search index of 'all' -- so existing fields will retain current functionality, but can be reconfigured to only return, books, for example.
IMHO, this is a pretty awesome change. :)
Comment #5
willvincent commentedComment #6
willvincent commentedPerhaps this should also get a 6.x backport.
Anyone want to tackle that?
Comment #7
willvincent commentedI think #396554: Allow dynamic amazon locale choice will still need some attention before this is really ready for prime time use with multiple locales.
Comment #8
wxman commentedWas this patch added to the latest dev version yet? I tried to add a test field to see the changes, but all I found was the locale setting.
Comment #9
willvincent commentedYes, it's in the latest dev. The product group only comes up after you select the locale, because available product groups are not the same between the various locales.
You probably need to clear cache after install since the locale data is cached, and it has changed.
Comment #10
willvincent commentedAlso worth noting, this is NOT in the 6.x branch
Comment #11
wxman commentedYou were right. I had cleared the cache, and saved the field with the locale. The field doesn't show up until you close the edit page, then go back into it. At least that happened to me.
Comment #12
willvincent commentedThere is an ajax callback for the search index field to display based on the setting of the locale field, so if you change the locale the available search indexes should refresh.
It sounds like you might have some weird problems due to caching issues with your site though based on your comments in the other issue, dunno how much more I can assist you with that.
Comment #13
wxman commentedThis issue is minor, and easy to deal with. The advantage of the new features far outweighs the possible problem.
I don't know what to tell you about the other problem. It's giving me a headache.
Comment #14
willvincent commentedAll I'm saying is that it sounds like there might be something wrong with your installation. Beyond the scope of anything this module is responsible for.