Projet

Général

Profil

Paste
Télécharger (6,19 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / media_youtube / themes / media_youtube.theme.inc @ 3acd948f

1
<?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
  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
  }
28
  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
  }
34

    
35
  // 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

    
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
  //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
  // 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
    // Add the origin for improved security
73
    $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
  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

    
119
  if ($variables['options']['protocol_specify']) {
120
    $protocol = $variables['options']['protocol'];
121
  }
122
  else {
123
    $protocol = 'https:';
124
  }
125

    
126
  // Non-query options.
127
  if ($variables['options']['nocookie']) {
128
    $url_base = 'youtube-nocookie.com';
129
  }
130
  else {
131
    $url_base = 'youtube.com';
132
  }
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
  if (isset($parts['l'])) {
158
    $query['list'] = $parts['l'];
159
  }
160
  // Build the iframe URL with options query string.
161
  $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
}