You are here

function asin_field_formatter_view in Amazon Product Advertisement API 7

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

Implements hook_field_formatter_view().

File

asin/asin.module, line 131
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();
  foreach ($items as $delta => $value) {
    $asin = trim($value['asin']);
    if (!empty($asin)) {
      if ($display['type'] == 'asin_plain') {
        $element[$delta] = array(
          '#markup' => $asin,
        );
      }
      else {
        $lookup = amazon_item_lookup($asin);
        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);
          }
          $element[$delta] = array(
            '#markup' => theme($theme_function, array(
              'item' => $item,
              'style' => $style,
            )),
          );
        }
      }
    }
  }
  return $element;
}