1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file media_youtube/themes/media_youtube.theme.inc
|
5 |
|
|
*
|
6 |
|
|
* Theme and preprocess functions for Media: YouTube.
|
7 |
|
|
*/
|
8 |
|
|
|
9 |
|
|
/**
|
10 |
|
|
* Preprocess function for theme('media_youtube_video').
|
11 |
|
|
*/
|
12 |
|
|
function media_youtube_preprocess_media_youtube_video(&$variables) {
|
13 |
|
|
// Build the URI.
|
14 |
|
|
$wrapper = file_stream_wrapper_get_instance_by_uri($variables['uri']);
|
15 |
3acd948f
|
Assos Assos
|
if ($wrapper instanceof MediaReadOnlyStreamWrapper) {
|
16 |
|
|
$parts = $wrapper->get_parameters();
|
17 |
|
|
if (isset($parts['v'])) {
|
18 |
|
|
$variables['embed_type'] = 'video';
|
19 |
|
|
$variables['video_id'] = check_plain($parts['v']);
|
20 |
|
|
$embed_path = '/embed/' . $variables['video_id'];
|
21 |
|
|
}
|
22 |
|
|
elseif (isset($parts['l'])) {
|
23 |
|
|
$variables['embed_type'] = 'playlist';
|
24 |
|
|
$variables['video_id'] = check_plain($parts['l']);
|
25 |
|
|
$embed_path = '/embed/videoseries';
|
26 |
|
|
}
|
27 |
18596a08
|
Assos Assos
|
}
|
28 |
3acd948f
|
Assos Assos
|
else {
|
29 |
|
|
// This happens when stream wrappers are not yet initialized. This is
|
30 |
|
|
// normally only encountered when creating content during profile install
|
31 |
|
|
// using drush make. At that point, video_id is irrelevant anyway.
|
32 |
|
|
$variables['video_id'] = '';
|
33 |
18596a08
|
Assos Assos
|
}
|
34 |
85ad3d82
|
Assos Assos
|
|
35 |
18596a08
|
Assos Assos
|
// Checked existing function.
|
36 |
|
|
if(function_exists('file_uri_to_object')) {
|
37 |
|
|
// Make the file object available.
|
38 |
|
|
$file_object = file_uri_to_object($variables['uri']);
|
39 |
|
|
}
|
40 |
|
|
else {
|
41 |
|
|
$file_object = media_youtube_file_uri_to_object($variables['uri']);
|
42 |
|
|
}
|
43 |
85ad3d82
|
Assos Assos
|
|
44 |
|
|
// Parse options and build the query string. Only add the option to the query
|
45 |
|
|
// array if the option value is not default. Be careful, depending on the
|
46 |
|
|
// wording in media_youtube.formatters.inc, TRUE may be query=0.
|
47 |
|
|
// @see https://developers.google.com/youtube/player_parameters#Parameters
|
48 |
|
|
$query = array();
|
49 |
|
|
|
50 |
|
|
// Make css z-index work with flash object. Must be the first parameter.
|
51 |
|
|
$query['wmode'] = 'opaque';
|
52 |
|
|
|
53 |
3acd948f
|
Assos Assos
|
//YouTube video controls, on or off.
|
54 |
|
|
if (isset($variables['options']['controls'])) {
|
55 |
|
|
//on or off (TRUE/FALSE) depending on what is stored in $variables['options']['controls'].
|
56 |
|
|
$query['controls'] = $variables['options']['controls'];
|
57 |
|
|
} else {
|
58 |
|
|
//on
|
59 |
|
|
$query['controls'] = TRUE;
|
60 |
|
|
}
|
61 |
85ad3d82
|
Assos Assos
|
// These queries default to 0. If the option is true, set value to 1.
|
62 |
|
|
foreach (array('autoplay', 'enablejsapi', 'loop', 'modestbranding') as $option) {
|
63 |
|
|
if ($variables['options'][$option]) {
|
64 |
|
|
$query[$option] = 1;
|
65 |
|
|
}
|
66 |
|
|
}
|
67 |
|
|
if ($variables['options']['enablejsapi']) {
|
68 |
|
|
// Add a query ID and identical html ID if js API is set.
|
69 |
|
|
$query['playerapiid'] = drupal_html_id('media-youtube-' . $variables['video_id']);
|
70 |
|
|
$variables['api_id_attribute'] = 'id="' . $query['playerapiid'] . '" ';
|
71 |
|
|
|
72 |
70a4c29b
|
Assos Assos
|
// Add the origin for improved security
|
73 |
85ad3d82
|
Assos Assos
|
$variables['options']['origin'] ? $query['origin'] = $variables['options']['origin'] : '';
|
74 |
|
|
}
|
75 |
|
|
else {
|
76 |
|
|
$variables['api_id_attribute'] = NULL;
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
// Currently, loop only works with a playlist. Make fake playlist out of a
|
80 |
|
|
// single video.
|
81 |
|
|
// @see https://developers.google.com/youtube/player_parameters#loop
|
82 |
|
|
if ($variables['options']['loop']) {
|
83 |
|
|
$query['playlist'] = $variables['video_id'];
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
// These queries default to 1. If the option is false, set value to 0.
|
87 |
|
|
foreach (array('rel', 'showinfo') as $option) {
|
88 |
|
|
if (!$variables['options'][$option]) {
|
89 |
|
|
$query[$option] = 0;
|
90 |
|
|
}
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
// These queries default to 1. Option wording is reversed, so if the option
|
94 |
|
|
// is true, set value to 0.
|
95 |
|
|
// (None right now.)
|
96 |
|
|
|
97 |
|
|
// Strings.
|
98 |
|
|
if ($variables['options']['theme'] != 'dark') {
|
99 |
|
|
$query['theme'] = $variables['options']['theme'];
|
100 |
|
|
}
|
101 |
|
|
if ($variables['options']['color'] != 'red') {
|
102 |
|
|
$query['color'] = $variables['options']['color'];
|
103 |
|
|
}
|
104 |
|
|
if ($variables['options']['autohide'] != '2') {
|
105 |
|
|
$query['autohide'] = $variables['options']['autohide'];
|
106 |
|
|
}
|
107 |
70a4c29b
|
Assos Assos
|
if ($variables['options']['captions'] > '0') {
|
108 |
|
|
// Add captions parameters to the query.
|
109 |
|
|
$query['cc_load_policy'] = '1';
|
110 |
|
|
if ($variables['options']['captions'] > '1') {
|
111 |
|
|
global $language;
|
112 |
|
|
// We can specify a default language for captions.
|
113 |
|
|
if ($language->language != LANGUAGE_NONE) {
|
114 |
|
|
$query['cc_lang_pref'] = $language->language;
|
115 |
|
|
}
|
116 |
|
|
}
|
117 |
|
|
}
|
118 |
85ad3d82
|
Assos Assos
|
|
119 |
18596a08
|
Assos Assos
|
if ($variables['options']['protocol_specify']) {
|
120 |
|
|
$protocol = $variables['options']['protocol'];
|
121 |
85ad3d82
|
Assos Assos
|
}
|
122 |
|
|
else {
|
123 |
18596a08
|
Assos Assos
|
$protocol = 'https:';
|
124 |
85ad3d82
|
Assos Assos
|
}
|
125 |
|
|
|
126 |
18596a08
|
Assos Assos
|
// Non-query options.
|
127 |
|
|
if ($variables['options']['nocookie']) {
|
128 |
|
|
$url_base = 'youtube-nocookie.com';
|
129 |
85ad3d82
|
Assos Assos
|
}
|
130 |
|
|
else {
|
131 |
18596a08
|
Assos Assos
|
$url_base = 'youtube.com';
|
132 |
85ad3d82
|
Assos Assos
|
}
|
133 |
|
|
|
134 |
|
|
// Add some options as their own template variables.
|
135 |
|
|
foreach (array('width', 'height') as $theme_var) {
|
136 |
|
|
$variables[$theme_var] = $variables['options'][$theme_var];
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
// Do something useful with the overridden attributes from the file
|
140 |
|
|
// object. We ignore alt and style for now.
|
141 |
|
|
if (isset($variables['options']['attributes']['class'])) {
|
142 |
|
|
if (is_array($variables['options']['attributes']['class'])) {
|
143 |
|
|
$variables['classes_array'] = array_merge($variables['classes_array'], $variables['options']['attributes']['class']);
|
144 |
|
|
}
|
145 |
|
|
else {
|
146 |
|
|
// Provide nominal support for Media 1.x.
|
147 |
|
|
$variables['classes_array'][] = $variables['options']['attributes']['class'];
|
148 |
|
|
}
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
// Add template variables for accessibility.
|
152 |
|
|
$variables['title'] = check_plain($file_object->filename);
|
153 |
|
|
// @TODO: Find an easy/ not too expensive way to get the YouTube description
|
154 |
|
|
// to use for the alternative content.
|
155 |
|
|
$variables['alternative_content'] = t('Video of @title', array('@title' => $variables['title']));
|
156 |
|
|
|
157 |
18596a08
|
Assos Assos
|
if (isset($parts['l'])) {
|
158 |
|
|
$query['list'] = $parts['l'];
|
159 |
|
|
}
|
160 |
85ad3d82
|
Assos Assos
|
// Build the iframe URL with options query string.
|
161 |
18596a08
|
Assos Assos
|
$variables['url'] = url($protocol . '//www.' . $url_base . $embed_path, array('query' => $query, 'external' => TRUE));
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
/**
|
165 |
|
|
* Helping function.
|
166 |
|
|
*/
|
167 |
|
|
function media_youtube_file_uri_to_object($uri) {
|
168 |
|
|
$uri = file_stream_wrapper_uri_normalize($uri);
|
169 |
|
|
$files = entity_load('file', FALSE, array('uri' => $uri));
|
170 |
|
|
$file = !empty($files) ? reset($files) : FALSE;
|
171 |
|
|
if (!$file) {
|
172 |
|
|
global $user;
|
173 |
|
|
$file = new stdClass();
|
174 |
|
|
$file->uid = $user->uid;
|
175 |
|
|
$file->filename = basename($uri);
|
176 |
|
|
$file->uri = $uri;
|
177 |
|
|
$file->filemime = file_get_mimetype($uri);
|
178 |
|
|
// This is gagged because some uris will not support it.
|
179 |
|
|
$file->filesize = @filesize($uri);
|
180 |
|
|
$file->timestamp = REQUEST_TIME;
|
181 |
|
|
$file->status = FILE_STATUS_PERMANENT;
|
182 |
|
|
}
|
183 |
|
|
return $file;
|
184 |
85ad3d82
|
Assos Assos
|
} |