-
Notifications
You must be signed in to change notification settings - Fork 0
/
sirv.module
306 lines (261 loc) · 7.89 KB
/
sirv.module
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/**
* @file
* Provides integration with Sirv's image service.
*/
define('SIRV_MENU_PATH', 'admin/config/media/sirv');
/**
* Implements hook_permission().
*/
function sirv_permission() {
return array(
'administer sirv settings' => array(
'title' => t('Administer Sirv settings'),
'description' => "Modify settings for connecting to Sirv.",
),
);
}
/**
* Implements hook_menu().
*/
function sirv_menu() {
$items = array();
$items[SIRV_MENU_PATH] = array(
'title' => 'Sirv',
'description' => 'Settings for connecting to Sirv.',
'page callback' => 'drupal_get_form',
'page arguments' => array('sirv_settings_form'),
'access arguments' => array('administer sirv settings'),
'file' => 'sirv.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items[SIRV_MENU_PATH . '/settings'] = array(
'title' => 'Settings',
'description' => 'Settings for connecting to Sirv.',
'page callback' => 'drupal_get_form',
'page arguments' => array('sirv_settings_form'),
'access arguments' => array('administer sirv settings'),
'file' => 'sirv.admin.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
return $items;
}
/**
* Implements hook_theme().
*/
function sirv_theme($existing, $type, $theme, $path) {
return array(
'sirv_image_formatter' => array(
'variables' => array('item' => NULL, 'path' => NULL),
'file' => 'sirv.theme.inc',
),
'sirv_image' => array(
'variables' => array('path' => NULL, 'width' => NULL, 'height' => NULL, 'alt' => '', 'title' => NULL, 'url_attribute' => 'src', 'attributes' => array()),
'file' => 'sirv.theme.inc',
),
);
}
/**
* Implements hook_field_formatter_info().
*/
function sirv_field_formatter_info() {
return array(
'sirv_image' => array(
'label' => t('Sirv Image'),
'field types' => array('image'),
'settings' => array('sirv_profile' => '', 'sirv_image_options' => '', 'image_link' => ''),
),
);
}
/**
* Implements hook_field_formatter_settings_form().
*/
function sirv_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$profiles = sirv_profiles();
$element['sirv_profile'] = array(
'#title' => t('Sirv profile'),
'#type' => 'select',
'#default_value' => $settings['sirv_profile'],
'#empty_option' => t('None (original image)'),
'#options' => $profiles,
);
$element['sirv_image_options'] = array(
'#title' => t('Additional Sirv Image options'),
'#type' => 'textfield',
'#description' => t('These options will override profile options. Example of format: %example', array('%example' => 'scale.width=250&grayscale=true')),
'#default_value' => $settings['sirv_image_options'],
);
$link_types = array(
'content' => t('Content'),
'file' => t('File'),
);
$element['image_link'] = array(
'#title' => t('Link image to'),
'#type' => 'select',
'#default_value' => $settings['image_link'],
'#empty_option' => t('Nothing'),
'#options' => $link_types,
);
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function sirv_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = array();
$profiles = sirv_profiles();
if (isset($profiles[$settings['sirv_profile']])) {
$summary[] = t('Sirv profile: @profile', array('@profile' => $profiles[$settings['sirv_profile']]));
}
else {
$summary[] = t('Original image');
}
if (!empty($settings['sirv_image_options'])) {
$summary[] = t('Additional Sirv Image options');
}
$link_types = array(
'content' => t('Linked to content'),
'file' => t('Linked to file'),
);
// Display this setting only if image is linked.
if (isset($link_types[$settings['image_link']])) {
$summary[] = $link_types[$settings['image_link']];
}
return implode('<br />', $summary);
}
/**
* Implements hook_field_formatter_view().
*/
function sirv_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
// Check if the formatter involves a link.
if ($display['settings']['image_link'] == 'content') {
$uri = entity_uri($entity_type, $entity);
}
elseif ($display['settings']['image_link'] == 'file') {
$link_file = TRUE;
}
foreach ($items as $delta => $item) {
// Add additional options to the URI.
$item['uri'] = _sirv_add_options_to_uri($item['uri'], $display['settings']['sirv_profile'], $display['settings']['sirv_image_options']);
if (isset($link_file)) {
$uri = array(
'path' => file_create_url($item['uri']),
);
}
$element[$delta] = array(
'#theme' => 'sirv_image_formatter',
'#item' => $item,
'#path' => isset($uri) ? $uri : '',
);
}
return $element;
}
/**
* Retrieve available Sirv profiles.
*
* @return array
* An array of Sirv profiles.
*/
function sirv_profiles() {
$profiles = array();
$bucket = variable_get('sirv_bucket');
$profiles_directory = variable_get('sirv_profiles_directory');
$s3client = _sirv_s3client();
if (!$s3client) {
return array();
}
$iterator = $s3client->getIterator('ListObjects', array(
'Bucket' => $bucket,
'Prefix' => $profiles_directory,
));
foreach ($iterator as $object) {
$key = $object['Key'];
if (preg_match('#([^/]+)\.profile$#', $key, $matches)) {
$profiles[$matches[1]] = $matches[1];
}
}
return $profiles;
}
/**
* Loads the awssdk2 library.
*
* @return array
* The array returned by libraries_load('awssdk2'), as if it used no cache.
*/
function _sirv_load_awssdk2_library() {
// Start by calling libraries_load().
$library = libraries_load('awssdk2');
// If it detects and loads the library, great! We're done.
if (!empty($library['loaded'])) {
return $library;
}
// Otherwise, clear the awssdk2 value from the Libraries cache, erase the
// static data for libraries_load(), then call it again to get the real
// state of the library.
cache_clear_all('awssdk2', 'cache_libraries');
drupal_static_reset('libraries_load');
return libraries_load('awssdk2');
}
/**
* Create and return a new S3 Client object.
*
* @return S3Client object
*/
function _sirv_s3client(){
$library = _sirv_load_awssdk2_library();
$key = variable_get('sirv_key');
$secret_key = variable_get('sirv_secret_key');
$host = variable_get('sirv_host');
if (empty($key) || empty($secret_key) || empty($host)) {
return NULL;
}
$client_config = array(
'base_url' => $host,
'key' => $key,
'secret' => $secret_key,
);
$s3client = Aws\S3\S3Client::factory($client_config);
return $s3client;
}
/**
* Add additional options to URI for an image.
*/
function _sirv_add_options_to_uri($uri, $profile = '', $options = array()) {
// Parse the URI and save the query and fragment.
$parsed_uri = drupal_parse_url($uri);
$path = $parsed_uri['path'];
$query = $parsed_uri['query'];
$fragment = $parsed_uri['fragment'];
// If a profile is specified, add it as a query parameter.
if ($profile) {
$query['profile'] = $profile;
}
// If additional options were entered, add them as query parameters.
if ($options) {
// parse_str is not used here, because it converts periods to underscores.
$options = explode('&', $options);
foreach ($options as $option) {
$parameter = explode('=', $option);
if (!empty($parameter[0]) && isset($parameter[1])) {
$query[$parameter[0]] = $parameter[1];
}
}
}
// Start building the URI again with the path.
$uri = $path;
// Add any query parameters to the URI.
if (!empty($query)) {
$uri .= '?' . drupal_http_build_query($query);
}
// Add back the fragment, if there is one.
if (!empty($fragment)) {
$uri .= '#' . $fragment;
}
return $uri;
}