function asin_autocomplete_callback in Amazon Product Advertisement API 7.2
Same name and namespace in other branches
- 6 asin/asin.module \asin_autocomplete_callback()
- 7 asin/asin.module \asin_autocomplete_callback()
Autocomplete callback for the asin_autocomplete widget.
1 string reference to 'asin_autocomplete_callback'
- asin_menu in asin/
asin.module - Implementation of hook_menu().
File
- asin/
asin.module, line 346 - Defines a field type for referencing an Amazon product.
Code
function asin_autocomplete_callback($entity_type, $bundle, $field_name, $string = '') {
$instance = field_info_instance($entity_type, $field_name, $bundle);
$items = $matches = array();
$cleanstring = trim(preg_replace('#[^\\p{L}\\p{N}]+#u', ' ', $string));
// Search Amazon.
$parameters = array(
'ResponseGroup' => 'Small',
'SearchIndex' => $instance['widget']['settings']['widget_settings']['productgroup'],
'Keywords' => $cleanstring,
);
if (isset($instance['widget']['settings']['widget_settings']['locale'])) {
$locale = $instance['widget']['settings']['widget_settings']['locale'];
}
else {
$locale = variable_get('amazon_default_locale', 'US');
}
$results = amazon_http_request('ItemSearch', $parameters, $locale);
// 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_output($matches);
}