Projet

Général

Profil

Paste
Télécharger (5,56 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / media_youtube / themes / media_youtube.theme.inc @ 18596a08

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
  $parts = $wrapper->get_parameters();
16
  if (isset($parts['v'])) {
17
    $variables['embed_type'] = 'video';
18
    $variables['video_id'] = check_plain($parts['v']);
19
    $embed_path = '/embed/' . $variables['video_id'];
20
  }
21
  elseif (isset($parts['l'])) {
22
    $variables['embed_type'] = 'playlist';
23
    $variables['video_id'] = check_plain($parts['l']);
24
    $embed_path = '/embed/videoseries';
25
  }
26

    
27
  // Checked existing function.
28
  if(function_exists('file_uri_to_object')) {
29
    // Make the file object available.
30
    $file_object = file_uri_to_object($variables['uri']);
31
  }
32
  else {
33
    $file_object = media_youtube_file_uri_to_object($variables['uri']);
34
  }
35

    
36
  // Parse options and build the query string. Only add the option to the query
37
  // array if the option value is not default. Be careful, depending on the
38
  // wording in media_youtube.formatters.inc, TRUE may be query=0.
39
  // @see https://developers.google.com/youtube/player_parameters#Parameters
40
  $query = array();
41

    
42
  // Make css z-index work with flash object. Must be the first parameter.
43
  $query['wmode'] = 'opaque';
44

    
45
  // These queries default to 0. If the option is true, set value to 1.
46
  foreach (array('autoplay', 'enablejsapi', 'loop', 'modestbranding') as $option) {
47
    if ($variables['options'][$option]) {
48
      $query[$option] = 1;
49
    }
50
  }
51
  if ($variables['options']['enablejsapi']) {
52
    // Add a query ID and identical html ID if js API is set.
53
    $query['playerapiid'] = drupal_html_id('media-youtube-' . $variables['video_id']);
54
    $variables['api_id_attribute'] = 'id="' . $query['playerapiid'] . '" ';
55

    
56
    // Add the origin for improved security
57
    $variables['options']['origin'] ? $query['origin'] = $variables['options']['origin'] : '';
58
  }
59
  else {
60
    $variables['api_id_attribute'] = NULL;
61
  }
62

    
63
  // Currently, loop only works with a playlist. Make fake playlist out of a
64
  // single video.
65
  // @see https://developers.google.com/youtube/player_parameters#loop
66
  if ($variables['options']['loop']) {
67
    $query['playlist'] = $variables['video_id'];
68
  }
69

    
70
  // These queries default to 1. If the option is false, set value to 0.
71
  foreach (array('rel', 'showinfo') as $option) {
72
    if (!$variables['options'][$option]) {
73
      $query[$option] = 0;
74
    }
75
  }
76

    
77
  // These queries default to 1. Option wording is reversed, so if the option
78
  // is true, set value to 0.
79
  // (None right now.)
80

    
81
  // Strings.
82
  if ($variables['options']['theme'] != 'dark') {
83
    $query['theme'] = $variables['options']['theme'];
84
  }
85
  if ($variables['options']['color'] != 'red') {
86
    $query['color'] = $variables['options']['color'];
87
  }
88
  if ($variables['options']['autohide'] != '2') {
89
    $query['autohide'] = $variables['options']['autohide'];
90
  }
91
  if ($variables['options']['captions'] > '0') {
92
    // Add captions parameters to the query.
93
    $query['cc_load_policy'] = '1';
94
    if ($variables['options']['captions'] > '1') {
95
      global $language;
96
      // We can specify a default language for captions.
97
      if ($language->language != LANGUAGE_NONE) {
98
        $query['cc_lang_pref'] = $language->language;
99
      }
100
    }
101
  }
102

    
103
  if ($variables['options']['protocol_specify']) {
104
    $protocol = $variables['options']['protocol'];
105
  }
106
  else {
107
    $protocol = 'https:';
108
  }
109

    
110
  // Non-query options.
111
  if ($variables['options']['nocookie']) {
112
    $url_base = 'youtube-nocookie.com';
113
  }
114
  else {
115
    $url_base = 'youtube.com';
116
  }
117

    
118
  // Add some options as their own template variables.
119
  foreach (array('width', 'height') as $theme_var) {
120
    $variables[$theme_var] = $variables['options'][$theme_var];
121
  }
122

    
123
  // Do something useful with the overridden attributes from the file
124
  // object. We ignore alt and style for now.
125
  if (isset($variables['options']['attributes']['class'])) {
126
    if (is_array($variables['options']['attributes']['class'])) {
127
      $variables['classes_array'] = array_merge($variables['classes_array'], $variables['options']['attributes']['class']);
128
    }
129
    else {
130
      // Provide nominal support for Media 1.x.
131
      $variables['classes_array'][] = $variables['options']['attributes']['class'];
132
    }
133
  }
134

    
135
  // Add template variables for accessibility.
136
  $variables['title'] = check_plain($file_object->filename);
137
  // @TODO: Find an easy/ not too expensive way to get the YouTube description
138
  // to use for the alternative content.
139
  $variables['alternative_content'] = t('Video of @title', array('@title' => $variables['title']));
140

    
141
  if (isset($parts['l'])) {
142
    $query['list'] = $parts['l'];
143
  }
144
  // Build the iframe URL with options query string.
145
  $variables['url'] = url($protocol . '//www.' . $url_base . $embed_path, array('query' => $query, 'external' => TRUE));
146
}
147

    
148
/**
149
 * Helping function.
150
 */
151
function media_youtube_file_uri_to_object($uri) {
152
  $uri = file_stream_wrapper_uri_normalize($uri);
153
  $files = entity_load('file', FALSE, array('uri' => $uri));
154
  $file = !empty($files) ? reset($files) : FALSE;
155
  if (!$file) {
156
    global $user;
157
    $file = new stdClass();
158
    $file->uid = $user->uid;
159
    $file->filename = basename($uri);
160
    $file->uri = $uri;
161
    $file->filemime = file_get_mimetype($uri);
162
    // This is gagged because some uris will not support it.
163
    $file->filesize = @filesize($uri);
164
    $file->timestamp = REQUEST_TIME;
165
    $file->status = FILE_STATUS_PERMANENT;
166
  }
167
  return $file;
168
}