You are here

function asin_field in Amazon Product Advertisement API 6

Implementation of hook_field().

File

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

Code

function asin_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'insert':
    case 'update':
      $results = _asin_load_items($items);
      return;
      break;
    case 'validate':

      // Trim ASINs to help the user out.
      _asin_trim_items($items);
      $results = _asin_load_items($items);
      foreach ($items as $delta => $item) {
        if (is_array($item)) {
          if (!empty($item['asin']) && empty($results[$item['asin']])) {
            form_set_error($field['field_name'] . '][' . $delta . '][asin', t('%name : No Amazon product with the ASIN "%id" could be located.', array(
              '%name' => $field['widget']['label'],
              '%id' => $item['asin'],
            )));
          }
        }
      }
      return;
      break;
    case 'load':
      _asin_trim_items($items);
      $results = _asin_load_items($items);
      foreach ($items as $delta => $item) {
        if (!empty($item['asin'])) {
          $items[$delta] = $results[$item['asin']];
        }
      }
      return $items;
      break;
    case 'delete':
      $results = _asin_load_items($items);
      foreach ($items as $delta => $item) {
        if (!empty($item['asin'])) {
          amazon_item_delete($item['asin']);
        }
      }
      return;
      break;
  }
}