-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia_pixabay.module
330 lines (270 loc) · 8.59 KB
/
media_pixabay.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
/**
* @file
* Provides definition for Pixabay media integration.
*/
/**
* Implements hook_menu().
*/
function media_pixabay_menu() {
$items['admin/config/media/pixabay'] = array(
'title' => 'Pixabay Media',
'description' => 'Settings for media_pixabay module',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_pixabay_admin'),
'access arguments' => array('administer media browser'),
'type' => MENU_NORMAL_ITEM,
'file' => 'media_pixabay.admin.inc',
);
$items['file/add/pixabay'] = array(
'title' => 'Pixabay',
'description' => 'Add images from Pixabay to your media library.',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_pixabay_external'),
'access arguments' => array('access media browser'),
'type' => MENU_LOCAL_TASK,
'file' => 'file_entity.pages.inc',
'file path' => drupal_get_path('module', 'file_entity'),
);
return $items;
}
/**
* Implements hook_theme().
*/
function media_pixabay_theme($existing, $type, $theme, $path) {
return array(
'media_pixabay_list' => array(
'variables' => array(
'images' => NULL,
),
'template' => 'media-pixabay-list',
'path' => drupal_get_path('module', 'media_pixabay') . '/templates',
),
);
}
/**
* Implements hook_forms().
*/
function media_pixabay_forms($form_id, $args) {
$forms = array();
// Create a copy of the upload wizard form for internet media.
if ($form_id == 'media_pixabay_external') {
$forms[$form_id] = array(
'callback' => 'file_entity_add_upload',
);
}
return $forms;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function media_pixabay_form_file_entity_add_upload_alter(&$form, &$form_state, $form_id) {
$step = $form['#step'];
$options = $form['#options'];
// Swap the upload field for an embed field when on the first step of the web
// tab.
if ($form_id == 'media_pixabay_external' && $step == 1) {
unset($form['upload']);
// Add JS & CSS.
drupal_add_js(drupal_get_path('module', 'media_pixabay') . '/js/media_pixabay.js');
drupal_add_css(drupal_get_path('module', 'media_pixabay') . '/css/media_pixabay.css');
$form['external_image'] = array(
'#type' => 'textfield',
'#reguired' => 1,
'#attributes' => array('placeholder' => t('Enter search term')),
);
$form['embed_code'] = array(
'#type' => 'hidden',
'#title' => t('File URL'),
'#description' => t('Enter a URL to a file.'),
'#maxlength' => 2083,
'#default_value' => isset($form_state['storage']['embed_code']) ? $form_state['storage']['embed_code'] : NULL,
);
$form['search'] = array(
'#type' => 'submit',
'#value' => t('Search'),
'#input' => TRUE,
'#name' => 'op',
'#button_type' => 'submit',
'#executes_submit_callback' => FALSE,
'#limit_validation_errors' => FALSE,
'#process' => array('ajax_process_form'),
'#theme_wrappers' => array('button'),
'#ajax' => array(
'callback' => 'media_pixabay_external_ajax_callback',
'wrapper' => 'pixabay-output',
'event' => 'click',
),
);
// Blank output field which we will fill using AJAX.
$form['output'] = array(
'#prefix' => '<div id="pixabay-output">',
'#suffix' => '</div>',
'#markup' => '',
);
// Create an array to hold potential Internet media providers.
$providers = array();
// Determine if there are any visible providers.
foreach (media_internet_get_providers() as $key => $provider) {
if (empty($provider['hidden']) || $provider['hidden'] != TRUE) {
$providers[] = check_plain($provider['title']);
}
}
$form['#validators'] = array();
if (!empty($options['types'])) {
$form['#validators']['media_file_validate_types'] = array($options['types']);
}
// Add validation and submission handlers to the form and ensure that they
// run first.
array_unshift($form['#validate'], 'media_pixabay_add_validate');
array_unshift($form['#submit'], 'media_internet_add_submit');
}
}
/**
* AJAX callback function for media_pixabay_external().
*/
function media_pixabay_external_ajax_callback($form, $form_state) {
// Search input.
$search_term = check_plain($form_state['values']['external_image']);
// Receive content.
$content = media_pixabay_search($search_term);
$output = theme('media_pixabay_list', array(
'images' => $content,
));
// Return ajax.
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_html('#pixabay-output', $output),
array(
'command' => 'afterAjaxScheduleAdded',
),
),
);
}
/**
* Main function for interaction with Pixabay API.
*
* @param string $search_term
* String upon which we call pixabay api.
*
* @return string
* Return HTML.
*/
function media_pixabay_search($search_term) {
// Pixabay API key.
$pixabay_api = variable_get('media_pixabay_api', "");
if (empty($pixabay_api)) {
return t('Please insert API key on module settings page');
}
$cid = 'media:pixabay:' . $search_term;
// If a cached entry exists, return it.
if ($cached = cache_get($cid)) {
$content = $cached->data;
}
else {
// Generate API url for request.
$context = array(
'key' => $pixabay_api,
'q' => $search_term,
// Todo - maybe add to settings, include vectors? Add category filters?
'image_type' => 'image,illustration',
'per_page' => 100,
);
$api_url = 'https://pixabay.com/api/?';
$url = url($api_url, array('query' => $context));
// Make request.
$request = drupal_http_request($url);
// If response is valid.
if ($request->code == 200) {
// Get response data.
$json_response = drupal_json_decode($request->data);
// Set default message if no images.
$content = t('No pictures for current search');
// Loop trough content.
if (!empty($json_response['hits'])) {
// Define result as array.
$images = array();
foreach ($json_response['hits'] as $key => $response_data) {
$thumb = $response_data['previewURL'];
$title = $response_data['tags'];
$id = $response_data['id'];
$full = $response_data['webformatURL'];
// Get full image.
$download_url = str_replace('_640', '_960', $full);
$output = '<div class="media-thumbnail">';
$output .= '<img class="pixabay" data-image="' . $download_url . '" src="' . $thumb . '"/>';
$output .= '<div class="description">' . $title . '</div>';
$output .= '</div>';
$images[] = array(
'download' => $download_url,
'thumb' => $thumb,
'title' => $title,
);
}
$content = $images;
// Set cache to three minutes, respect Pixabay rules.
cache_set($cid, $content, 'cache', time() + 180);
}
}
else {
// Print error message from Pixabay API.
$content = $request->data;
}
}
return $content;
}
/**
* Allow stream wrappers to have their chance at validation.
*
* Any module that implements hook_media_parse will have an
* opportunity to validate this.
*
* @see media_parse_to_uri()
*/
function media_pixabay_add_validate($form, &$form_state) {
$embed_code = $form_state['values']['embed_code'];
// See @file_entity_add_upload_submit
// Needed for skipping fields if is enabled and if Web tab is active.
$form_state['triggering_element']['#id'] = 'edit-next';
if (!empty($embed_code)) {
try {
$provider = media_internet_get_provider($embed_code);
$provider->validate();
}
catch (MediaInternetNoHandlerException $e) {
form_set_error('embed_code', $e->getMessage());
return;
}
catch (MediaInternetValidationException $e) {
form_set_error('embed_code', $e->getMessage());
return;
}
$validators = $form['#validators'];
$file = $provider->getFileObject();
if ($validators) {
try {
$file = $provider->getFileObject();
}
catch (Exception $e) {
form_set_error('embed_code', $e->getMessage());
return;
}
$errors = file_validate($file, $validators);
if (!empty($errors)) {
$message = t('%url could not be added.', array('%url' => $embed_code));
if (count($errors) > 1) {
$message .= theme('item_list', array('items' => $errors));
}
else {
$message .= ' ' . array_pop($errors);
}
form_set_error('embed_code', $message);
return FALSE;
}
}
}
else {
return FALSE;
}
}