-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamazon.module
More file actions
103 lines (91 loc) · 2.57 KB
/
amazon.module
File metadata and controls
103 lines (91 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?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;
}