Projet

Général

Profil

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

root / drupal7 / sites / all / modules / media_youtube / includes / media_youtube.formatters.inc @ 5e632cae

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' => 'https:',
188
    '#options' => array(
189
      'https:' => 'https://',
190
    ),
191
    '#states' => array(
192
      'invisible' => array(
193
        ':input[name="displays[media_youtube_video][settings][protocol_specify]"]' => array('checked' => FALSE),
194
      ),
195
    ),
196
  );
197

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

    
218
  return $element;
219
}
220

    
221
/**
222
 * Validation for width and height.
223
 */
224
function _youtube_validate_video_width_and_height($element, &$form_state, $form) {
225
  // Check if the value is a number with an optional decimal or percentage sign, or "auto".
226
  if (!empty($element['#value']) && !preg_match('/^(auto|([0-9]*(\.[0-9]+)?%?))$/', $element['#value'])) {
227
    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'])));
228
  }
229
}
230

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

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

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

    
267
    return $element;
268
  }
269
}
270

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

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

    
285
  return $element;
286
}