You are here

function asin_field_formatter_view in Amazon Product Advertisement API 7.2

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

Implements hook_field_formatter_view().

File

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

Code

function asin_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  if (isset($instance['widget']['settings']['widget_settings']['locale'])) {
    $locale = $instance['widget']['settings']['widget_settings']['locale'];
  }
  else {
    $locale = variable_get('amazon_default_locale');
  }
  foreach ($items as $delta => $value) {
    $asin = trim($value['asin']);
    if (!empty($asin)) {

      // If :: Check to see if the field format is set to "ASIN as plain text"
      // 1 - Return plain Amazon Product ASIN
      // 0 - Return themed Amazon Product Item
      if ($display['type'] == 'asin_plain') {
        $element[$delta] = array(
          '#markup' => $asin,
        );
      }
      else {

        // Lookup :: Search the amazon_item table or request Amazon API information
        $lookup = amazon_item_lookup($asin, FALSE, $locale);

        // If :: Check to see if Amazon Product was returned succesfully
        if (!empty($lookup) && ($item = $lookup[$asin])) {

          // // TODO: kill off amazon_inline_item. There's no reason for it to clutter the earth.
          $theme_function = $display['type'] == 'asin_inline' ? 'amazon_inline_item' : 'amazon_item';

          // Trim the 'asin_' prefix from the formatter machine name before
          // passing it to the theme function.
          if (strpos($display['type'], 'asin_') === 0) {
            $style = substr($display['type'], 5);
          }
          $elements = array(
            '#theme' => $theme_function,
            '#style' => $style,
            '#item' => $item,
          );
          $element[$delta] = array(
            '#markup' => drupal_render($elements),
          );
        }
      }
    }
  }
  return $element;
}