-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_image.module
121 lines (111 loc) · 4.29 KB
/
remote_image.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
<?php
/**
* @file
* Code for the remote_image feature.
*/
/**
* Implements hook_action_info
* @return array associative array of action descriptions
*/
function remote_image_action_info() {
return array(
'remote_image_get_remote_image' => array(
'type' => 'node',
'label' => t('Pull the remote images into their local image fields'),
'behavior' => array('changes_property'),
'configurable' => FALSE,
'vbo_configurable' => TRUE,
'triggers' => array('any'),
),
);
}
function remote_image_get_remote_image_views_bulk_operations_form($options) {
$form = array();
$form['remote_image_url_field'] = array(
'#type' => 'textfield',
'#title' => t('Machine name of the image URL field'),
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
'#default_value' => !empty($options['remote_image_url_field']) ? $options['remote_image_url_field'] : '',
);
$form['remote_image_image_field'] = array(
'#type' => 'textfield',
'#title' => t('Machine name of the field where the image will be stored'),
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
'#default_value' => !empty($options['remote_image_image_field']) ? $options['remote_image_image_field'] : '',
);
$form['remote_image_filename_field'] = array(
'#type' => 'textfield',
'#title' => t('Machine name of the field that holds the image file name'),
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
'#default_value' => !empty($options['remote_image_filename_field']) ? $options['remote_image_filename_field'] : '',
);
$form['remote_image_file_path'] = array(
'#type' => 'textfield',
'#title' => t('Directory to store the file in (this directory must already exist)'),
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
'#default_value' => !empty($options['remote_image_file_path']) ? $options['remote_image_file_path'] : 'public://remote_image',
);
$form['remote_image_replace_existing'] = array(
'#type' => 'checkbox',
'#title' => t('Replace all existing images'),
'#default_value' => empty($options['remote_image_replace_existing']) ? $options['remote_image_replace_existing'] : 0,
);
return $form;
}
function remote_image_get_remote_image_form($settings, &$form_state) {
$form = remote_image_get_remote_image_views_bulk_operations_form($settings['settings']);
return $form;
}
function remote_image_get_remote_image_submit($form, $form_state) {
$return = array();
$return['remote_image_url_field'] = $form_state['values']['remote_image_url_field'];
$return['remote_image_image_field'] = $form_state['values']['remote_image_image_field'];
$return['remote_image_filename_field'] = $form_state['values']['remote_image_filename_field'];
$return['remote_image_file_path'] = $form_state['values']['remote_image_file_path'];
$return['remote_image_replace_existing'] = $form_state['values']['remote_image_replace_existing'];
return $return;
}
function remote_image_get_remote_image(&$node, $context) {
$url_field = $context['remote_image_url_field'];
$image_field = $context['remote_image_image_field'];
$filename_field = $context['remote_image_filename_field'];
$wrapper = entity_metadata_wrapper('node', $node);
$image = $wrapper->$image_field->value();
if (!empty($image['uri']) && ($context['remote_image_replace_existing'] == 0)) {
return;
}
$url = $wrapper->$url_field->value();
if (isset($url['url'])) {
$options = array(
'max_redirects' => 6,
'timeout' => 60.0,
);
$result = drupal_http_request($url['url'], $options);
if ($result->code == 200) {
$filename = $wrapper->$filename_field->value();
$path = $context['remote_image_file_path'] . '/' . $filename;
$file_object = file_save_data($result->data, $path, FILE_EXISTS_REPLACE);
if ($file_object !== FALSE) {
$wrapper->$image_field->file->set($file_object);
$wrapper->save();
}
else {
drupal_set_message(t('Could not save image for @title', array('@title' => $wrapper->title->value())), 'status', FALSE);
}
}
else {
drupal_set_message(t('Could not get image from @path - error @err',
array('@path' => $url['url'], '@err' => $result->code)), 'status', FALSE);
}
}
}
function _remote_image_save_file($data, $path) {
}