Projet

Général

Profil

Paste
Télécharger (9,48 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / media_youtube / includes / media_youtube.formatters.inc @ 3acd948f

1
<?php
2

    
3
/**
4
 * @file
5
 * File formatters for YouTube videos.
6
 */
7

    
8
/**
9
 * Implements hook_file_formatter_info().
10
 */
11
function media_youtube_file_formatter_info() {
12
  $formatters['media_youtube_video'] = array(
13
    'label' => t('YouTube Video'),
14
    'file types' => array('video'),
15
    'default settings' => array(
16
      'width' => 640,
17
      'height' => 390,
18
      'autohide' => 2,
19
      'autoplay' => FALSE,
20
      'color' => 'red',
21
      'enablejsapi' => FALSE,
22
      'loop' => FALSE,
23
      'modestbranding' => FALSE,
24
      'nocookie' => FALSE,
25
      'origin' => '',
26
      'protocol' => 'https:',
27
      'protocol_specify' => FALSE,
28
      'rel' => TRUE,
29
      'showinfo' => TRUE,
30
      'theme' => 'dark',
31
      'captions' => FALSE,
32
      'controls' => FALSE,
33
    ),
34
    'view callback' => 'media_youtube_file_formatter_video_view',
35
    'settings callback' => 'media_youtube_file_formatter_video_settings',
36
    'mime types' => array('video/youtube'),
37
  );
38

    
39
  $formatters['media_youtube_image'] = array(
40
    'label' => t('YouTube Preview Image'),
41
    'file types' => array('video'),
42
    'default settings' => array(
43
      'image_style' => '',
44
    ),
45
    'view callback' => 'media_youtube_file_formatter_image_view',
46
    'settings callback' => 'media_youtube_file_formatter_image_settings',
47
    'mime types' => array('video/youtube'),
48
  );
49

    
50
  return $formatters;
51
}
52

    
53
/**
54
 * Implements hook_file_formatter_FORMATTER_view().
55
 */
56
function media_youtube_file_formatter_video_view($file, $display, $langcode) {
57
  $scheme = file_uri_scheme($file->uri);
58

    
59
  // WYSIWYG does not yet support video inside a running editor instance.
60
  if ($scheme == 'youtube' && empty($file->override['wysiwyg'])) {
61
    $element = array(
62
      '#theme' => 'media_youtube_video',
63
      '#uri' => $file->uri,
64
      '#options' => array(),
65
    );
66

    
67
    // Fake a default for attributes so the ternary doesn't choke.
68
    $display['settings']['attributes'] = array();
69

    
70
    foreach (array('width', 'height', 'autohide', 'autoplay', 'color', 'enablejsapi', 'loop', 'modestbranding', 'nocookie', 'origin', 'protocol', 'protocol_specify', 'rel', 'showinfo', 'theme', 'attributes', 'captions','controls') as $setting) {
71
      $element['#options'][$setting] = isset($file->override[$setting]) ? $file->override[$setting] : $display['settings'][$setting];
72
    }
73

    
74
    return $element;
75
  }
76
}
77

    
78
/**
79
 * Implements hook_file_formatter_FORMATTER_settings().
80
 */
81
function media_youtube_file_formatter_video_settings($form, &$form_state, $settings) {
82
  $element = array();
83

    
84
  $element['width'] = array(
85
    '#title' => t('Width'),
86
    '#type' => 'textfield',
87
    '#default_value' => $settings['width'],
88
    '#element_validate' => array('_youtube_validate_video_width_and_height'),
89
  );
90
  $element['height'] = array(
91
    '#title' => t('Height'),
92
    '#type' => 'textfield',
93
    '#default_value' => $settings['height'],
94
    '#element_validate' => array('_youtube_validate_video_width_and_height'),
95
  );
96

    
97
  // @see https://developers.google.com/youtube/player_parameters#Parameters
98

    
99
  // Multiple options.
100
  $element['theme'] = array(
101
    '#title' => t('Player theme'),
102
    '#type' => 'radios',
103
    '#options' => array(
104
      'dark' => t('Dark'),
105
      'light' => t('Light'),
106
    ),
107
    '#default_value' => $settings['theme'],
108
  );
109
  $element['color'] = array(
110
    '#title' => t('Progress bar color'),
111
    '#type' => 'radios',
112
    '#options' => array(
113
      'red' => t('Red'),
114
      'white' => t('White'),
115
    ),
116
    '#default_value' => $settings['color'],
117
  );
118
  $element['autohide'] = array(
119
    '#title' => t('Controls during playback'),
120
    '#type' => 'radios',
121
    '#options' => array(
122
      '0' => t('Keep progress bar and player controls on screen while playing'),
123
      '2' => t('Hide progress bar while playing'),
124
      '1' => t('Hide progress bar and player controls'),
125
    ),
126
    '#default_value' => $settings['autohide'],
127
  );
128
  $element['captions'] = array(
129
    '#title' => t('Captions displaying options'),
130
    '#type' => 'radios',
131
    '#options' => array(
132
      '0' => t('Turns captions off by default'),
133
      '1' => t('Turns captions on by default'),
134
      '2' => t('Turns captions on and set the caption language for the video by default'),
135
    ),
136
    '#default_value' => $settings['captions'],
137
  );
138

    
139
  // Single Options.
140
  $element['autoplay'] = array(
141
    '#title' => t('Autoplay video on load'),
142
    '#type' => 'checkbox',
143
    '#default_value' => $settings['autoplay'],
144
  );
145
  $element['loop'] = array(
146
    '#title' => t('Loop video'),
147
    '#type' => 'checkbox',
148
    '#default_value' => $settings['loop'],
149
  );
150
  $element['controls'] = array(
151
    '#title' => t('Show Controls'),
152
    '#type' => 'checkbox',
153
    '#default_value' => $settings['controls'],
154
  );
155

    
156
  // Note: make sure the positive/negative language lines up with option
157
  // processing in media_youtube.theme.inc.
158
  $element['showinfo'] = array(
159
    '#title' => t('Display video title and uploader'),
160
    '#type' => 'checkbox',
161
    '#default_value' => $settings['showinfo'],
162
  );
163
  $element['modestbranding'] = array(
164
    '#title' => t('Remove YouTube logo from the control bar'),
165
    '#type' => 'checkbox',
166
    '#default_value' => $settings['modestbranding'],
167
  );
168
  $element['rel'] = array(
169
    '#title' => t('Show related videos when playback ends'),
170
    '#type' => 'checkbox',
171
    '#default_value' => $settings['rel'],
172
  );
173
  $element['nocookie'] = array(
174
    '#title' => t('Use privacy enhanced (no cookie) mode'),
175
    '#type' => 'checkbox',
176
    '#default_value' => $settings['nocookie'],
177
  );
178
  $element['protocol_specify'] = array(
179
    '#title' => t('Specify an http protocol'),
180
    '#type' => 'checkbox',
181
    '#default_value' => $settings['protocol_specify'],
182
    '#description' => t('An explicit protocol may be necessary for videos embedded in RSS feeds and emails. If no protocol is specified, iframes will be protocol relative.'),
183
  );
184
  $element['protocol'] = array(
185
    '#title' => t('Iframe protocol'),
186
    '#type' => 'radios',
187
    '#default_value' => $settings['protocol'],
188
    '#options' => array(
189
      'http:' => 'http://',
190
      'https:' => 'https://',
191
    ),
192
    '#states' => array(
193
      'invisible' => array(
194
        ':input[name="displays[media_youtube_video][settings][protocol_specify]"]' => array('checked' => FALSE),
195
      ),
196
    ),
197
  );
198

    
199
  // JS api.
200
  $element['enablejsapi'] = array(
201
    '#title' => t('Enable the <a href="https://developers.google.com/youtube/js_api_reference">Javascript API</a>'),
202
    '#type' => 'checkbox',
203
    '#default_value' => $settings['enablejsapi'],
204
    '#description' => 'An id in the format <code>media-youtube-{video_id}</code> will be added to each iframe.',
205
  );
206
  $element['origin'] = array(
207
    '#title' => t('Origin'),
208
    '#type' => 'textfield',
209
    '#description' => t('If the Javascript API is enabled, enter your site\'s domain for added security.'),
210
    '#default_value' => $settings['origin'],
211
    '#states' => array(
212
      'invisible' => array(
213
        ':input[name="displays[media_youtube_video][settings][enablejsapi]"]' => array('checked' => FALSE),
214
      ),
215
    ),
216
    '#element_validate' => array('_youtube_validate_jsapi_domain'),
217
  );
218

    
219
  return $element;
220
}
221

    
222
/**
223
 * Validation for width and height.
224
 */
225
function _youtube_validate_video_width_and_height($element, &$form_state, $form) {
226
  // Check if the value is a number with an optional decimal or percentage sign, or "auto".
227
  if (!empty($element['#value']) && !preg_match('/^(auto|([0-9]*(\.[0-9]+)?%?))$/', $element['#value'])) {
228
    form_error($element, t("The value entered for @dimension is invalid. Please insert a unitless integer for pixels, a percent, or \"auto\". Note that percent and auto may not function correctly depending on the browser and doctype.", array('@dimension' => $element['#title'])));
229
  }
230
}
231

    
232
/**
233
 * Validation for Js API Origin.
234
 */
235
function _youtube_validate_jsapi_domain ($element, &$form_state, $form) {
236
  // Check if the value is a url with http/s and no trailing directories.
237
  if (!empty($element['#value']) && !preg_match('/^https?\:\/\/[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}){1,2}$/', $element['#value'])) {
238
    form_error($element, t('Please insert a valid domain in the format http://www.yourdomain.com'));
239
  }
240
}
241

    
242
/**
243
 * Implements hook_file_formatter_FORMATTER_view().
244
 */
245
function media_youtube_file_formatter_image_view($file, $display, $langcode) {
246
  $scheme = file_uri_scheme($file->uri);
247
  if ($scheme == 'youtube') {
248
    $wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
249
    $image_style = $display['settings']['image_style'];
250
    $valid_image_styles = image_style_options(FALSE);
251

    
252
    if (empty($image_style) || !isset($valid_image_styles[$image_style])) {
253
      $element = array(
254
        '#theme' => 'image',
255
        '#path' => str_replace('http:', '', $wrapper->getLocalThumbnailPath()),
256
        '#alt' => isset($file->override['attributes']['alt']) ? $file->override['attributes']['alt'] : $file->filename,
257
      );
258
    }
259
    else {
260
      $element = array(
261
        '#theme' => 'image_style',
262
        '#style_name' => $image_style,
263
        '#path' => $wrapper->getLocalThumbnailPath(),
264
        '#alt' => isset($file->override['attributes']['alt']) ? $file->override['attributes']['alt'] : $file->filename,
265
      );
266
    }
267

    
268
    return $element;
269
  }
270
}
271

    
272
/**
273
 * Implements hook_file_formatter_FORMATTER_settings().
274
 */
275
function media_youtube_file_formatter_image_settings($form, &$form_state, $settings) {
276
  $element = array();
277

    
278
  $element['image_style'] = array(
279
    '#title' => t('Image style'),
280
    '#type' => 'select',
281
    '#options' => image_style_options(FALSE),
282
    '#default_value' => $settings['image_style'],
283
    '#empty_option' => t('None (original image)'),
284
  );
285

    
286
  return $element;
287
}