-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvideo_embed_field.devel_generate.inc
93 lines (85 loc) · 2.48 KB
/
video_embed_field.devel_generate.inc
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
<?php
/**
* @file
* Devel generate support for video_embed_field module.
*/
// The Youtube’s API url.
define('YT_API_URL', 'https://www.youtube.com/feeds/videos.xml?user=backdropcms');
/**
* Devel generate plugin definition.
*/
function video_embed_field_devel_generate($object, $field, $instance, $bundle) {
if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
return devel_generate_multiple('_video_embed_field_devel_generate', $object, $field, $instance, $bundle);
}
else {
return _video_embed_field_devel_generate($object, $field, $instance, $bundle);
}
}
/**
* Generates a random video_embed_field item.
*
* @param object $object
* The devel_generate object.
* @param array $field
* The field definition.
* @param array $instance
* The field instance definition.
* @param array $bundle
* The bundle definition.
*
* @return array
* The video_embed_field item.
*/
function _video_embed_field_devel_generate($object, $field, $instance, $bundle) {
$video = video_embed_field_retrieve_video();
$object_field = array();
$object_field['video_url'] = $video['video_url'];
if ($instance['settings']['description_field']) {
$object_field['description'] = $video['description'];
}
return $object_field;
}
/**
* Retrieves a random youtube video info from the bunch.
*
* @return array
* The video definition.
*/
function video_embed_field_retrieve_video() {
$videos = video_embed_field_generate_videos();
return $videos[array_rand($videos)];
}
/**
* Generates a pseudo random bunch of youtube videos.
*
* @return array
* A bunch of youtube videos.
*/
function video_embed_field_generate_videos() {
$videos = &backdrop_static(__FUNCTION__);
if (!isset($videos)) {
$videos = array();
// Using cURL php extension to make the request to youtube API.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, YT_API_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $feed holds a rss feed xml returned by youtube API.
$feed = curl_exec($ch);
curl_close($ch);
// Using SimpleXML to parse youtube’s feed.
$xml = simplexml_load_string($feed);
$json = json_encode($xml);
$array = json_decode($json, TRUE);
foreach ($array['entry'] as $entry) {
$videos[] = array(
'video_url' => $entry['link']['@attributes']['href'],
'description' => $entry['title'],
);
}
if (empty($videos)) {
video_embed_field_generate_videos();
}
}
return $videos;
}