You are here

function asin_autocomplete_callback in Amazon Product Advertisement API 7

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

Code

function asin_autocomplete_callback($entity_type, $bundle, $field_name, $string = '') {
  $field = field_info_field($field_name);
  $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,
  );
  $results = amazon_http_request('ItemSearch', $parameters, $instance['widget']['settings']['widget_settings']['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);
}