video_embed_field.devel_generate.inc in Video Embed Field 7.2
Devel generate support for video_embed_field module.
File
video_embed_field.devel_generate.inc
View source
<?php
define('YT_API_URL', 'http://gdata.youtube.com/feeds/api/videos?q=');
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);
}
}
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;
}
function video_embed_field_retrieve_video() {
$videos = video_embed_field_generate_videos();
return $videos[array_rand($videos)];
}
function video_embed_field_generate_videos() {
$videos =& drupal_static(__FUNCTION__);
if (!isset($videos)) {
$videos = array();
$video_id = user_password(2);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, YT_API_URL . $video_id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$feed = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($feed);
foreach ($xml->entry as $entry) {
$videos[] = array(
'video_url' => $entry
->children('media', TRUE)->group->player
->attributes()->url,
'description' => $entry->title,
);
}
if (empty($videos)) {
video_embed_field_generate_videos();
}
}
return $videos;
}