Projet

Général

Profil

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

root / drupal7 / sites / all / modules / media / includes / media.fields.inc @ fc3d89c3

1
<?php
2

    
3
/**
4
 * @file
5
 * Provide the media selector widget and media field formatters to the Fields
6
 * API.
7
 */
8

    
9
/**
10
 * Implements hook_field_widget_info().
11
 */
12
function media_field_widget_info() {
13
  return array(
14
    'media_generic' => array(
15
      'label' => t('Media browser'),
16
      'field types' => array('file', 'image'),
17
      'settings' => array(
18
        'allowed_types' => array(
19
          'image' => 'image',
20
        ),
21
        'browser_plugins' => array(),
22
        'allowed_schemes' => array(
23
          'public' => 'public',
24
        ),
25
      ),
26
      'behaviors' => array(
27
        'multiple values' => FIELD_BEHAVIOR_CUSTOM,
28
        'default value' => FIELD_BEHAVIOR_NONE,
29
      ),
30
    ),
31
  );
32
}
33

    
34
/**
35
 * Implements hook_field_widget_settings_form().
36
 */
37
function media_field_widget_settings_form($field, $instance) {
38
  $widget = $instance['widget'];
39
  $settings = $widget['settings'];
40

    
41
  $plugins = media_get_browser_plugin_info();
42
  $options = array();
43
  foreach ($plugins as $key => $plugin) {
44
    $options[$key] = check_plain($plugin['title']);
45
  }
46

    
47
  $form['browser_plugins'] = array(
48
    '#type' => 'checkboxes',
49
    '#title' => t('Enabled browser plugins'),
50
    '#options' => $options,
51
    '#default_value' => $settings['browser_plugins'],
52
    '#description' => t('Media browser plugins which are allowed for this field. If no plugins are selected, they will all be available.'),
53
  );
54

    
55
  $form['allowed_types'] = array(
56
    '#type' => 'checkboxes',
57
    '#title' => t('Allowed file types'),
58
    '#options' => file_entity_type_get_names(),
59
    '#default_value' => $settings['allowed_types'],
60
    '#description' => t('File types which are allowed for this field. If no file types are selected, they will all be available.'),
61
  );
62

    
63
  $visible_steam_wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_VISIBLE);
64
  $options = array();
65
  foreach ($visible_steam_wrappers as $scheme => $information) {
66
    $options[$scheme] = check_plain($information['name']);
67
  }
68

    
69
  $form['allowed_schemes'] = array(
70
    '#type' => 'checkboxes',
71
    '#title' => t('Allowed URI schemes'),
72
    '#options' => $options,
73
    '#default_value' => $settings['allowed_schemes'],
74
    '#description' => t('URI schemes which are allowed for this field. If no schemes are selected, they will all be available.'),
75
  );
76

    
77
  return $form;
78
}
79

    
80
/**
81
 * Implements hook_field_widget_form().
82
 */
83
function media_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
84
  $defaults = array(
85
    'fid' => 0,
86
    'display' => !empty($field['settings']['display_default']),
87
    'description' => '',
88
  );
89

    
90
  // Load the items for form rebuilds from the field state as they might not be
91
  // in $form_state['values'] because of validation limitations. Also, they are
92
  // only passed in as $items when editing existing entities.
93
  $field_state = field_form_get_state($element['#field_parents'], $field['field_name'], $langcode, $form_state);
94
  if (isset($field_state['items'])) {
95
    $items = $field_state['items'];
96
  }
97

    
98
  $field_settings = $instance['settings'];
99
  $widget_settings = $instance['widget']['settings'];
100

    
101
  // Essentially we use the media type, extended with some enhancements.
102
  $element_info = element_info('media');
103
  $multiselect = ($field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED);
104

    
105
  $element += array(
106
    '#type' => 'media',
107
    '#value_callback' => 'media_field_widget_value',
108
    '#process' => array_merge($element_info['#process'], array('media_field_widget_process')),
109
    '#media_options' => array(
110
      'global' => array(
111
        'types' => array_filter($widget_settings['allowed_types']),
112
        'enabledPlugins' => array_filter($instance['widget']['settings']['browser_plugins']),
113
        'schemes' => array_filter($widget_settings['allowed_schemes']),
114
        'file_directory' => isset($field_settings['file_directory']) ? $field_settings['file_directory'] : '',
115
        'file_extensions' => isset($field_settings['file_extensions']) ? $field_settings['file_extensions'] : variable_get('file_entity_default_allowed_extensions', 'jpg jpeg gif png txt doc docx xls xlsx pdf ppt pptx pps ppsx odt ods odp mp3 mov mp4 m4a m4v mpeg avi ogg oga ogv weba webp webm'),
116
        'max_filesize' => isset($field_settings['max_filesize']) ? $field_settings['max_filesize'] : 0,
117
        'uri_scheme' => !empty($field['settings']['uri_scheme']) ? $field['settings']['uri_scheme'] : file_default_scheme(),
118
        'multiselect' => $multiselect,
119
      ),
120
    ),
121
    // Allows this field to return an array instead of a single value.
122
    '#extended' => TRUE,
123
  );
124

    
125
  // Add image field specific validators.
126
  if ($field['type'] == 'image') {
127
    if ($field_settings['min_resolution'] || $field_settings['max_resolution']) {
128
      $element['#media_options']['global']['min_resolution'] = $field_settings['min_resolution'];
129
      $element['#media_options']['global']['max_resolution'] = $field_settings['max_resolution'];
130
    }
131
  }
132

    
133
  if ($field['cardinality'] == 1) {
134
    // Set the default value.
135
    $element['#default_value'] = !empty($items) ? $items[0] : $defaults;
136
    // If there's only one field, return it as delta 0.
137
    if (empty($element['#default_value']['fid'])) {
138
      $element['#description'] = theme('media_upload_help', array('description' => $element['#description']));
139
    }
140
    $elements = array($element);
141
  }
142
  else {
143
    // If there are multiple values, add an element for each existing one.
144
    foreach ($items as $item) {
145
      $elements[$delta] = $element;
146
      $elements[$delta]['#default_value'] = $item;
147
      $elements[$delta]['#weight'] = $delta;
148
      $delta++;
149
    }
150
    // And then add one more empty row for new uploads except when this is a
151
    // programmed form as it is not necessary.
152
    if (($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta < $field['cardinality']) && empty($form_state['programmed'])) {
153
      $elements[$delta] = $element;
154
      $elements[$delta]['#default_value'] = $defaults;
155
      $elements[$delta]['#weight'] = $delta;
156
      $elements[$delta]['#required'] = ($element['#required'] && $delta == 0);
157
    }
158
    // The group of elements all-together need some extra functionality
159
    // after building up the full list (like draggable table rows).
160
    $elements['#file_upload_delta'] = $delta;
161
    $elements['#theme'] = 'media_widget_multiple';
162
    $elements['#theme_wrappers'] = array('fieldset');
163
    $elements['#process'] = array('media_field_widget_process_multiple');
164
    $elements['#title'] = $element['#title'];
165
    $elements['#description'] = $element['#description'];
166
    $elements['#field_name'] = $element['#field_name'];
167
    $elements['#language'] = $element['#language'];
168
    $elements['#display_field'] = intval(!empty($field['settings']['display_field']));
169

    
170
    // Add some properties that will eventually be added to the media upload
171
    // field. These are added here so that they may be referenced easily through
172
    // a hook_form_alter().
173
    $elements['#file_upload_title'] = t('Attach media');
174
    $elements['#file_upload_description'] = theme('media_upload_help', array('description' => ''));
175
  }
176

    
177
  return $elements;
178
}
179

    
180
/**
181
 * The #value_callback for the media field element.
182
 */
183
function media_field_widget_value($element, $input = FALSE, $form_state) {
184
  if ($input) {
185
    // Checkboxes lose their value when empty.
186
    // If the display field is present make sure its unchecked value is saved.
187
    $field = field_widget_field($element, $form_state);
188
    if (empty($input['display'])) {
189
      $input['display'] = intval(empty($field['settings']['display_field']));
190
    }
191
  }
192

    
193
  // We depend on the media element to handle uploads.
194
  $return = media_file_value($element, $input, $form_state);
195

    
196
  // Ensure that all the required properties are returned even if empty.
197
  $return += array(
198
    'fid' => 0,
199
    'display' => 1,
200
    'description' => '',
201
  );
202

    
203
  return $return;
204
}
205

    
206
/**
207
 * An element #process callback for the media field type.
208
 *
209
 * Expands the media type to include the description and display fields.
210
 */
211
function media_field_widget_process($element, &$form_state, $form) {
212
  $item = $element['#value'];
213
  $item['fid'] = $element['fid']['#value'];
214

    
215
  $field = field_widget_field($element, $form_state);
216
  $instance = field_widget_instance($element, $form_state);
217
  $settings = $instance['widget']['settings'];
218

    
219
  $element['#theme'] = 'media_widget';
220

    
221
  // Add the display field if enabled.
222
  if (!empty($field['settings']['display_field']) && $item['fid']) {
223
    $element['display'] = array(
224
      '#type' => empty($item['fid']) ? 'hidden' : 'checkbox',
225
      '#title' => t('Include file in display'),
226
      '#value' => isset($item['display']) ? $item['display'] : $field['settings']['display_default'],
227
      '#attributes' => array('class' => array('file-display')),
228
    );
229
  }
230
  else {
231
    $element['display'] = array(
232
      '#type' => 'hidden',
233
      '#value' => '1',
234
    );
235
  }
236

    
237
  // Add the description field if enabled.
238
  if (!empty($instance['settings']['description_field']) && $item['fid']) {
239
    $element['description'] = array(
240
      '#type' => variable_get('file_description_type', 'textfield'),
241
      '#title' => t('Description'),
242
      '#value' => isset($item['description']) ? $item['description'] : '',
243
      '#maxlength' => variable_get('file_description_length', 128),
244
      '#description' => t('The description may be used as the label of the link to the file.'),
245
    );
246
  }
247

    
248
  // Adjust the Ajax settings so that on upload and remove of any individual
249
  // file, the entire group of file fields is updated together.
250
  if ($field['cardinality'] != 1) {
251
    $parents = array_slice($element['#array_parents'], 0, -1);
252
    $new_path = 'media/ajax/' . implode('/', $parents) . '/' . $form['form_build_id']['#value'];
253
    $field_element = drupal_array_get_nested_value($form, $parents);
254
    $new_wrapper = $field_element['#id'] . '-ajax-wrapper';
255
    foreach (element_children($element) as $key) {
256
      if (isset($element[$key]['#ajax'])) {
257
        $element[$key]['#ajax']['path'] = $new_path;
258
        $element[$key]['#ajax']['wrapper'] = $new_wrapper;
259
      }
260
    }
261
    unset($element['#prefix'], $element['#suffix']);
262
  }
263

    
264
  // Add another submit handler to the upload and remove buttons, to implement
265
  // functionality needed by the field widget. This submit handler, along with
266
  // the rebuild logic in media_field_widget_form() requires the entire field,
267
  // not just the individual item, to be valid.
268
  foreach (array('attach_button', 'remove_button') as $key) {
269
    $element[$key]['#submit'][] = 'media_field_widget_submit';
270
    $element[$key]['#limit_validation_errors'] = array(array_slice($element['#parents'], 0, -1));
271
  }
272

    
273
  return $element;
274
}
275

    
276
/**
277
 * An element #process callback for a group of media fields.
278
 *
279
 * Adds the weight field to each row so it can be ordered and adds a new Ajax
280
 * wrapper around the entire group so it can be replaced all at once.
281
 */
282
function media_field_widget_process_multiple($element, &$form_state, $form) {
283
    // In order to support multiple selection, we need to reconstruct the _POST
284
  // data that is checked in media_attach_file(). We need to reconstruct the
285
  // field's _POST key name, for example: field_mediafield_und_0.
286
  $upload_name_prefix = implode('_', $element['#parents']) . '_';
287
  $upload_name = $upload_name_prefix . $element['#file_upload_delta'];
288
  if (!empty($_POST['media'][$upload_name])) {
289
    $files = explode(',', $_POST['media'][$upload_name]);
290
    $count = count($files);
291
    // Supposing #file_upload_delta is always the last delta this will work
292
    for ($i = 0; $i < $count; $i++) {
293
      // For each file selected, increment the field key to be processed.
294
      // field_mediafield_und_0 becomes field_mediafield_und_1, etc.
295
      $_POST['media'][$upload_name_prefix . ($element['#file_upload_delta'] + $i)] = $files[$i];
296
      $element[] = $element[$element['#file_upload_delta']];
297
    }
298
  }
299

    
300
  $element_children = element_children($element, TRUE);
301
  $count = count($element_children);
302

    
303
  foreach ($element_children as $delta => $key) {
304
    if ($key != $element['#file_upload_delta']) {
305
      $description = _media_field_get_description_from_element($element[$key]);
306
      $element[$key]['_weight'] = array(
307
        '#type' => 'weight',
308
        '#title' => $description ? t('Weight for @title', array('@title' => $description)) : t('Weight for new file'),
309
        '#title_display' => 'invisible',
310
        '#delta' => $count,
311
        '#default_value' => $delta,
312
      );
313
    }
314
    else {
315
      // The title needs to be assigned to the attach field so that validation
316
      // errors include the correct widget label.
317
      $element[$key]['#title'] = $element['#title'];
318
      $element[$key]['_weight'] = array(
319
        '#type' => 'hidden',
320
        '#default_value' => $delta,
321
      );
322
    }
323
  }
324

    
325
  // Add a new wrapper around all the elements for Ajax replacement.
326
  $element['#prefix'] = '<div id="' . $element['#id'] . '-ajax-wrapper">';
327
  $element['#suffix'] = '</div>';
328

    
329
  return $element;
330
}
331

    
332
/**
333
 * Retrieves the file description from a media field element.
334
 *
335
 * This helper function is used by media_field_widget_process_multiple().
336
 *
337
 * @param $element
338
 *   The element being processed.
339
 *
340
 * @return
341
 *   A description of the file suitable for use in the administrative interface.
342
 */
343
function _media_field_get_description_from_element($element) {
344
  // Use the actual file description, if it's available.
345
  if (!empty($element['#default_value']['description'])) {
346
    return $element['#default_value']['description'];
347
  }
348
  // Otherwise, fall back to the filename.
349
  if (!empty($element['#default_value']['filename'])) {
350
    return $element['#default_value']['filename'];
351
  }
352
  // This is probably a newly uploaded file; no description is available.
353
  return FALSE;
354
}
355

    
356
/**
357
 * Form submission handler for attach/remove button of media_field_widget_form().
358
 *
359
 * This runs in addition to and after media_field_widget_submit().
360
 *
361
 * @see media_field_widget_submit()
362
 * @see media_field_widget_form()
363
 * @see media_field_widget_process()
364
 */
365
function media_field_widget_submit($form, &$form_state) {
366
  // During the form rebuild, media_field_widget_form() will create field item
367
  // widget elements using re-indexed deltas, so clear out $form_state['input']
368
  // to avoid a mismatch between old and new deltas. The rebuilt elements will
369
  // have #default_value set appropriately for the current state of the field,
370
  // so nothing is lost in doing this.
371
  $parents = array_slice($form_state['triggering_element']['#parents'], 0, -2);
372
  drupal_array_set_nested_value($form_state['input'], $parents, NULL);
373

    
374
  $button = $form_state['triggering_element'];
375

    
376
  // Go one level up in the form, to the widgets container.
377
  $element = drupal_array_get_nested_value($form, array_slice($button['#array_parents'], 0, -1));
378
  $field_name = $element['#field_name'];
379
  $langcode = $element['#language'];
380
  $parents = $element['#field_parents'];
381

    
382
  $submitted_values = drupal_array_get_nested_value($form_state['values'], array_slice($button['#parents'], 0, -2));
383
  foreach ($submitted_values as $delta => $submitted_value) {
384
    if (!$submitted_value['fid']) {
385
      unset($submitted_values[$delta]);
386
    }
387
  }
388

    
389
  // Re-index deltas after removing empty items.
390
  $submitted_values = array_values($submitted_values);
391

    
392
  // Update form_state values.
393
  drupal_array_set_nested_value($form_state['values'], array_slice($button['#parents'], 0, -2), $submitted_values);
394

    
395
  // Update items.
396
  $field_state = field_form_get_state($parents, $field_name, $langcode, $form_state);
397
  $field_state['items'] = $submitted_values;
398
  field_form_set_state($parents, $field_name, $langcode, $form_state, $field_state);
399
}
400

    
401
/**
402
 * Returns HTML for an individual media widget.
403
 *
404
 * @param $variables
405
 *   An associative array containing:
406
 *   - element: A render element representing the widget.
407
 *
408
 * @ingroup themeable
409
 */
410
function theme_media_widget($variables) {
411
  $element = $variables['element'];
412
  $output = '';
413

    
414
  // The "form-media" class is required for proper Ajax functionality.
415
  $output .= '<div class="media-widget form-media clearfix">';
416
  $output .= drupal_render_children($element);
417
  $output .= '</div>';
418

    
419
  return $output;
420
}
421

    
422
/**
423
 * Returns HTML for a group of media widgets.
424
 *
425
 * @param $variables
426
 *   An associative array containing:
427
 *   - element: A render element representing the widgets.
428
 *
429
 * @ingroup themeable
430
 */
431
function theme_media_widget_multiple($variables) {
432
  $element = $variables['element'];
433

    
434
  // Special ID and classes for draggable tables.
435
  $weight_class = $element['#id'] . '-weight';
436
  $table_id = $element['#id'] . '-table';
437

    
438
  // Build up a table of applicable fields.
439
  $headers = array();
440
  $headers[] = t('File information');
441
  if ($element['#display_field']) {
442
    $headers[] = array(
443
      'data' => t('Display'),
444
      'class' => array('checkbox'),
445
    );
446
  }
447
  $headers[] = t('Weight');
448
  $headers[] = t('Operations');
449

    
450
  // Get our list of widgets in order (needed when the form comes back after
451
  // preview or failed validation).
452
  $widgets = array();
453
  foreach (element_children($element) as $key) {
454
    $widgets[] = &$element[$key];
455
  }
456
  usort($widgets, '_field_sort_items_value_helper');
457

    
458
  $rows = array();
459
  foreach ($widgets as $key => &$widget) {
460
    // Save the uploading row for last.
461
    if ($widget['#file'] == FALSE) {
462
      $widget['#title'] = $element['#file_upload_title'];
463
      $widget['#description'] = $element['#file_upload_description'];
464
      continue;
465
    }
466

    
467
    // Delay rendering of the buttons, so that they can be rendered later in the
468
    // "operations" column.
469
    $operations_elements = array();
470
    foreach (element_children($widget) as $sub_key) {
471
      if (isset($widget[$sub_key]['#type']) && ($widget[$sub_key]['#type'] == 'submit' || $widget[$sub_key]['#type'] == 'link')) {
472
        hide($widget[$sub_key]);
473
        $operations_elements[] = &$widget[$sub_key];
474
      }
475
    }
476

    
477
    // Delay rendering of the "Display" option and the weight selector, so that
478
    // each can be rendered later in its own column.
479
    if ($element['#display_field']) {
480
      hide($widget['display']);
481
    }
482
    hide($widget['_weight']);
483

    
484
    // Render everything else together in a column, without the normal wrappers.
485
    $widget['#theme_wrappers'] = array();
486
    $information = drupal_render($widget);
487

    
488
    // Render the previously hidden elements, using render() instead of
489
    // drupal_render(), to undo the earlier hide().
490
    $operations = '';
491
    foreach ($operations_elements as $operation_element) {
492
      $operations .= render($operation_element);
493
    }
494
    $display = '';
495
    if ($element['#display_field']) {
496
      unset($widget['display']['#title']);
497
      $display = array(
498
        'data' => render($widget['display']),
499
        'class' => array('checkbox'),
500
      );
501
    }
502
    $widget['_weight']['#attributes']['class'] = array($weight_class);
503
    $weight = render($widget['_weight']);
504

    
505
    // Arrange the row with all of the rendered columns.
506
    $row = array();
507
    $row[] = $information;
508
    if ($element['#display_field']) {
509
      $row[] = $display;
510
    }
511
    $row[] = $weight;
512
    $row[] = $operations;
513
    $rows[] = array(
514
      'data' => $row,
515
      'class' => isset($widget['#attributes']['class']) ? array_merge($widget['#attributes']['class'], array('draggable')) : array('draggable'),
516
    );
517
  }
518

    
519
  drupal_add_tabledrag($table_id, 'order', 'sibling', $weight_class);
520

    
521
  $output = '';
522
  $output = empty($rows) ? '' : theme('table', array('header' => $headers, 'rows' => $rows, 'attributes' => array('id' => $table_id)));
523
  $output .= drupal_render_children($element);
524
  return $output;
525
}
526

    
527
/**
528
 * Returns HTML for help text.
529
 *
530
 * @param $variables
531
 *   An associative array containing:
532
 *   - description: The normal description for this field, specified by the
533
 *     user.
534
 *
535
 * @ingroup themeable
536
 */
537
function theme_media_upload_help($variables) {
538
  $description = $variables['description'];
539

    
540
  $descriptions = array();
541

    
542
  if (strlen($description)) {
543
    $descriptions[] = $description;
544
  }
545

    
546
  return implode('<br />', $descriptions);
547
}
548

    
549
/**
550
 * Implements hook_field_formatter_info().
551
 *
552
 * Provides legacy support for the "Large filetype icon" file field formatter.
553
 * This was originally used when media entities contained file fields. The
554
 * current file entity architecture no longer needs this, but people may
555
 * have used this formatter for other file fields on their website.
556
 *
557
 * @todo Some day, remove this.
558
 */
559
function media_field_formatter_info() {
560
  $formatters = array(
561
    'media_large_icon' => array(
562
      'label' => t('Large filetype icon'),
563
      'field types' => array('file'),
564
      'settings' => array(
565
        'image_style' => '',
566
      ),
567
    ),
568
  );
569

    
570
  return $formatters;
571
}
572

    
573
/**
574
 * Implements hook_field_formatter_settings_form().
575
 *
576
 * Legacy support for the "Large filetype icon" file field formatter.
577
 * @see media_field_formatter_info()
578
 */
579
function media_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
580
  $display = $instance['display'][$view_mode];
581
  $settings = $display['settings'];
582

    
583
  $image_styles = image_style_options(FALSE, PASS_THROUGH);
584
  $element['image_style'] = array(
585
    '#title' => t('Image style'),
586
    '#type' => 'select',
587
    '#default_value' => $settings['image_style'],
588
    '#empty_option' => t('None (original image)'),
589
    '#options' => $image_styles,
590
  );
591

    
592
  return $element;
593
}
594

    
595
/**
596
 * Implements hook_field_formatter_settings_summary().
597
 *
598
 * Legacy support for the "Large filetype icon" file field formatter.
599
 * @see media_field_formatter_info()
600
 */
601
function media_field_formatter_settings_summary($field, $instance, $view_mode) {
602
  $display = $instance['display'][$view_mode];
603
  $settings = $display['settings'];
604

    
605
  $summary = array();
606

    
607
  $image_styles = image_style_options(FALSE, PASS_THROUGH);
608
  // Unset possible 'No defined styles' option.
609
  unset($image_styles['']);
610
  // Styles could be lost because of enabled/disabled modules that defines
611
  // their styles in code.
612
  if (isset($image_styles[$settings['image_style']])) {
613
    $summary[] = t('Image style: @style', array('@style' => $image_styles[$settings['image_style']]));
614
  }
615
  else {
616
    $summary[] = t('Original image');
617
  }
618

    
619
  return implode('<br />', $summary);
620
}
621

    
622
/**
623
 * Implements hook_field_formatter_view().
624
 *
625
 * Legacy support for the "Large filetype icon" file field formatter.
626
 * @see media_field_formatter_info()
627
 */
628
function media_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
629
  $element = array();
630

    
631
  if ($display['type'] == 'media_large_icon') {
632
    foreach ($items as $delta => $item) {
633
      $element[$delta] = array(
634
        '#theme' => 'media_formatter_large_icon',
635
        '#file' => (object) $item,
636
        '#style_name' => $display['settings']['image_style'],
637
      );
638
    }
639
  }
640

    
641
  return $element;
642
}