video_embed_field.devel_generate.inc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * @file
  4. * Devel generate support for video_embed_field module.
  5. */
  6. // The Youtube’s API url.
  7. define('YT_API_URL', 'http://gdata.youtube.com/feeds/api/videos?q=');
  8. /**
  9. * Devel generate plugin definition.
  10. */
  11. function video_embed_field_devel_generate($object, $field, $instance, $bundle) {
  12. if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
  13. return devel_generate_multiple('_video_embed_field_devel_generate', $object, $field, $instance, $bundle);
  14. }
  15. else {
  16. return _video_embed_field_devel_generate($object, $field, $instance, $bundle);
  17. }
  18. }
  19. /**
  20. * Generates a random video_embed_field item.
  21. *
  22. * @param object $object
  23. * The devel_generate object.
  24. * @param array $field
  25. * The field definition.
  26. * @param array $instance
  27. * The field instance definition.
  28. * @param array $bundle
  29. * The bundle definition.
  30. *
  31. * @return array
  32. * The video_embed_field item.
  33. */
  34. function _video_embed_field_devel_generate($object, $field, $instance, $bundle) {
  35. $video = video_embed_field_retrieve_video();
  36. $object_field = array();
  37. $object_field['video_url'] = $video['video_url'];
  38. if ($instance['settings']['description_field']) {
  39. $object_field['description'] = $video['description'];
  40. }
  41. return $object_field;
  42. }
  43. /**
  44. * Retrieves a random youtube video info from the bunch.
  45. *
  46. * @return array
  47. * The video definition.
  48. */
  49. function video_embed_field_retrieve_video() {
  50. $videos = video_embed_field_generate_videos();
  51. return $videos[array_rand($videos)];
  52. }
  53. /**
  54. * Generates a pseudo random bunch of youtube videos.
  55. *
  56. * @return array
  57. * A bunch of youtube videos.
  58. */
  59. function video_embed_field_generate_videos() {
  60. $videos = &drupal_static(__FUNCTION__);
  61. if (!isset($videos)) {
  62. $videos = array();
  63. // Create random video seed.
  64. $video_id = user_password(2);
  65. // Using cURL php extension to make the request to youtube API.
  66. $ch = curl_init();
  67. curl_setopt($ch, CURLOPT_URL, YT_API_URL . $video_id);
  68. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  69. // $feed holds a rss feed xml returned by youtube API.
  70. $feed = curl_exec($ch);
  71. curl_close($ch);
  72. // Using SimpleXML to parse youtube’s feed.
  73. $xml = simplexml_load_string($feed);
  74. foreach ($xml->entry as $entry) {
  75. $videos[] = array(
  76. 'video_url' => $entry->children('media', TRUE)->group->player->attributes()->url,
  77. 'description' => $entry->title,
  78. );
  79. }
  80. if (empty($videos)) {
  81. video_embed_field_generate_videos();
  82. }
  83. }
  84. return $videos;
  85. }