-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_embed_aol.module
executable file
·195 lines (174 loc) · 5.84 KB
/
video_embed_aol.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
<?php
/**
* @file
* The main module file for video_embed_aol.module.
*
* Stores various Drupal hook_implementations and helper functions.
*/
module_load_include('inc', 'video_embed_aol', 'video_embed_aol.video_embed_field');
/**
* This constant provides AOL ON single video api endpoint.
*/
define('VIDEO_EMBED_AOL_API_SINGLE_VIDEO_ENDPOINT', 'http://api.5min.com/video');
/**
* Implements hook_field_info_alter().
*/
function video_embed_aol_field_info_alter(&$info) {
if (isset($info['video_embed_field'])) {
$info['video_embed_field']['instance_settings']['use_id'] = 0;
}
}
/**
* Implements hook_form_field_ui_field_edit_form_alter().
*/
function video_embed_aol_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
$field = $form['#field'];
$instance = $form['#instance'];
if ($field['type'] == 'video_embed_field') {
$form['instance']['settings']['use_id'] = array(
'#type' => 'radios',
'#title' => t('AOL: Use video ID instead of URL'),
'#default_value' => $instance['settings']['use_id'],
'#options' => array(t('No'), t('Yes')),
'#description' => t('AOI ON videos provider only.'),
);
}
}
/**
* Custom function to get video data from aol network.
*
* @param int $video_id
* Video unique identifier.
* @param array $settings
* The request settings.
*
* @link https://support.aolonnetwork.com/API/VideoSeed.
*
* @return mixed
* Video info if request succeed.
*/
function _video_embed_aol_get_video_info($video_id, array $settings = array()) {
$cached = &drupal_static(__FUNCTION__ . $video_id, '');
if (!empty($cached)) {
return $cached;
}
$url = VIDEO_EMBED_AOL_API_SINGLE_VIDEO_ENDPOINT . '/' . $video_id . '/info.json';
foreach ($settings as &$value) {
if ($value <= 1 && is_numeric($value)) {
$value = var_export((bool) $value, TRUE);
}
}
$query_path = url($url, array('query' => $settings));
$result = drupal_http_request($query_path, array('timeout' => '5'));
if ($result->code == '200') {
$data = drupal_json_decode($result->data);
if (isset($data['api-info'])) {
$cached = $item_info = reset($data['items']);
return $item_info;
}
return FALSE;
}
return FALSE;
}
/**
* Helper function to retrieve data from AOL ON for a given video URL.
*
* @param string $url
* The video page url.
*
* @return mixed
* Array of video info or FALSE if can't fetch the info.
*/
function _video_embed_aol_extract_aol_video_data_from_url($url, $settings = array()) {
$possible_video_id = _video_embed_aol_extract_aol_video_id_from_url($url);
$video_info = _video_embed_aol_get_video_info($possible_video_id, $settings);
return $video_info;
}
/**
* Helper function to extract AOL ON video ID for a given video URL.
*
* @param string $url
* The video URL.
*
* @return bool|mixed
* The AOL ON video ID or FALSE if can't extract video ID.
*/
function _video_embed_aol_extract_aol_video_id_from_url($url) {
$parsed = @parse_url($url);
if (!empty($parsed['path'])) {
$exploded_path = explode('-', $parsed['path']);
$possible_video_id = end($exploded_path);
if (is_numeric($possible_video_id)) {
return $possible_video_id;
}
}
return FALSE;
}
/**
* Implements hook_field_widget_form_alter().
*/
function video_embed_aol_field_widget_form_alter(&$element, &$form_state, $context) {
if ($context['field']['type'] == 'video_embed_field') {
$instance = $context['instance'];
$element['video_url']['#element_validate'][] = 'video_embed_aol_video_url_field_validate';
if ($instance['settings']['use_id']) {
$video_info = array('id' => '');
if (isset($context['items'][$context['delta']]['video_data'])) {
$video_info = unserialize($context['items'][$context['delta']]['video_data']);
}
$element['video_id'] = array(
'#type' => 'textfield',
'#title' => t('AOL Video ID'),
'#element_validate' => array('video_embed_aol_video_id_field_validate'),
'#default_value' => $video_info['id'],
'#field' => $context['field'],
'#instance' => $instance,
);
hide($element['video_url']);
}
}
}
/**
* Validate function.
*
* Set the video URL if field use video ID instead of full URL.
*/
function video_embed_aol_video_id_field_validate($element, &$form_state, $form) {
$video_id = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
if (empty($video_id)) {
return;
}
$video_info = _video_embed_aol_get_video_info($video_id);
if ($video_info) {
$video_url_element_parents = array('#parents' => $element['#parents']);
$video_url_element_parents['#parents'][count($element['#parents']) - 1] = 'video_url';
form_set_value($video_url_element_parents, $video_info['player']['url'], $form_state);
}
else {
drupal_set_message(t('Field %name: enable to get information from AOL.
Please, try again. ', array('%name' => $element['#title'])), 'error');
}
}
/**
* Validate function.
*
* Set the video URL if field use video ID instead of full URL.
*/
function video_embed_aol_video_url_field_validate($element, &$form_state, $form) {
$video_url = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
if (($handler = video_embed_get_handler($video_url)) && $handler['name'] == 'aol') {
$parsed_video_url = @parse_url($video_url);
if ($video_url) {
// Accept only path like
// http://on.aol.com/video/reasons-for-people-getting-bored-in-relationships-517666458.
// Maybe some other rules?
$path = trim($parsed_video_url['path'], ' /');
$exploded_path = explode('/', $path);
if (count($exploded_path) >= 2) {
if ($exploded_path[0] != 'video') {
form_error($element, t('The video path is not supported.'));
}
}
}
}
}