You are here

function asin_autocomplete_callback in Amazon Product Advertisement API 6

Same name and namespace in other branches
  1. 7.2 asin/asin.module \asin_autocomplete_callback()
  2. 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 399
Defines a field type for referencing an Amazon product.

Code

function asin_autocomplete_callback($string = '') {
  $items = $matches = array();
  $cleanstring = trim(preg_replace('#[^\\p{L}\\p{N}]+#u', ' ', $string));

  // Search Amazon.
  $parameters = array(
    'ResponseGroup' => 'Small',
    'SearchIndex' => 'All',
    'Keywords' => urlencode($cleanstring),
  );
  $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);
}