array( 'name' => t('YouTube videos'), 'class' => 'MediaYouTubeStreamWrapper', 'description' => t('Videos provided by YouTube.'), 'type' => STREAM_WRAPPERS_READ_VISIBLE, ), ); } function media_youtube_media_format_form_prepare_alter(&$form, &$form_state, $media) { $settings = array('autosubmit' => ($media->type == "video")); drupal_add_js(array('media_format_form' => $settings), 'setting'); } /** * Implements hook_theme(). */ function media_youtube_theme($existing, $type, $theme, $path) { return array( 'media_youtube_preview_style' => array( 'variables' => array('style_name' => NULL), 'file' => 'media_youtube.theme.inc', 'path' => $path . '/includes/themes', ), 'media_youtube_field_formatter_styles' => array( 'variables' => array('element' => NULL, 'style' => NULL), 'file' => 'media_youtube.theme.inc', 'path' => $path . '/includes/themes', ), 'media_youtube_video' => array( 'variables' => array('uri' => NULL, 'width' => NULL, 'height' => NULL, 'autoplay' => NULL, 'fullscreen' => NULL), 'file' => 'media_youtube.theme.inc', 'path' => $path . '/includes/themes', 'template' => 'media-youtube-video', ), 'media_youtube_embed' => array( 'variables' => array('style_name' => NULL, 'uri' => NULL, 'alt' => NULL, 'title' => NULL), 'file' => 'media_youtube.theme.inc', 'path' => $path . '/includes/themes', ), 'media_youtube_styles' => array( 'variables' => array('element' => NULL, 'style' => NULL), 'file' => 'media_youtube.theme.inc', 'path' => $path . '/includes/themes', ), ); } /** * Implementation of Styles module hook_styles_register(). */ function media_youtube_styles_register() { return array( 'MediaYouTubeStyles' => array( 'field_types' => 'file', 'name' => t('MediaYouTube'), 'description' => t('Media YouTube styles.'), 'path' => drupal_get_path('module', 'media_youtube') .'/includes', 'file' => 'media_youtube.styles.inc', ), ); } /** * Implements hook_styles_containers(). (Deprecated in version 2) */ function media_youtube_styles_containers() { return array( 'file' => array( 'containers' => array( 'media_youtube' => array( 'label' => t('YouTube Styles'), 'data' => array( 'streams' => array( 'youtube', ), 'mimetypes' => array( 'video/youtube', ), ), 'weight' => 0, 'filter callback' => 'media_youtube_formatter_filter', 'themes' => array( 'field_formatter_styles' => 'media_youtube_field_formatter_styles', 'styles' => 'media_youtube_styles', 'preview' => 'media_youtube_preview_style', ), 'description' => t('YouTube Styles will display embedded YouTube videos and thumbnails to your choosing, such as by resizing, setting colors, and autoplay. You can !manage.', array('!manage' => l(t('manage your YouTube styles here'), 'admin/config/media/media-youtube-styles'))), ), ), ), ); } function media_youtube_formatter_filter($variables) { if (isset($variables['object'])) { $object = $variables['object']; return (file_uri_scheme($object->uri) == 'youtube') && ($object->filemime == 'video/youtube'); } } /** * Implementation of the File Styles module's hook_file_styles_filter(). */ function media_youtube_file_styles_filter($object) { if ((file_uri_scheme($object->uri) == 'youtube') && ($object->filemime == 'video/youtube')) { return 'media_youtube'; } } /** * Implements hook_styles_styles(). */ function media_youtube_styles_styles() { $styles = array( 'file' => array( 'containers' => array( 'media_youtube' => array( 'styles' => array( 'youtube_thumbnail' => array( 'name' => 'youtube_thumbnail', 'effects' => array( array('label' => t('Thumbnail'), 'name' => 'thumbnail', 'data' => array('thumbnail' => 1)), array('label' => t('Resize'), 'name' => 'resize', 'data' => array('width' => 100, 'height' => 75)), ), ), 'youtube_preview' => array( 'name' => 'youtube_preview', 'effects' => array( array('label' => t('Autoplay'), 'name' => 'autoplay', 'data' => array('autoplay' => 1)), array('label' => t('Resize'), 'name' => 'resize', 'data' => array('width' => 220, 'height' => 165)), ), ), 'youtube_full' => array( 'name' => 'youtube_full', 'effects' => array( array('label' => t('Autoplay'), 'name' => 'autoplay', 'data' => array('autoplay' => 0)), array('label' => t('Resize'), 'name' => 'resize', 'data' => array('width' => 640, 'height' => 480)), array('label' => t('Full screen'), 'name' => 'fullscreen', 'data' => array('fullscreen' => 1)), ), ), ), ), ), ), ); // Allow any image style to be applied to the thumbnail. foreach (image_styles() as $style_name => $image_style) { $styles['file']['containers']['media_youtube']['styles']['youtube_thumbnail_' . $style_name] = array( 'name' => 'youtube_thumbnail_' . $style_name, 'image_style' => $style_name, 'effects' => array( array('label' => t('Thumbnail'), 'name' => 'thumbnail', 'data' => array('thumbnail' => 1)), ), ); } return $styles; } /** * Implements hook_styles_presets(). */ function media_youtube_styles_presets() { $presets = array( 'file' => array( 'square_thumbnail' => array( 'media_youtube' => array( 'youtube_thumbnail_square_thumbnail', ), ), 'thumbnail' => array( 'media_youtube' => array( 'youtube_thumbnail', ), ), 'small' => array( 'media_youtube' => array( 'youtube_preview', ), ), 'large' => array( 'media_youtube' => array( 'youtube_full', ), ), 'original' => array( 'media_youtube' => array( 'youtube_full', ), ), ), ); return $presets; } /** * Implements hook_media_parse(). * * @todo: this might be deprecated now that we have media_internet, * but the hook is still being called in a couple places in media. */ function media_youtube_media_parse($url, $options = array()) { $scheme = 'youtube://'; preg_match('@youtube\.com/watch[#\?]v=([^"\& ]+)@i', $url, $matches); if (isset($matches[1])) { return file_stream_wrapper_uri_normalize($scheme . 'v/' . $matches[1]); } // @TODO: Validate for malformed youtube urls. } /** * Implements hook_media_internet_providers(); */ function media_youtube_media_internet_providers() { $path = drupal_get_path('module', 'media_youtube'); return array( 'MediaInternetYouTubeHandler' => array( 'title' => 'youtube', 'image' => $path . '/images/stream-youtube.png' ), ); } class MediaInternetYouTubeHandler extends MediaInternetBaseHandler { public function claim($embedCode) { if (media_youtube_media_parse($embedCode)) { return TRUE; } } public function validate() { // @todo Media module currently fails when two files try to have the same // URI, so catch that in the validation step. Some day, it would be nice // to allow it, however. See http://drupal.org/node/952422. $uri = media_youtube_media_parse($this->embedCode); $existing_files = file_load_multiple(array(), array('uri' => $uri)); if (count($existing_files)) { throw new MediaInternetValidationException(t('You have entered a URL for a video that is already in your library.')); } } public function save() { $file = $this->getFileObject(); file_save($file); return $file; } public function getFileObject() { $uri = media_youtube_media_parse($this->embedCode); //@todo: this is terribly broken in some ways because the function is really // made for local files which are 'real' return file_uri_to_object($uri); } /** * Returns information about the media. See http://video.search.yahoo.com/mrss. * * @return * If ATOM+MRSS information is available, a SimpleXML element containing * ATOM and MRSS elements, as per those respective specifications. * * @todo Would be better for the return value to be an array rather than a * SimpleXML element, but media_retrieve_xml() needs to be upgraded to * handle namespaces first. */ public function getMRSS() { $uri = media_youtube_media_parse($this->embedCode); $video_id = arg(1, file_uri_target($uri)); $rss_url = url('http://gdata.youtube.com/feeds/api/videos/' . $video_id, array('query' => array('v' => '2'))); // @todo Use media_retrieve_xml() once it's upgraded to include elements // from all namespaces, not just the document default namespace. $entry = simplexml_load_file($rss_url); return $entry; } /** * Returns information about the media. See http://www.oembed.com/. * * @return * If oEmbed information is available, an array containing 'title', 'type', * 'url', and other information as specified by the oEmbed standard. * Otherwise, NULL. */ public function getOEmbed() { $uri = media_youtube_media_parse($this->embedCode); $external_url = drupal_realpath($uri); $oembed_url = url('http://www.youtube.com/oembed', array('query' => array('url' => $external_url, 'format' => 'json'))); $response = drupal_http_request($oembed_url); if (!isset($response->error)) { return drupal_json_decode($response->data); } } }