Projet

Général

Profil

Paste
Télécharger (26,1 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

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

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

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

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

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

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

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

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

    
76
  return $form;
77
}
78

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

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

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

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

    
104
  $element += array(
105
    '#type' => 'media',
106
    '#value_callback' => 'media_field_widget_value',
107
    '#process' => array_merge($element_info['#process'], array('media_field_widget_process')),
108
    '#media_options' => array(
109
      'global' => array(
110
        'types' => array_filter($widget_settings['allowed_types']),
111
        'enabledPlugins' => array_filter($instance['widget']['settings']['browser_plugins']),
112
        'schemes' => array_filter($widget_settings['allowed_schemes']),
113
        'file_directory' => isset($field_settings['file_directory']) ? $field_settings['file_directory'] : '',
114
        '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'),
115
        'max_filesize' => isset($field_settings['max_filesize']) ? $field_settings['max_filesize'] : 0,
116
        'uri_scheme' => !empty($field['settings']['uri_scheme']) ? $field['settings']['uri_scheme'] : file_default_scheme(),
117
        'multiselect' => $multiselect,
118
        'field' => $field['field_name'],
119
      ),
120
    ),
121
    // Allows this field to return an array instead of a single value.
122
    '#extended' => TRUE,
123
  );
124

    
125
  // If translation is enabled for the entity, store its form/source langcodes
126
  // on the elements for further usage in media_element_process().
127
  if (module_invoke('entity_translation', 'enabled', $element['#entity_type'], $element['#entity'])) {
128
    $translation_handler = entity_translation_get_handler($element['#entity_type'], $element['#entity']);
129
    if ($translation_handler) {
130
      $element['#media_parent_entity_form_langcode'] = $translation_handler->getActiveLanguage();
131
      if ($source_langcode = $translation_handler->getSourceLanguage()) {
132
        $element['#media_parent_entity_source_langcode'] = $source_langcode;
133
      }
134
    }
135
  }
136
  elseif (module_exists('translation') && $element['#entity_type'] == 'node' && translation_supported_type($element['#entity']->type)) {
137
    $element['#media_parent_entity_form_langcode'] = $element['#entity']->language;
138
    $element['#media_parent_entity_source_langcode'] = $element['#entity']->language;
139
  }
140
  elseif ($element['#entity_type'] == 'field_collection_item' && !empty($form['#entity']) && property_exists($form['#entity'], 'language')) {
141
    $element['#media_parent_entity_form_langcode'] = $form['#entity']->language;
142
  }
143
  elseif ($element['#entity_type'] == 'paragraphs_item' && !empty($form['#entity'])) {
144
    $host = $element['#entity']->hostEntity();
145
    if (isset($host->language)) {
146
      $element['#media_parent_entity_form_langcode'] = $host->language;
147
      $element['#media_parent_entity_source_langcode'] = $host->language;
148
    }
149
  }
150

    
151
  // Add image field specific validators.
152
  if ($field['type'] == 'image') {
153
    if ($field_settings['min_resolution'] || $field_settings['max_resolution']) {
154
      $element['#media_options']['global']['min_resolution'] = $field_settings['min_resolution'];
155
      $element['#media_options']['global']['max_resolution'] = $field_settings['max_resolution'];
156
    }
157
  }
158

    
159
  if ($field['cardinality'] == 1) {
160
    // Set the default value.
161
    $element['#default_value'] = !empty($items) ? $items[0] : $defaults;
162
    // If there's only one field, return it as delta 0.
163
    if (empty($element['#default_value']['fid'])) {
164
      $element['#description'] = theme('media_upload_help', array('description' => $element['#description']));
165
    }
166
    $elements = array($element);
167
  }
168
  else {
169
    // If there are multiple values, add an element for each existing one.
170
    foreach ($items as $item) {
171
      $elements[$delta] = $element;
172
      $elements[$delta]['#default_value'] = $item;
173
      $elements[$delta]['#weight'] = isset($item['_weight']) ? $item['_weight'] : $delta;
174
      $delta++;
175
    }
176
    // And then add one more empty row for new uploads except when this is a
177
    // programmed form as it is not necessary.
178
    if (($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta < $field['cardinality']) && empty($form_state['programmed'])) {
179
      $elements[$delta] = $element;
180
      $elements[$delta]['#default_value'] = $defaults;
181
      $elements[$delta]['#weight'] = $delta;
182
      $elements[$delta]['#required'] = ($element['#required'] && $delta == 0);
183
    }
184
    // The group of elements all-together need some extra functionality
185
    // after building up the full list (like draggable table rows).
186
    $elements['#file_upload_delta'] = $delta;
187
    $elements['#theme'] = 'media_widget_multiple';
188
    $elements['#theme_wrappers'] = array('fieldset');
189
    $elements['#process'] = array('media_field_widget_process_multiple');
190
    $elements['#title'] = $element['#title'];
191
    $elements['#description'] = $element['#description'];
192
    $elements['#field_name'] = $element['#field_name'];
193
    $elements['#language'] = $element['#language'];
194
    $elements['#display_field'] = intval(!empty($field['settings']['display_field']));
195

    
196
    // Add some properties that will eventually be added to the media upload
197
    // field. These are added here so that they may be referenced easily through
198
    // a hook_form_alter().
199
    $elements['#file_upload_title'] = t('Attach media');
200
    $elements['#file_upload_description'] = theme('media_upload_help', array('description' => ''));
201
  }
202

    
203
  return $elements;
204
}
205

    
206
/**
207
 * The #value_callback for the media field element.
208
 */
209
function media_field_widget_value($element, $input = FALSE, $form_state = array()) {
210
  if ($input) {
211
    // Checkboxes lose their value when empty.
212
    // If the display field is present make sure its unchecked value is saved.
213
    $field = field_widget_field($element, $form_state);
214
    if (empty($input['display'])) {
215
      $input['display'] = !empty($field['settings']['display_field']) ? 0 : 1;
216
    }
217
  }
218

    
219
  // We depend on the media element to handle uploads.
220
  $return = media_file_value($element, $input, $form_state);
221

    
222
  // Ensure that all the required properties are returned even if empty.
223
  $return += array(
224
    'fid' => 0,
225
    'display' => 1,
226
    'description' => '',
227
  );
228

    
229
  return $return;
230
}
231

    
232
/**
233
 * An element #process callback for the media field type.
234
 *
235
 * Expands the media type to include the description and display fields.
236
 */
237
function media_field_widget_process($element, &$form_state, $form) {
238
  $item = $element['#value'];
239
  $item['fid'] = $element['fid']['#value'];
240

    
241
  $field = field_widget_field($element, $form_state);
242
  $instance = field_widget_instance($element, $form_state);
243
  $settings = $instance['widget']['settings'];
244

    
245
  $element['#theme'] = 'media_widget';
246

    
247
  // Add the display field if enabled.
248
  if (!empty($field['settings']['display_field']) && $item['fid']) {
249
    $element['display'] = array(
250
      '#type' => empty($item['fid']) ? 'hidden' : 'checkbox',
251
      '#title' => t('Include file in display'),
252
      '#value' => isset($item['display']) ? $item['display'] : $field['settings']['display_default'],
253
      '#attributes' => array('class' => array('file-display')),
254
    );
255
  }
256
  else {
257
    $element['display'] = array(
258
      '#type' => 'hidden',
259
      '#value' => '1',
260
    );
261
  }
262

    
263
  // Add the description field if enabled.
264
  if (!empty($instance['settings']['description_field']) && $item['fid']) {
265
    $element['description'] = array(
266
      '#type' => variable_get('file_description_type', 'textfield'),
267
      '#title' => t('Description'),
268
      '#value' => isset($item['description']) ? $item['description'] : '',
269
      '#maxlength' => variable_get('file_description_length', 128),
270
      '#description' => t('The description may be used as the label of the link to the file.'),
271
    );
272
  }
273

    
274
  // Add the alt field if enabled.
275
  if (!empty($instance['settings']['alt_field']) && $item['fid']) {
276
    $element['alt'] = array(
277
      '#title' => t('Alternate text'),
278
      '#type' => 'textfield',
279
      '#default_value' => isset($item['alt']) ? $item['alt'] : '',
280
      '#description' => t('This text will be used by screen readers, search engines, or when the image cannot be loaded.<br />
281
<em>(Notice: this field is not fetched on -all- formatters yet. For example: If the Rendered file formatter will be used, this field is not available at the moment.)</em>'),
282
      '#maxlength' => variable_get('image_alt_length', 100),
283
      '#weight' => 1,
284
    );
285
  }
286

    
287
  // Add the title field if enabled.
288
  if (!empty($instance['settings']['title_field']) && $item['fid']) {
289
    $element['title'] = array(
290
      '#type' => 'textfield',
291
      '#title' => t('Title'),
292
      '#default_value' => isset($item['title']) ? $item['title'] : '',
293
      '#description' => t('The title is used as a tool tip when the user hovers the mouse over the image.<br />
294
<em>(Notice: this field is not fetched on -all- formatters yet. For example: If the Rendered file formatter will be used, this field is not available at the moment.)</em>'),
295
      '#maxlength' => variable_get('image_title_length', 500),
296
      '#weight' => 2,
297
    );
298
  }
299

    
300
  // Adjust the Ajax settings so that on upload and remove of any individual
301
  // file, the entire group of file fields is updated together.
302
  if ($field['cardinality'] != 1) {
303
    $parents = array_slice($element['#array_parents'], 0, -1);
304
    $new_path = 'media/ajax/' . implode('/', $parents) . '/' . $form['form_build_id']['#value'];
305
    $field_element = drupal_array_get_nested_value($form, $parents);
306
    $new_wrapper = $field_element['#id'] . '-ajax-wrapper';
307
    foreach (element_children($element) as $key) {
308
      if (isset($element[$key]['#ajax'])) {
309
        $element[$key]['#ajax']['path'] = $new_path;
310
        $element[$key]['#ajax']['wrapper'] = $new_wrapper;
311
      }
312
    }
313
    unset($element['#prefix'], $element['#suffix']);
314
  }
315

    
316
  // Add another submit handler to the upload and remove buttons, to implement
317
  // functionality needed by the field widget. This submit handler, along with
318
  // the rebuild logic in media_field_widget_form() requires the entire field,
319
  // not just the individual item, to be valid.
320
  foreach (array('attach_button', 'remove_button') as $key) {
321
    $element[$key]['#submit'][] = 'media_field_widget_submit';
322
    $element[$key]['#limit_validation_errors'] = array(array_slice($element['#parents'], 0, -1));
323
  }
324

    
325
  return $element;
326
}
327

    
328
/**
329
 * An element #process callback for a group of media fields.
330
 *
331
 * Adds the weight field to each row so it can be ordered and adds a new Ajax
332
 * wrapper around the entire group so it can be replaced all at once.
333
 */
334
function media_field_widget_process_multiple($element, &$form_state, $form) {
335
  // In order to support multiple selection, we need to reconstruct the _POST
336
  // data that is checked in media_attach_file(). We need to reconstruct the
337
  // field's _POST key name, for example: field_mediafield_und_0.
338
  $upload_name_prefix = implode('_', $element['#parents']) . '_';
339
  $upload_name = $upload_name_prefix . $element['#file_upload_delta'];
340
  if (!empty($_POST['media'][$upload_name])) {
341
    $files = explode(',', $_POST['media'][$upload_name]);
342
    $count = count($files);
343
    // Supposing #file_upload_delta is always the last delta this will work.
344
    for ($i = 0; $i < $count; $i++) {
345
      // For each file selected, increment the field key to be processed.
346
      // field_mediafield_und_0 becomes field_mediafield_und_1, etc.
347
      $_POST['media'][$upload_name_prefix . ($element['#file_upload_delta'] + $i)] = $files[$i];
348

    
349
      // Copy the default file element to each newly selected file position.
350
      $default_element = $element[$element['#file_upload_delta']];
351
      $element[] = array_merge(
352
        $default_element,
353
        array('#weight' => ($element['#file_upload_delta'] + $i + 1))
354
      );
355
    }
356
  }
357

    
358
  $element_children = element_children($element, TRUE);
359
  $count = count($element_children);
360

    
361
  foreach ($element_children as $delta => $key) {
362
    if ($key != $element['#file_upload_delta']) {
363
      $description = _media_field_get_description_from_element($element[$key]);
364
      $element[$key]['_weight'] = array(
365
        '#type' => 'weight',
366
        '#title' => $description ? t('Weight for @title', array('@title' => $description)) : t('Weight for new file'),
367
        '#title_display' => 'invisible',
368
        '#delta' => $count,
369
        '#default_value' => $delta,
370
      );
371
    }
372
    else {
373
      // The title needs to be assigned to the attach field so that validation
374
      // errors include the correct widget label.
375
      $element[$key]['#title'] = $element['#title'];
376
      $element[$key]['_weight'] = array(
377
        '#type' => 'hidden',
378
        '#default_value' => $delta,
379
      );
380
    }
381
  }
382

    
383
  // Add a new wrapper around all the elements for Ajax replacement.
384
  $element['#prefix'] = '<div id="' . $element['#id'] . '-ajax-wrapper">';
385
  $element['#suffix'] = '</div>';
386

    
387
  return $element;
388
}
389

    
390
/**
391
 * Retrieves the file description from a media field element.
392
 *
393
 * This helper function is used by media_field_widget_process_multiple().
394
 *
395
 * @param array $element
396
 *   The element being processed.
397
 *
398
 * @return
399
 *   A description of the file suitable for use in the administrative interface.
400
 */
401
function _media_field_get_description_from_element($element) {
402
  // Use the actual file description, if it's available.
403
  if (!empty($element['#default_value']['description'])) {
404
    return $element['#default_value']['description'];
405
  }
406
  // Otherwise, fall back to the filename.
407
  if (!empty($element['#default_value']['filename'])) {
408
    return $element['#default_value']['filename'];
409
  }
410
  // This is probably a newly uploaded file; no description is available.
411
  return FALSE;
412
}
413

    
414
/**
415
 * Form submit handler for attach/remove button of media_field_widget_form().
416
 *
417
 * This runs in addition to and after media_field_widget_submit().
418
 *
419
 * @see media_field_widget_submit()
420
 * @see media_field_widget_form()
421
 * @see media_field_widget_process()
422
 */
423
function media_field_widget_submit($form, &$form_state) {
424
  // During the form rebuild, media_field_widget_form() will create field item
425
  // widget elements using re-indexed deltas, so clear out $form_state['input']
426
  // to avoid a mismatch between old and new deltas. The rebuilt elements will
427
  // have #default_value set appropriately for the current state of the field,
428
  // so nothing is lost in doing this.
429
  $parents = array_slice($form_state['triggering_element']['#parents'], 0, -2);
430
  drupal_array_set_nested_value($form_state['input'], $parents, NULL);
431

    
432
  $button = $form_state['triggering_element'];
433

    
434
  // Go one level up in the form, to the widgets container.
435
  $element = drupal_array_get_nested_value($form, array_slice($button['#array_parents'], 0, -1));
436
  $field_name = $element['#field_name'];
437
  $langcode = $element['#language'];
438
  $parents = $element['#field_parents'];
439

    
440
  $submitted_values = drupal_array_get_nested_value($form_state['values'], array_slice($button['#parents'], 0, -2));
441
  foreach ($submitted_values as $delta => $submitted_value) {
442
    if (!$submitted_value['fid']) {
443
      unset($submitted_values[$delta]);
444
    }
445
  }
446

    
447
  ksort($submitted_values);
448
  // Re-index deltas after removing empty items.
449
  $submitted_values = array_values($submitted_values);
450

    
451
  // Update form_state values.
452
  drupal_array_set_nested_value($form_state['values'], array_slice($button['#parents'], 0, -2), $submitted_values);
453

    
454
  // Update items.
455
  $field_state = field_form_get_state($parents, $field_name, $langcode, $form_state);
456
  $field_state['items'] = $submitted_values;
457
  field_form_set_state($parents, $field_name, $langcode, $form_state, $field_state);
458
}
459

    
460
/**
461
 * Returns HTML for an individual media widget.
462
 *
463
 * @param array $variables
464
 *   An associative array containing:
465
 *   - element: A render element representing the widget.
466
 *
467
 * @ingroup themeable
468
 */
469
function theme_media_widget($variables) {
470
  $element = $variables['element'];
471
  $output = '';
472

    
473
  // Merge provided attributes with existing ones.
474
  // The "form-media" class is required for proper Ajax functionality.
475
  $attributes = array(
476
    'class' => array("media-widget", "form-media", "clearfix"),
477
  );
478
  if (!empty($element['#attributes'])) {
479
    $attributes = array_merge_recursive($attributes, $element['#attributes']);
480
  }
481
  if (!empty($element['#id'])) {
482
    $attributes = array_merge_recursive($attributes, array('id' => $element['#id'] . '--widget'));
483
  }
484

    
485
  // Render attributes into div in one go.
486
  $output .= '<div ' . drupal_attributes($attributes) . '>';
487
  $output .= drupal_render_children($element);
488
  $output .= '</div>';
489

    
490
  return $output;
491
}
492

    
493
/**
494
 * Returns HTML for a group of media widgets.
495
 *
496
 * @param array $variables
497
 *   An associative array containing:
498
 *   - element: A render element representing the widgets.
499
 *
500
 * @ingroup themeable
501
 */
502
function theme_media_widget_multiple($variables) {
503
  $element = $variables['element'];
504

    
505
  // Special ID and classes for draggable tables.
506
  $weight_class = $element['#id'] . '-weight';
507
  $table_id = $element['#id'] . '-table';
508

    
509
  // Build up a table of applicable fields.
510
  $headers = array();
511
  $headers[] = t('File information');
512
  if ($element['#display_field']) {
513
    $headers[] = array(
514
      'data' => t('Display'),
515
      'class' => array('checkbox'),
516
    );
517
  }
518
  $headers[] = t('Weight');
519
  $headers[] = t('Operations');
520

    
521
  // Get our list of widgets in order (needed when the form comes back after
522
  // preview or failed validation).
523
  $widgets = array();
524
  foreach (element_children($element) as $key) {
525
    $widgets[] = &$element[$key];
526
  }
527
  usort($widgets, '_field_sort_items_value_helper');
528

    
529
  $rows = array();
530
  foreach ($widgets as $key => &$widget) {
531
    // Save the uploading row for last.
532
    if ($widget['#file'] == FALSE) {
533
      $widget['#title'] = $element['#file_upload_title'];
534
      $widget['#description'] = $element['#file_upload_description'];
535
      continue;
536
    }
537

    
538
    // Delay rendering of the buttons, so that they can be rendered later in the
539
    // "operations" column.
540
    $operations_elements = array();
541
    foreach (element_children($widget) as $sub_key) {
542
      if (isset($widget[$sub_key]['#type']) && ($widget[$sub_key]['#type'] == 'submit' || $widget[$sub_key]['#type'] == 'link')) {
543
        hide($widget[$sub_key]);
544
        $operations_elements[] = &$widget[$sub_key];
545
      }
546
    }
547

    
548
    // Delay rendering of the "Display" option and the weight selector, so that
549
    // each can be rendered later in its own column.
550
    if ($element['#display_field']) {
551
      hide($widget['display']);
552
    }
553
    hide($widget['_weight']);
554

    
555
    // Render everything else together in a column, without the normal wrappers.
556
    $widget['#theme_wrappers'] = array();
557
    $information = drupal_render($widget);
558

    
559
    // Render the previously hidden elements, using render() instead of
560
    // drupal_render(), to undo the earlier hide().
561
    $operations = '';
562
    foreach ($operations_elements as $operation_element) {
563
      $operations .= render($operation_element);
564
    }
565
    $display = '';
566
    if ($element['#display_field']) {
567
      unset($widget['display']['#title']);
568
      $display = array(
569
        'data' => render($widget['display']),
570
        'class' => array('checkbox'),
571
      );
572
    }
573
    $widget['_weight']['#attributes']['class'] = array($weight_class);
574
    $weight = render($widget['_weight']);
575

    
576
    // Arrange the row with all of the rendered columns.
577
    $row = array();
578
    $row[] = $information;
579
    if ($element['#display_field']) {
580
      $row[] = $display;
581
    }
582
    $row[] = $weight;
583
    $row[] = $operations;
584
    $rows[] = array(
585
      'data' => $row,
586
      'class' => isset($widget['#attributes']['class']) ? array_merge($widget['#attributes']['class'], array('draggable')) : array('draggable'),
587
    );
588
  }
589

    
590
  $table = array(
591
    'header' => $headers,
592
    'rows' => $rows,
593
    'attributes' => array('id' => $table_id)
594
  );
595

    
596
  drupal_alter('media_widget_multiple', $table, $element);
597

    
598
  drupal_add_tabledrag($table_id, 'order', 'sibling', $weight_class);
599

    
600
  $output = '';
601
  $output = empty($rows) ? '' : theme('table', $table);
602
  $output .= drupal_render_children($element);
603
  return $output;
604
}
605

    
606
/**
607
 * Returns HTML for help text.
608
 *
609
 * @param $variables
610
 *   An associative array containing:
611
 *   - description: The normal description for this field, specified by the
612
 *     user.
613
 *
614
 * @ingroup themeable
615
 */
616
function theme_media_upload_help($variables) {
617
  $description = $variables['description'];
618

    
619
  $descriptions = array();
620

    
621
  if (strlen($description)) {
622
    $descriptions[] = $description;
623
  }
624

    
625
  return implode('<br />', $descriptions);
626
}
627

    
628
/**
629
 * Implements hook_field_formatter_info().
630
 *
631
 * Provides legacy support for the "Large filetype icon" file field formatter.
632
 * This was originally used when media entities contained file fields. The
633
 * current file entity architecture no longer needs this, but people may
634
 * have used this formatter for other file fields on their website.
635
 *
636
 * @todo Some day, remove this.
637
 */
638
function media_field_formatter_info() {
639
  $formatters = array(
640
    'media_large_icon' => array(
641
      'label' => t('Large filetype icon'),
642
      'field types' => array('file'),
643
      'settings' => array(
644
        'image_style' => '',
645
      ),
646
    ),
647
  );
648

    
649
  return $formatters;
650
}
651

    
652
/**
653
 * Implements hook_field_formatter_settings_form().
654
 *
655
 * Legacy support for the "Large filetype icon" file field formatter.
656
 *
657
 * @see media_field_formatter_info()
658
 */
659
function media_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
660
  $display = $instance['display'][$view_mode];
661
  $settings = $display['settings'];
662

    
663
  $image_styles = image_style_options(FALSE, PASS_THROUGH);
664
  $element['image_style'] = array(
665
    '#title' => t('Image style'),
666
    '#type' => 'select',
667
    '#default_value' => $settings['image_style'],
668
    '#empty_option' => t('None (original image)'),
669
    '#options' => $image_styles,
670
  );
671

    
672
  return $element;
673
}
674

    
675
/**
676
 * Implements hook_field_formatter_settings_summary().
677
 *
678
 * Legacy support for the "Large filetype icon" file field formatter.
679
 *
680
 * @see media_field_formatter_info()
681
 */
682
function media_field_formatter_settings_summary($field, $instance, $view_mode) {
683
  $display = $instance['display'][$view_mode];
684
  $settings = $display['settings'];
685

    
686
  $summary = array();
687

    
688
  $image_styles = image_style_options(FALSE, PASS_THROUGH);
689
  // Unset possible 'No defined styles' option.
690
  unset($image_styles['']);
691
  // Styles could be lost because of enabled/disabled modules that defines
692
  // their styles in code.
693
  if (isset($image_styles[$settings['image_style']])) {
694
    $summary[] = t('Image style: @style', array('@style' => $image_styles[$settings['image_style']]));
695
  }
696
  else {
697
    $summary[] = t('Original image');
698
  }
699

    
700
  return implode('<br />', $summary);
701
}
702

    
703
/**
704
 * Implements hook_field_formatter_view().
705
 *
706
 * Legacy support for the "Large filetype icon" file field formatter.
707
 *
708
 * @see media_field_formatter_info()
709
 */
710
function media_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
711
  $element = array();
712

    
713
  if ($display['type'] == 'media_large_icon') {
714
    foreach ($items as $delta => $item) {
715
      $element[$delta] = array(
716
        '#theme' => 'media_formatter_large_icon',
717
        '#file' => (object) $item,
718
        '#style_name' => $display['settings']['image_style'],
719
      );
720
    }
721
  }
722

    
723
  return $element;
724
}