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

rfay’s picture

If 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.

willvincent’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev
willvincent’s picture

Assigned: Unassigned » willvincent

Working on a bit more robust implementation of this.

willvincent’s picture

And 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. :)

willvincent’s picture

Status: Active » Fixed
willvincent’s picture

Status: Fixed » Patch (to be ported)

Perhaps this should also get a 6.x backport.

Anyone want to tackle that?

willvincent’s picture

I think #396554: Allow dynamic amazon locale choice will still need some attention before this is really ready for prime time use with multiple locales.

wxman’s picture

Was 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.

willvincent’s picture

Yes, 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.

willvincent’s picture

Also worth noting, this is NOT in the 6.x branch

wxman’s picture

You 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.

willvincent’s picture

There 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.

wxman’s picture

This 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.

willvincent’s picture

All 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.