Projet

Général

Profil

Paste
Télécharger (14,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / file_entity / file_entity.field.inc @ 2b3c8cc1

1
<?php
2

    
3
/**
4
 * @file
5
 * Field API integration for the file_entity module.
6
 */
7

    
8
/**
9
 * Implements hook_field_formatter_info().
10
 */
11
function file_entity_field_formatter_info() {
12
  $info['file_rendered'] = array(
13
    'label' => t('Rendered file'),
14
    'description' => t('Display the file in a specific view mode'),
15
    'field types' => array('file', 'image'),
16
    'settings' => array(
17
      'file_view_mode' => 'default',
18
    ),
19
    'file formatter' => array(
20
      'hidden' => TRUE,
21
    ),
22
  );
23
  $info['file_download_link'] = array(
24
    'label' => t('Download link'),
25
    'description' => t('Displays a link that will force the browser to download the file.'),
26
    'field types' => array('file', 'image'),
27
    'settings' => array(
28
      'text' => t('Download [file:name]'),
29
    ),
30
  );
31
  $info['file_audio'] = array(
32
    'label' => t('Audio'),
33
    'description' => t('Render the file using an HTML5 audio tag.'),
34
    'field types' => array('file'),
35
    'settings' => array(
36
      'controls' => TRUE,
37
      'autoplay' => FALSE,
38
      'loop' => FALSE,
39
      'preload' => '',
40
      'multiple_file_behavior' => 'tags',
41
    ),
42
    'file formatter' => array(
43
      'mime types' => array('audio/*'),
44
    ),
45
  );
46
  $info['file_video'] = array(
47
    'label' => t('Video'),
48
    'description' => t('Render the file using an HTML5 video tag.'),
49
    'field types' => array('file'),
50
    'settings' => array(
51
      'controls' => TRUE,
52
      'autoplay' => FALSE,
53
      'loop' => FALSE,
54
      'muted' => FALSE,
55
      'width' => NULL,
56
      'height' => NULL,
57
      'preload' => '',
58
      'multiple_file_behavior' => 'tags',
59
    ),
60
    'file formatter' => array(
61
      'mime types' => array('video/*'),
62
    ),
63
  );
64
  return $info;
65
}
66

    
67
/**
68
 * Implements hook_field_formatter_info_alter().
69
 */
70
function file_entity_field_formatter_info_alter(&$info) {
71
  // Add descriptions to core formatters.
72
  $descriptions = array(
73
    'file_default' => t('Create a simple link to the file. The link is prefixed by a file type icon and the name of the file is used as the link text'),
74
    'file_table' => t('Build a two-column table where the first column contains a generic link to the file and the second column displays the size of the file.'),
75
    'file_url_plain' => t('Display a plain text URL to the file.'),
76
    'image' => t('Format the file as an image. The image can be displayed using an image style and can optionally be linked to the image file itself or its parent content.'),
77
  );
78
  foreach ($descriptions as $key => $description) {
79
    if (isset($info[$key]) && empty($info[$key]['description'])) {
80
      $info[$key]['description'] = $description;
81
    }
82
  }
83

    
84
  // Formatters that can be used for images but not files, should have a
85
  // default mimetype restriction added to the image/* mime type for use with
86
  // file formatters.
87
  foreach ($info as &$formatter) {
88
    if (!isset($formatter['file formatter']) && in_array('image', $formatter['field types']) && !in_array('file', $formatter['field types'])) {
89
      $formatter['file formatter']['mime types'] = array('image/*');
90
    }
91
  }
92
}
93

    
94
/**
95
 * Implements hook_field_formatter_settings_form().
96
 */
97
function file_entity_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
98
  $display = $instance['display'][$view_mode];
99
  $settings = $display['settings'];
100
  $element = array();
101

    
102
  if ($display['type'] == 'file_rendered') {
103
    $element['file_view_mode'] = array(
104
      '#title'   => t('View mode'),
105
      '#type'    => 'select',
106
      '#options' => file_entity_view_mode_labels(),
107
      '#default_value' => $settings['file_view_mode'],
108
      // Never empty, so no #empty_option
109
    );
110
  }
111
  elseif ($display['type'] == 'file_download_link') {
112
    $element['text'] = array(
113
      '#type' => 'textfield',
114
      '#title' => t('Link text'),
115
      '#description' => t('This field support tokens.'),
116
      '#default_value' => $settings['text'],
117
      '#required' => TRUE,
118
    );
119
  }
120
  elseif ($display['type'] == 'file_audio') {
121
    $element['controls'] = array(
122
      '#title' => t('Show audio controls'),
123
      '#type' => 'checkbox',
124
      '#default_value' => $settings['controls'],
125
    );
126
    $element['autoplay'] = array(
127
      '#title' => t('Autoplay'),
128
      '#type' => 'checkbox',
129
      '#default_value' => $settings['autoplay'],
130
    );
131
    $element['loop'] = array(
132
      '#title' => t('Loop'),
133
      '#type' => 'checkbox',
134
      '#default_value' => $settings['loop'],
135
    );
136
    $element['preload'] = array(
137
      '#title' => t('Preload'),
138
      '#type' => 'select',
139
      '#default_value' => $settings['preload'],
140
      '#options' => drupal_map_assoc(array('none', 'auto', 'metadata')),
141
      '#empty_option' => 'unspecified',
142
    );
143
    $element['multiple_file_behavior'] = array(
144
      '#title' => t('Display of multiple files'),
145
      '#type' => 'radios',
146
      '#options' => array(
147
        'tags' => t('Use multiple @tag tags, each with a single source', array('@tag' => '<audio>')),
148
        'sources' => t('Use multiple sources within a single @tag tag', array('@tag' => '<audio>')),
149
      ),
150
      '#default_value' => $settings['multiple_file_behavior'],
151
      // Hide this setting in the manage file display configuration.
152
      '#access' => !empty($field),
153
    );
154

    
155
  }
156
  elseif ($display['type'] == 'file_video') {
157
    $element['controls'] = array(
158
      '#title' => t('Show video controls'),
159
      '#type' => 'checkbox',
160
      '#default_value' => $settings['controls'],
161
    );
162
    $element['autoplay'] = array(
163
      '#title' => t('Autoplay'),
164
      '#type' => 'checkbox',
165
      '#default_value' => $settings['autoplay'],
166
    );
167
    $element['loop'] = array(
168
      '#title' => t('Loop'),
169
      '#type' => 'checkbox',
170
      '#default_value' => $settings['loop'],
171
    );
172
    $element['muted'] = array(
173
      '#title' => t('Muted'),
174
      '#type' => 'checkbox',
175
      '#default_value' => $settings['muted'],
176
    );
177
    $element['width'] = array(
178
      '#type' => 'textfield',
179
      '#title' => t('Width'),
180
      '#default_value' => $settings['width'],
181
      '#size' => 5,
182
      '#maxlength' => 5,
183
      '#field_suffix' => t('pixels'),
184
    );
185
    $element['height'] = array(
186
      '#type' => 'textfield',
187
      '#title' => t('Height'),
188
      '#default_value' => $settings['height'],
189
      '#size' => 5,
190
      '#maxlength' => 5,
191
      '#field_suffix' => t('pixels'),
192
    );
193
    $element['preload'] = array(
194
      '#title' => t('Preload'),
195
      '#type' => 'select',
196
      '#default_value' => $settings['preload'],
197
      '#options' => drupal_map_assoc(array('none', 'auto', 'metadata')),
198
      '#empty_option' => 'unspecified',
199
    );
200
    $element['multiple_file_behavior'] = array(
201
      '#title' => t('Display of multiple files'),
202
      '#type' => 'radios',
203
      '#options' => array(
204
        'tags' => t('Use multiple @tag tags, each with a single source', array('@tag' => '<video>')),
205
        'sources' => t('Use multiple sources within a single @tag tag', array('@tag' => '<video>')),
206
      ),
207
      '#default_value' => $settings['multiple_file_behavior'],
208
      // Hide this setting in the manage file display configuration.
209
      '#access' => !empty($field),
210
    );
211
  }
212

    
213
  return $element;
214
}
215

    
216
/**
217
 * Implements hook_field_formatter_settings_summary().
218
 */
219
function file_entity_field_formatter_settings_summary($field, $instance, $view_mode) {
220
  $display = $instance['display'][$view_mode];
221
  $settings = $display['settings'];
222
  $summary = array();
223

    
224
  if ($display['type'] === 'file_rendered') {
225
    $view_mode_label = file_entity_view_mode_label($settings['file_view_mode'], t('Unknown'));
226
    $summary[] = t('View mode: %mode', array('%mode' => $view_mode_label));
227
  }
228
  elseif ($display['type'] == 'file_download_link') {
229
    $summary[] = t('Link text: %text', array('%text' => $settings['text']));
230
  }
231
  elseif ($display['type'] === 'file_audio') {
232
    if (isset($settings['controls'])) {
233
      $summary[] = t('Controls: %controls', array('%controls' => $settings['controls'] ? 'visible' : 'hidden'));
234
    }
235
    if (isset($settings['autoplay'])) {
236
      $summary[] = t('Autoplay: %autoplay', array('%autoplay' => $settings['autoplay'] ? t('yes') : t('no')));
237
    }
238
    if (isset($settings['loop'])) {
239
      $summary[] = t('Loop: %loop', array('%loop' => $settings['loop'] ? t('yes') : t('no')));
240
    }
241
    if (!empty($settings['preload'])) {
242
      $summary[] = t('Preload: %preload', array('%preload' => $settings['preload']));
243
    }
244
    if (isset($settings['multiple_file_behavior'])) {
245
      $summary[] = t('Multiple files: %multiple', array('%multiple' => $settings['multiple_file_behavior']));
246
    }
247
  }
248
  elseif ($display['type'] === 'file_video') {
249
    if (isset($settings['controls'])) {
250
      $summary[] = t('Controls: %controls', array('%controls' => $settings['controls'] ? 'visible' : 'hidden'));
251
    }
252
    if (isset($settings['autoplay'])) {
253
      $summary[] = t('Autoplay: %autoplay', array('%autoplay' => $settings['autoplay'] ? t('yes') : t('no')));
254
    }
255
    if (isset($settings['loop'])) {
256
      $summary[] = t('Loop: %loop', array('%loop' => $settings['loop'] ? t('yes') : t('no')));
257
    }
258
    if (isset($settings['muted'])) {
259
      $summary[] = t('Muted: %muted', array('%muted' => $settings['muted'] ? t('yes') : t('no')));
260
    }
261
    if ($settings['width'] && $settings['height']) {
262
      $summary[] = t('Size: %width x %height', array('%width' => $settings['width'], '%height' => $settings['height']));
263
    }
264
    if (!empty($settings['preload'])) {
265
      $summary[] = t('Preload: %preload', array('%preload' => $settings['preload']));
266
    }
267
    if (isset($settings['multiple_file_behavior'])) {
268
      $summary[] = t('Multiple files: %multiple', array('%multiple' => $settings['multiple_file_behavior']));
269
    }
270
  }
271

    
272
  return implode('<br />', $summary);
273
}
274

    
275
/**
276
 * Implements hook_field_formatter_view().
277
 */
278
function file_entity_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
279
  $settings = $display['settings'];
280
  $element = array();
281

    
282
  if ($display['type'] == 'file_rendered') {
283
    $view_mode = $settings['file_view_mode'];
284

    
285
    // To prevent infinite recursion caused by reference cycles, we store
286
    // diplayed nodes in a recursion queue.
287
    $recursion_queue = &drupal_static(__FUNCTION__, array());
288

    
289
    // If no 'referencing entity' is set, we are starting a new 'reference
290
    // thread' and need to reset the queue.
291
    // @todo Bug: $entity->referencing_entity on files referenced in a different
292
    // thread on the page.
293
    // E.g: 1 references 1+2 / 2 references 1+2 / visit homepage.
294
    // We'd need a more accurate way...
295
    if (!isset($entity->referencing_entity)) {
296
      $recursion_queue = array();
297
    }
298

    
299
    // The recursion queue only needs to track files.
300
    if ($entity_type == 'file') {
301
      list($id) = entity_extract_ids($entity_type, $entity);
302
      $recursion_queue[$id] = $id;
303
    }
304

    
305
    // Prevent 'empty' fields from causing a WSOD.
306
    $items = array_filter($items);
307

    
308
    // Check the recursion queue to determine which nodes should be fully
309
    // displayed, and which nodes will only be displayed as a title.
310
    $files_display = array();
311
    foreach ($items as $delta => $item) {
312
      if (!isset($recursion_queue[$item['fid']])) {
313
        $files_display[$item['fid']] = file_load($item['fid']);
314
        if (!empty($item['description'])) {
315
          $files_display[$item['fid']]->description = $item['description'];
316
        }
317
      }
318
    }
319

    
320
    // Load and build the fully displayed nodes.
321
    if ($files_display) {
322
      foreach ($files_display as $fid => $file) {
323
        $files_display[$fid]->referencing_entity = $entity;
324
        $files_display[$fid]->referencing_entity_type = $entity_type;
325
        $files_display[$fid]->referencing_field = $field['field_name'];
326
      }
327
      $output = file_view_multiple($files_display, $view_mode);
328
      // Remove the first level from the output array.
329
      $files_built = reset($output);
330
    }
331

    
332
    // Assemble the render array.
333
    foreach ($items as $delta => $item) {
334
      if (isset($files_built[$item['fid']])) {
335
        $element[$delta] = $files_built[$item['fid']];
336
      }
337
    }
338
  }
339
  elseif ($display['type'] == 'file_download_link') {
340
    foreach ($items as $delta => $item) {
341
      $file = (object) $item;
342
      if (file_entity_access('download', $file)) {
343
        $element[$delta] = array(
344
          '#theme' => 'file_entity_download_link',
345
          '#file' => $file,
346
          '#text' => $settings['text'],
347
        );
348
      }
349
    }
350
  }
351
  elseif ($display['type'] == 'file_audio') {
352
    $multiple_file_behavior = $settings['multiple_file_behavior'];
353

    
354
    // Prevent 'empty' fields from causing a WSOD.
355
    $items = array_filter($items);
356

    
357
    // Build an array of sources for each <audio> element.
358
    $source_lists = array();
359
    if ($multiple_file_behavior == 'tags') {
360
      foreach ($items as $delta => $item) {
361
        if ($item['type'] == 'audio') {
362
          $source_lists[$delta] = array($item);
363
        }
364
      }
365
    }
366
    else {
367
      foreach ($items as $delta => $item) {
368
        if ($item['type'] == 'audio') {
369
          $source_lists[0][$delta] = $item;
370
        }
371
      }
372
    }
373

    
374
    // Render each source list as an <audio> element.
375
    foreach ($source_lists as $delta => $sources) {
376
      $element[$delta] = array(
377
        '#theme' => 'file_entity_file_audio',
378
        '#files' => $sources,
379
        '#controls' => $settings['controls'],
380
        '#autoplay' => $settings['autoplay'],
381
        '#loop' => $settings['loop'],
382
        '#preload' => $settings['preload'],
383
      );
384
    }
385
  }
386
  elseif ($display['type'] == 'file_video') {
387
    $multiple_file_behavior = $settings['multiple_file_behavior'];
388

    
389
    // Prevent 'empty' fields from causing a WSOD.
390
    $items = array_filter($items);
391

    
392
    // Build an array of sources for each <video> element.
393
    $source_lists = array();
394
    if ($multiple_file_behavior == 'tags') {
395
      foreach ($items as $delta => $item) {
396
        if ($item['type'] == 'video') {
397
          $source_lists[$delta] = array($item);
398
        }
399
      }
400
    }
401
    else {
402
      foreach ($items as $delta => $item) {
403
        if ($item['type'] == 'video') {
404
          $source_lists[0][$delta] = $item;
405
        }
406
      }
407
    }
408

    
409
    // Render each source list as an <video> element.
410
    foreach ($source_lists as $delta => $sources) {
411
      $element[$delta] = array(
412
        '#theme' => 'file_entity_file_video',
413
        '#files' => $sources,
414
        '#controls' => $settings['controls'],
415
        '#autoplay' => $settings['autoplay'],
416
        '#loop' => $settings['loop'],
417
        '#muted' => $settings['muted'],
418
        '#width' => $settings['width'],
419
        '#height' => $settings['height'],
420
        '#preload' => $settings['preload'],
421
      );
422
    }
423
  }
424

    
425
  return $element;
426
}