You are here

amazon.module in Amazon Product Advertisement API 8.2

Same filename and directory in other branches
  1. 6 amazon.module
  2. 7.2 amazon.module
  3. 7 amazon.module

Provides the easy integration of the Amazon Product Advertising API.

File

amazon.module
View source
<?php

/**
 * @file
 * Provides the easy integration of the Amazon Product Advertising API.
 */

/**
 * Implements hook_theme().
 */
function amazon_theme($existing, $type, $theme, $path) {
  return [
    'amazon_inline' => [
      'variables' => [
        'results' => NULL,
        'bundle' => '',
        'field' => '',
      ],
    ],
    'amazon_image' => [
      'variables' => [
        'results' => NULL,
        'size' => '',
      ],
    ],
  ];
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function amazon_theme_suggestions_amazon_inline(array $variables) {
  $suggestions = [];
  if (empty($variables['bundle'] || $variables['field'])) {
    return $suggestions;
  }

  // Add theme suggestions based on the bundle and field.
  $bundle = $variables['bundle'];
  $field = $variables['field'];
  $base = $variables['theme_hook_original'];
  $suggestions[] = $base . '__' . $bundle;
  $suggestions[] = $base . '__' . $field;
  $suggestions[] = $base . '__' . $bundle . '__' . $field;
  return $suggestions;
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function amazon_theme_suggestions_amazon_image(array $variables) {
  $suggestions = [];

  // Add theme suggestions based on image size.
  if (!empty($variables['size'])) {
    $suggestions[] = 'amazon_image__' . $variables['size'];
  }
  return $suggestions;
}

/**
 * Prepares variables for the amazon-inline Twig template.
 *
 * @param array $variables
 *   Array of variables including the results of the APA API call.
 */
function template_preprocess_amazon_inline(&$variables) {

  // Only use the first result since we only build one link.
  $variables['title'] = '';
  $variables['url'] = '';
  $variables['item'] = [];
  if (!empty($variables['results'][0])) {
    $item = $variables['results'][0];
    $variables['title'] = $item->ItemAttributes->Title;
    $variables['url'] = $item->DetailPageURL;
    $variables['item'] = $item;
  }
}

/**
 * Prepares variables for the amazon-image Twig template.
 *
 * @param array $variables
 *   Array of variables including the results of the APA API call.
 */
function template_preprocess_amazon_image(&$variables) {
  switch ($variables['size']) {
    case 'small':
      $image = $variables['results'][0]->SmallImage;
      break;
    case 'medium':
      $image = $variables['results'][0]->MediumImage;
      break;
    case 'large':
      $image = $variables['results'][0]->LargeImage;
      break;
  }
  $variables['image_src'] = (string) $image->URL;
  $variables['width'] = (string) $image->Width;
  $variables['height'] = (string) $image->Height;
  $variables['url'] = $variables['results'][0]->DetailPageURL;
}

Functions

Namesort descending Description
amazon_theme Implements hook_theme().
amazon_theme_suggestions_amazon_image Implements hook_theme_suggestions_HOOK().
amazon_theme_suggestions_amazon_inline Implements hook_theme_suggestions_HOOK().
template_preprocess_amazon_image Prepares variables for the amazon-image Twig template.
template_preprocess_amazon_inline Prepares variables for the amazon-inline Twig template.