You are here

function asin_field_widget_element_validate in Amazon Product Advertisement API 7

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

Widget validation function.

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 267
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) {
    $asin = trim(amazon_convert_to_asin($item['asin']));
    if (!empty($asin) && is_numeric($delta)) {
      $results = amazon_item_lookup(array(
        $asin,
      ));
      if (empty($results)) {
        form_set_error("{$field_name}][{$langcode}][{$delta}][asin", t('No Amazon product with the ASIN "%id" could be located.', array(
          '%id' => $asin,
        )));
      }
      else {
        $item['asin'] = (string) key($results);
      }
    }
  }
}