You are here

function asin_field_widget_element_validate in Amazon Product Advertisement API 7.2

Same name and namespace in other branches
  1. 7 asin/asin.module \asin_field_widget_element_validate()

Text callback for the asin_text widget.

Checks to see if we can look up an ASIN, URL, or ISBN-13 using the provided text. If we can, it's OK and we turn it into an ASIN. Otherwise, flag as error.

1 string reference to 'asin_field_widget_element_validate'
asin_field_widget_form in asin/asin.module
Implements hook_field_widget_form().

File

asin/asin.module, line 316
Defines a field type for referencing an Amazon product.

Code

function asin_field_widget_element_validate($element, &$form_state) {
  $field_name = $element['#field_name'];
  $langcode = $form_state['langcode'];
  foreach ($form_state['values'][$field_name][$langcode] as $delta => &$item) {
    $locale = NULL;
    if (isset($form_state['field'][$field_name][LANGUAGE_NONE]['instance']['widget']['settings']['widget_settings']['locale'])) {
      $locale = $form_state['field'][$field_name][LANGUAGE_NONE]['instance']['widget']['settings']['widget_settings']['locale'];
    }
    $asin = trim(amazon_convert_to_asin($item['asin']));
    if (!empty($asin) && is_numeric($delta)) {
      $results = amazon_item_lookup(array(
        $asin,
      ), FALSE, $locale);
      if (empty($results)) {
        form_set_error("{$field_name}][{$langcode}][{$delta}][asin", t('No Amazon product with the ASIN "%id" could be located in %locale.', array(
          '%id' => $asin,
          '%locale' => $locale,
        )));
      }
      else {
        $item['asin'] = (string) key($results);
      }
    }
  }
}