-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfieldyoutube.module
More file actions
executable file
·236 lines (209 loc) · 7 KB
/
fieldyoutube.module
File metadata and controls
executable file
·236 lines (209 loc) · 7 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
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
<?php
// $Id$
/**
* @file
* Youtube field hooks, and functionalities
*/
/**
* Implements hook_field_info().
*/
function fieldyoutube_field_info() {
return array(
'youtube' => array(
'label' => 'YouTube',
'description' => t('This field stores and renderes YouTube videos.'),
'default_widget' => 'text_textfield',
'default_formatter' => 'default',
'property_type' => 'text',
),
);
}
function fieldyoutube_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
foreach($items as $delta => $item) {
if(isset($item['youtube'])) {
$youtubeID = parseURL($item['youtube']);
$headers = get_headers('http://gdata.youtube.com/feeds/api/videos/' . $youtubeID);
if (!strpos($headers[0], '200')) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'fieldyoutube_invalid_id',
'message' => t('%name: You have entered an invalid YouTube URL.', array('%name' => $instance['label'])),
);
}
}
}
}
function fieldyoutube_field_widget_error($element, $error, $form, &$form_state) {
form_error($element, $error['message']);
}
/**
* Implements hook_content_is_empty().
*/
function fieldyoutube_field_is_empty($item, $field) {
if (empty($item['youtube'])) {
return TRUE;
}
return FALSE;
}
/**
* Implements hook_field_formatter_info().
*
*/
function fieldyoutube_field_formatter_info() {
$formats = array(
'fieldyoutube_embed' => array(
'label' => t('Default Video @ 640 x 360'),
'description' => t('Will show embeded YouTube on your node'),
'field types' => array('youtube'),
),
'fieldyoutube_embed_desired_width_autoplay' => array(
'label' => t('Video @ Specified Size Autoplay'),
'description' => t('Will show embeded YouTube on your node'),
'field types' => array('youtube'),
),
'fieldyoutube_embed_desired_width_no_autoplay' => array(
'label' => t('Video @ Specified Size No Autoplay'),
'description' => t('Will show embeded YouTube on your node'),
'field types' => array('youtube'),
),
'fieldyoutube_thumblink' => array(
'label' => t('Image with link to video'),
'description' => t('Will show image thumbnail linked to YouTube video'),
'field types' => array('youtube'),
),
'fieldyoutube_plain' => array(
'label' => t('Text link to video'),
'description' => t('Will show plain text link to YouTube video'),
'field types' => array('youtube'),
),
);
return $formats;
}
/**
* Implements hook_field_formatter_view().
*/
function fieldyoutube_field_formatter_view($object_type, $object, $field, $instance, $langcode, $items, $display) {
$res = explode('x', $instance['widget']['settings']['resolution']);
$element = array();
switch ($display['type']) {
case 'fieldyoutube_embed':
foreach ($items as $delta => $item) {
$youtube = new YouTube($item['youtube']);
//$output = $youtube->EmbedVideo($res[0], $res[1]);
$output = $youtube->EmbedVideo(640, 360);
$element[$delta] = array('#markup' => $output);
}
break;
case 'fieldyoutube_embed_desired_width_autoplay':
foreach ($items as $delta => $item) {
$youtube = new YouTube($item['youtube']);
$output = $youtube->EmbedVideo($res[0], $res[1]);
//$output = $youtube->EmbedVideo(640, 360);
$element[$delta] = array('#markup' => $output);
}
break;
case 'fieldyoutube_embed_desired_width_no_autoplay':
foreach ($items as $delta => $item) {
$youtube = new YouTube($item['youtube']);
$output = $youtube->EmbedVideo($res[0], $res[1], null, 1, 0);
//$output = $youtube->EmbedVideo(640, 360);
$element[$delta] = array('#markup' => $output);
}
break;
case 'fieldyoutube_thumblink':
foreach ($items as $delta => $item) {
$youtube = new YouTube($item['youtube']);
$output = l( $youtube->ShowImg(), $item['youtube'], array('html' => TRUE) );
$element[$delta] = array('#markup' => $output);
break;
}
break;
case 'fieldyoutube_plain':
foreach ($items as $delta => $item) {
$output = l( t('Click here to watch this video.'), $item['youtube']);
$element[$delta] = array('#markup' => $output);
}
break;
}
return $element;
}
/**
* Implements hook_field_widget_info().
*/
function fieldyoutube_field_widget_info() {
return array(
'fieldyoutube_textfield' => array(
'label' => t('Text field'),
'field types' => array('youtube'),
'settings' => array('size' => 60),
),
);
}
/**
* Implements hook_field_widget_settings_form().
*/
function fieldyoutube_field_widget_settings_form($field, $instance) {
$widget = $instance['widget'];
$settings = $widget['settings'];
$form['size'] = array(
'#type' => 'textfield',
'#title' => t('Size of textfield'),
'#default_value' => $settings['size'],
'#required' => TRUE,
'#element_validate' => array('_element_validate_integer_positive'),
);
$form['resolution'] = array(
'#type' => 'textfield',
'#title' => t('Resolution of rendered video'),
'#default_value' => (isset($settings['resolution'])) ? $settings['resolution'] : '425x344',
'#required' => FALSE,
);
// TODO: Quality switcher
return $form;
}
/**
* Implements hook_field_widget_form().
*/
function fieldyoutube_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $base) {
$element = $base;
$element['youtube'] = $base + array(
'#type' => 'textfield',
'#default_value' => isset($items[$delta]['youtube']) ? $items[$delta]['youtube'] : NULL,
'#size' => $instance['widget']['settings']['size'],
'#prefix' => '<div class="text-full-wrapper">',
'#suffix' => '</div>',
);
return $element;
}
/**
* Implements hook_node_insert().
*/
function fieldyoutube_node_presave($node) {
if($node->type == 'media_video') {
$path = GetImg( $node->field_youtube['und'][0]['youtube'] ,0);
$name = pathinfo($path);
$fid = '';
$img_import = write_image_info($path, $name['filename'], $fid, parseURL($node->field_youtube['und'][0]['youtube']));
$node->field_video_preview['und'][0]['fid'] = $fid;
$node->field_video_preview['und'][0]['description'] = '';
$node->field_video_preview['und'][0]['title'] = '';
$node->field_video_preview['und'][0]['alt'] = '';
}
}
function GetImg($url = '', $imgid = 1) {
$videoid = parseURL($url);
if (!$videoid) $videoid = $url;
return "http://img.youtube.com/vi/".$videoid."/".$imgid.".jpg";
}
function parseURL($url) {
if (preg_match('/watch\?v\=([A-Za-z0-9_-]+)/', $url, $matches))
return $matches[1];
else
return FALSE;
}
function write_image_info($remote_image_path, $remote_image_name, &$fid, $vid) {
$image_path = $remote_image_path;
$remote_image_name = $remote_image_name . '.jpg';
$file_temp = file_get_contents($remote_image_path);
$file_temp = file_save_data($file_temp, file_default_scheme() . '://' . $vid . $remote_image_name, FILE_EXISTS_RENAME);
$fid = $file_temp->fid;
}