Projet

Général

Profil

Paste
Télécharger (24,2 ko) Statistiques
| Branche: | Révision:

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

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5 ca0757b9 Assos Assos
 * Provide the media selector widget and media field formatters to the Fields
6
 * API.
7 85ad3d82 Assos Assos
 */
8
9
/**
10
 * Implements hook_field_widget_info().
11
 */
12
function media_field_widget_info() {
13
  return array(
14
    'media_generic' => array(
15 ca0757b9 Assos Assos
      'label' => t('Media browser'),
16 85ad3d82 Assos Assos
      'field types' => array('file', 'image'),
17
      'settings' => array(
18 bf3c8457 Florent Torregrosa
        'allowed_types' => array(
19
          'image' => 'image',
20
        ),
21 85ad3d82 Assos Assos
        'browser_plugins' => array(),
22 bf3c8457 Florent Torregrosa
        'allowed_schemes' => array(
23
          'public' => 'public',
24
        ),
25 85ad3d82 Assos Assos
      ),
26
      'behaviors' => array(
27 ca0757b9 Assos Assos
        'multiple values' => FIELD_BEHAVIOR_CUSTOM,
28 85ad3d82 Assos Assos
        '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 ca0757b9 Assos Assos
  $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 85ad3d82 Assos Assos
55
  $form['allowed_types'] = array(
56
    '#type' => 'checkboxes',
57 ca0757b9 Assos Assos
    '#title' => t('Allowed file types'),
58 85ad3d82 Assos Assos
    '#options' => file_entity_type_get_names(),
59
    '#default_value' => $settings['allowed_types'],
60 ca0757b9 Assos Assos
    '#description' => t('File types which are allowed for this field. If no file types are selected, they will all be available.'),
61 85ad3d82 Assos Assos
  );
62
63 ca0757b9 Assos Assos
  $visible_steam_wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_VISIBLE);
64 85ad3d82 Assos Assos
  $options = array();
65 ca0757b9 Assos Assos
  foreach ($visible_steam_wrappers as $scheme => $information) {
66
    $options[$scheme] = check_plain($information['name']);
67 85ad3d82 Assos Assos
  }
68 ca0757b9 Assos Assos
69 85ad3d82 Assos Assos
  $form['allowed_schemes'] = array(
70
    '#type' => 'checkboxes',
71
    '#title' => t('Allowed URI schemes'),
72
    '#options' => $options,
73
    '#default_value' => $settings['allowed_schemes'],
74 ca0757b9 Assos Assos
    '#description' => t('URI schemes which are allowed for this field. If no schemes are selected, they will all be available.'),
75 85ad3d82 Assos Assos
  );
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 ca0757b9 Assos Assos
  $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 fc3d89c3 Assos Assos
  $multiselect = ($field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED);
104
105 85ad3d82 Assos Assos
  $element += array(
106
    '#type' => 'media',
107 ca0757b9 Assos Assos
    '#value_callback' => 'media_field_widget_value',
108
    '#process' => array_merge($element_info['#process'], array('media_field_widget_process')),
109 85ad3d82 Assos Assos
    '#media_options' => array(
110
      'global' => array(
111
        'types' => array_filter($widget_settings['allowed_types']),
112
        'enabledPlugins' => array_filter($instance['widget']['settings']['browser_plugins']),
113 ca0757b9 Assos Assos
        'schemes' => array_filter($widget_settings['allowed_schemes']),
114 85ad3d82 Assos Assos
        '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 fc3d89c3 Assos Assos
        'multiselect' => $multiselect,
119 85ad3d82 Assos Assos
      ),
120
    ),
121 ca0757b9 Assos Assos
    // Allows this field to return an array instead of a single value.
122
    '#extended' => TRUE,
123 85ad3d82 Assos Assos
  );
124
125 7295e063 Assos Assos
  // 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 388c412d Assos Assos
    $element['#media_parent_entity_form_langcode'] = $translation_handler->getActiveLanguage();
130 7295e063 Assos Assos
    if ($source_langcode = $translation_handler->getSourceLanguage()) {
131
      $element['#media_parent_entity_source_langcode'] = $source_langcode;
132
    }
133
  }
134
  elseif (module_exists('translation') && $element['#entity_type'] == 'node' && translation_supported_type($element['#entity']->type)) {
135
    $element['#media_parent_entity_form_langcode'] = $element['#entity']->language;
136
    $element['#media_parent_entity_source_langcode'] = $element['#entity']->language;
137 0939d55c Assos Assos
  } elseif ($element['#entity_type'] == 'field_collection_item' && !empty($form['#entity']) && property_exists($form['#entity'], 'language')) {
138 7295e063 Assos Assos
    $element['#media_parent_entity_form_langcode'] = $form['#entity']->language;
139
  }
140
141 ca0757b9 Assos Assos
  // Add image field specific validators.
142
  if ($field['type'] == 'image') {
143
    if ($field_settings['min_resolution'] || $field_settings['max_resolution']) {
144
      $element['#media_options']['global']['min_resolution'] = $field_settings['min_resolution'];
145
      $element['#media_options']['global']['max_resolution'] = $field_settings['max_resolution'];
146
    }
147
  }
148
149
  if ($field['cardinality'] == 1) {
150
    // Set the default value.
151
    $element['#default_value'] = !empty($items) ? $items[0] : $defaults;
152
    // If there's only one field, return it as delta 0.
153
    if (empty($element['#default_value']['fid'])) {
154
      $element['#description'] = theme('media_upload_help', array('description' => $element['#description']));
155
    }
156
    $elements = array($element);
157
  }
158
  else {
159
    // If there are multiple values, add an element for each existing one.
160
    foreach ($items as $item) {
161
      $elements[$delta] = $element;
162
      $elements[$delta]['#default_value'] = $item;
163
      $elements[$delta]['#weight'] = $delta;
164
      $delta++;
165
    }
166
    // And then add one more empty row for new uploads except when this is a
167
    // programmed form as it is not necessary.
168
    if (($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta < $field['cardinality']) && empty($form_state['programmed'])) {
169
      $elements[$delta] = $element;
170
      $elements[$delta]['#default_value'] = $defaults;
171
      $elements[$delta]['#weight'] = $delta;
172
      $elements[$delta]['#required'] = ($element['#required'] && $delta == 0);
173
    }
174
    // The group of elements all-together need some extra functionality
175
    // after building up the full list (like draggable table rows).
176
    $elements['#file_upload_delta'] = $delta;
177
    $elements['#theme'] = 'media_widget_multiple';
178
    $elements['#theme_wrappers'] = array('fieldset');
179
    $elements['#process'] = array('media_field_widget_process_multiple');
180
    $elements['#title'] = $element['#title'];
181
    $elements['#description'] = $element['#description'];
182
    $elements['#field_name'] = $element['#field_name'];
183
    $elements['#language'] = $element['#language'];
184 bf3c8457 Florent Torregrosa
    $elements['#display_field'] = intval(!empty($field['settings']['display_field']));
185 ca0757b9 Assos Assos
186
    // Add some properties that will eventually be added to the media upload
187
    // field. These are added here so that they may be referenced easily through
188
    // a hook_form_alter().
189
    $elements['#file_upload_title'] = t('Attach media');
190
    $elements['#file_upload_description'] = theme('media_upload_help', array('description' => ''));
191
  }
192
193
  return $elements;
194
}
195
196
/**
197
 * The #value_callback for the media field element.
198
 */
199
function media_field_widget_value($element, $input = FALSE, $form_state) {
200
  if ($input) {
201
    // Checkboxes lose their value when empty.
202
    // If the display field is present make sure its unchecked value is saved.
203
    $field = field_widget_field($element, $form_state);
204
    if (empty($input['display'])) {
205 0ece262b Assos Assos
      $input['display'] = !empty($field['settings']['display_field']) ? 0 : 1;
206 ca0757b9 Assos Assos
    }
207 85ad3d82 Assos Assos
  }
208
209 ca0757b9 Assos Assos
  // We depend on the media element to handle uploads.
210
  $return = media_file_value($element, $input, $form_state);
211
212
  // Ensure that all the required properties are returned even if empty.
213
  $return += array(
214
    'fid' => 0,
215
    'display' => 1,
216
    'description' => '',
217
  );
218
219
  return $return;
220
}
221
222
/**
223
 * An element #process callback for the media field type.
224
 *
225
 * Expands the media type to include the description and display fields.
226
 */
227
function media_field_widget_process($element, &$form_state, $form) {
228
  $item = $element['#value'];
229
  $item['fid'] = $element['fid']['#value'];
230
231
  $field = field_widget_field($element, $form_state);
232
  $instance = field_widget_instance($element, $form_state);
233
  $settings = $instance['widget']['settings'];
234
235
  $element['#theme'] = 'media_widget';
236
237
  // Add the display field if enabled.
238
  if (!empty($field['settings']['display_field']) && $item['fid']) {
239 85ad3d82 Assos Assos
    $element['display'] = array(
240 ca0757b9 Assos Assos
      '#type' => empty($item['fid']) ? 'hidden' : 'checkbox',
241
      '#title' => t('Include file in display'),
242
      '#value' => isset($item['display']) ? $item['display'] : $field['settings']['display_default'],
243
      '#attributes' => array('class' => array('file-display')),
244
    );
245
  }
246
  else {
247
    $element['display'] = array(
248
      '#type' => 'hidden',
249
      '#value' => '1',
250 85ad3d82 Assos Assos
    );
251
  }
252
253 ca0757b9 Assos Assos
  // Add the description field if enabled.
254
  if (!empty($instance['settings']['description_field']) && $item['fid']) {
255
    $element['description'] = array(
256
      '#type' => variable_get('file_description_type', 'textfield'),
257
      '#title' => t('Description'),
258
      '#value' => isset($item['description']) ? $item['description'] : '',
259
      '#maxlength' => variable_get('file_description_length', 128),
260
      '#description' => t('The description may be used as the label of the link to the file.'),
261
    );
262
  }
263
264
  // Adjust the Ajax settings so that on upload and remove of any individual
265
  // file, the entire group of file fields is updated together.
266
  if ($field['cardinality'] != 1) {
267
    $parents = array_slice($element['#array_parents'], 0, -1);
268
    $new_path = 'media/ajax/' . implode('/', $parents) . '/' . $form['form_build_id']['#value'];
269
    $field_element = drupal_array_get_nested_value($form, $parents);
270
    $new_wrapper = $field_element['#id'] . '-ajax-wrapper';
271
    foreach (element_children($element) as $key) {
272
      if (isset($element[$key]['#ajax'])) {
273
        $element[$key]['#ajax']['path'] = $new_path;
274
        $element[$key]['#ajax']['wrapper'] = $new_wrapper;
275
      }
276
    }
277
    unset($element['#prefix'], $element['#suffix']);
278
  }
279
280
  // Add another submit handler to the upload and remove buttons, to implement
281
  // functionality needed by the field widget. This submit handler, along with
282
  // the rebuild logic in media_field_widget_form() requires the entire field,
283
  // not just the individual item, to be valid.
284
  foreach (array('attach_button', 'remove_button') as $key) {
285
    $element[$key]['#submit'][] = 'media_field_widget_submit';
286
    $element[$key]['#limit_validation_errors'] = array(array_slice($element['#parents'], 0, -1));
287
  }
288
289
  return $element;
290
}
291
292
/**
293
 * An element #process callback for a group of media fields.
294
 *
295
 * Adds the weight field to each row so it can be ordered and adds a new Ajax
296
 * wrapper around the entire group so it can be replaced all at once.
297
 */
298
function media_field_widget_process_multiple($element, &$form_state, $form) {
299 fc3d89c3 Assos Assos
    // In order to support multiple selection, we need to reconstruct the _POST
300
  // data that is checked in media_attach_file(). We need to reconstruct the
301
  // field's _POST key name, for example: field_mediafield_und_0.
302
  $upload_name_prefix = implode('_', $element['#parents']) . '_';
303
  $upload_name = $upload_name_prefix . $element['#file_upload_delta'];
304
  if (!empty($_POST['media'][$upload_name])) {
305
    $files = explode(',', $_POST['media'][$upload_name]);
306
    $count = count($files);
307
    // Supposing #file_upload_delta is always the last delta this will work
308
    for ($i = 0; $i < $count; $i++) {
309
      // For each file selected, increment the field key to be processed.
310
      // field_mediafield_und_0 becomes field_mediafield_und_1, etc.
311
      $_POST['media'][$upload_name_prefix . ($element['#file_upload_delta'] + $i)] = $files[$i];
312 a2bb1a14 Assos Assos
313
      // Copy the default file element to each newly selected file position.
314
      $default_element = $element[$element['#file_upload_delta']];
315
      $element[] = array_merge(
316
        $default_element,
317
        array('#weight' => ($element['#file_upload_delta'] + $i + 1))
318
      );
319 fc3d89c3 Assos Assos
    }
320
  }
321
322 ca0757b9 Assos Assos
  $element_children = element_children($element, TRUE);
323
  $count = count($element_children);
324
325
  foreach ($element_children as $delta => $key) {
326
    if ($key != $element['#file_upload_delta']) {
327
      $description = _media_field_get_description_from_element($element[$key]);
328
      $element[$key]['_weight'] = array(
329
        '#type' => 'weight',
330
        '#title' => $description ? t('Weight for @title', array('@title' => $description)) : t('Weight for new file'),
331
        '#title_display' => 'invisible',
332
        '#delta' => $count,
333
        '#default_value' => $delta,
334
      );
335
    }
336
    else {
337
      // The title needs to be assigned to the attach field so that validation
338
      // errors include the correct widget label.
339
      $element[$key]['#title'] = $element['#title'];
340
      $element[$key]['_weight'] = array(
341
        '#type' => 'hidden',
342
        '#default_value' => $delta,
343
      );
344 85ad3d82 Assos Assos
    }
345
  }
346
347 ca0757b9 Assos Assos
  // Add a new wrapper around all the elements for Ajax replacement.
348
  $element['#prefix'] = '<div id="' . $element['#id'] . '-ajax-wrapper">';
349
  $element['#suffix'] = '</div>';
350
351 85ad3d82 Assos Assos
  return $element;
352
}
353
354
/**
355 ca0757b9 Assos Assos
 * Retrieves the file description from a media field element.
356 85ad3d82 Assos Assos
 *
357 ca0757b9 Assos Assos
 * This helper function is used by media_field_widget_process_multiple().
358 85ad3d82 Assos Assos
 *
359 ca0757b9 Assos Assos
 * @param $element
360
 *   The element being processed.
361
 *
362
 * @return
363
 *   A description of the file suitable for use in the administrative interface.
364
 */
365
function _media_field_get_description_from_element($element) {
366
  // Use the actual file description, if it's available.
367
  if (!empty($element['#default_value']['description'])) {
368
    return $element['#default_value']['description'];
369
  }
370
  // Otherwise, fall back to the filename.
371
  if (!empty($element['#default_value']['filename'])) {
372
    return $element['#default_value']['filename'];
373
  }
374
  // This is probably a newly uploaded file; no description is available.
375
  return FALSE;
376
}
377
378
/**
379
 * Form submission handler for attach/remove button of media_field_widget_form().
380
 *
381
 * This runs in addition to and after media_field_widget_submit().
382
 *
383
 * @see media_field_widget_submit()
384 85ad3d82 Assos Assos
 * @see media_field_widget_form()
385 ca0757b9 Assos Assos
 * @see media_field_widget_process()
386
 */
387
function media_field_widget_submit($form, &$form_state) {
388
  // During the form rebuild, media_field_widget_form() will create field item
389
  // widget elements using re-indexed deltas, so clear out $form_state['input']
390
  // to avoid a mismatch between old and new deltas. The rebuilt elements will
391
  // have #default_value set appropriately for the current state of the field,
392
  // so nothing is lost in doing this.
393
  $parents = array_slice($form_state['triggering_element']['#parents'], 0, -2);
394
  drupal_array_set_nested_value($form_state['input'], $parents, NULL);
395
396
  $button = $form_state['triggering_element'];
397
398
  // Go one level up in the form, to the widgets container.
399
  $element = drupal_array_get_nested_value($form, array_slice($button['#array_parents'], 0, -1));
400
  $field_name = $element['#field_name'];
401
  $langcode = $element['#language'];
402
  $parents = $element['#field_parents'];
403
404
  $submitted_values = drupal_array_get_nested_value($form_state['values'], array_slice($button['#parents'], 0, -2));
405
  foreach ($submitted_values as $delta => $submitted_value) {
406
    if (!$submitted_value['fid']) {
407
      unset($submitted_values[$delta]);
408
    }
409
  }
410
411 64156087 Assos Assos
  ksort($submitted_values);
412 ca0757b9 Assos Assos
  // Re-index deltas after removing empty items.
413
  $submitted_values = array_values($submitted_values);
414
415
  // Update form_state values.
416
  drupal_array_set_nested_value($form_state['values'], array_slice($button['#parents'], 0, -2), $submitted_values);
417
418
  // Update items.
419
  $field_state = field_form_get_state($parents, $field_name, $langcode, $form_state);
420
  $field_state['items'] = $submitted_values;
421
  field_form_set_state($parents, $field_name, $langcode, $form_state, $field_state);
422
}
423
424
/**
425
 * Returns HTML for an individual media widget.
426
 *
427
 * @param $variables
428
 *   An associative array containing:
429
 *   - element: A render element representing the widget.
430
 *
431
 * @ingroup themeable
432
 */
433
function theme_media_widget($variables) {
434
  $element = $variables['element'];
435
  $output = '';
436
437 b3ab3446 Assos Assos
  // Merge provided attributes with existing ones.
438 ca0757b9 Assos Assos
  // The "form-media" class is required for proper Ajax functionality.
439 b3ab3446 Assos Assos
  $attributes = array(
440
    'class' => array("media-widget", "form-media", "clearfix"),
441
  );
442
  if (!empty($element['#attributes'])) {
443
    $attributes = array_merge_recursive($attributes, $element['#attributes']);
444
  }
445
  if (!empty($element['#id'])) {
446
    $attributes = array_merge_recursive($attributes, array('id' => $element['#id'] . '--widget'));
447
  }
448
449
  // Render attributes into div in one go.
450
  $output .= '<div ' . drupal_attributes($attributes) . '>';
451 ca0757b9 Assos Assos
  $output .= drupal_render_children($element);
452
  $output .= '</div>';
453
454
  return $output;
455
}
456
457
/**
458
 * Returns HTML for a group of media widgets.
459
 *
460
 * @param $variables
461
 *   An associative array containing:
462
 *   - element: A render element representing the widgets.
463
 *
464
 * @ingroup themeable
465 85ad3d82 Assos Assos
 */
466 ca0757b9 Assos Assos
function theme_media_widget_multiple($variables) {
467
  $element = $variables['element'];
468
469
  // Special ID and classes for draggable tables.
470
  $weight_class = $element['#id'] . '-weight';
471
  $table_id = $element['#id'] . '-table';
472 85ad3d82 Assos Assos
473 ca0757b9 Assos Assos
  // Build up a table of applicable fields.
474
  $headers = array();
475
  $headers[] = t('File information');
476
  if ($element['#display_field']) {
477
    $headers[] = array(
478
      'data' => t('Display'),
479
      'class' => array('checkbox'),
480
    );
481 85ad3d82 Assos Assos
  }
482 ca0757b9 Assos Assos
  $headers[] = t('Weight');
483
  $headers[] = t('Operations');
484 85ad3d82 Assos Assos
485 ca0757b9 Assos Assos
  // Get our list of widgets in order (needed when the form comes back after
486
  // preview or failed validation).
487
  $widgets = array();
488
  foreach (element_children($element) as $key) {
489
    $widgets[] = &$element[$key];
490 85ad3d82 Assos Assos
  }
491 ca0757b9 Assos Assos
  usort($widgets, '_field_sort_items_value_helper');
492 85ad3d82 Assos Assos
493 ca0757b9 Assos Assos
  $rows = array();
494
  foreach ($widgets as $key => &$widget) {
495
    // Save the uploading row for last.
496
    if ($widget['#file'] == FALSE) {
497
      $widget['#title'] = $element['#file_upload_title'];
498
      $widget['#description'] = $element['#file_upload_description'];
499
      continue;
500
    }
501
502
    // Delay rendering of the buttons, so that they can be rendered later in the
503
    // "operations" column.
504
    $operations_elements = array();
505
    foreach (element_children($widget) as $sub_key) {
506 bf3c8457 Florent Torregrosa
      if (isset($widget[$sub_key]['#type']) && ($widget[$sub_key]['#type'] == 'submit' || $widget[$sub_key]['#type'] == 'link')) {
507 ca0757b9 Assos Assos
        hide($widget[$sub_key]);
508
        $operations_elements[] = &$widget[$sub_key];
509
      }
510
    }
511
512
    // Delay rendering of the "Display" option and the weight selector, so that
513
    // each can be rendered later in its own column.
514
    if ($element['#display_field']) {
515
      hide($widget['display']);
516
    }
517
    hide($widget['_weight']);
518
519
    // Render everything else together in a column, without the normal wrappers.
520
    $widget['#theme_wrappers'] = array();
521
    $information = drupal_render($widget);
522
523
    // Render the previously hidden elements, using render() instead of
524
    // drupal_render(), to undo the earlier hide().
525
    $operations = '';
526
    foreach ($operations_elements as $operation_element) {
527
      $operations .= render($operation_element);
528
    }
529
    $display = '';
530
    if ($element['#display_field']) {
531
      unset($widget['display']['#title']);
532
      $display = array(
533
        'data' => render($widget['display']),
534
        'class' => array('checkbox'),
535
      );
536
    }
537
    $widget['_weight']['#attributes']['class'] = array($weight_class);
538
    $weight = render($widget['_weight']);
539
540
    // Arrange the row with all of the rendered columns.
541
    $row = array();
542
    $row[] = $information;
543
    if ($element['#display_field']) {
544
      $row[] = $display;
545
    }
546
    $row[] = $weight;
547
    $row[] = $operations;
548
    $rows[] = array(
549
      'data' => $row,
550
      'class' => isset($widget['#attributes']['class']) ? array_merge($widget['#attributes']['class'], array('draggable')) : array('draggable'),
551
    );
552
  }
553
554
  drupal_add_tabledrag($table_id, 'order', 'sibling', $weight_class);
555
556
  $output = '';
557
  $output = empty($rows) ? '' : theme('table', array('header' => $headers, 'rows' => $rows, 'attributes' => array('id' => $table_id)));
558
  $output .= drupal_render_children($element);
559
  return $output;
560
}
561
562
/**
563
 * Returns HTML for help text.
564
 *
565
 * @param $variables
566
 *   An associative array containing:
567
 *   - description: The normal description for this field, specified by the
568
 *     user.
569
 *
570
 * @ingroup themeable
571
 */
572
function theme_media_upload_help($variables) {
573
  $description = $variables['description'];
574
575
  $descriptions = array();
576
577
  if (strlen($description)) {
578
    $descriptions[] = $description;
579
  }
580
581
  return implode('<br />', $descriptions);
582
}
583
584
/**
585
 * Implements hook_field_formatter_info().
586
 *
587
 * Provides legacy support for the "Large filetype icon" file field formatter.
588
 * This was originally used when media entities contained file fields. The
589
 * current file entity architecture no longer needs this, but people may
590
 * have used this formatter for other file fields on their website.
591
 *
592
 * @todo Some day, remove this.
593
 */
594
function media_field_formatter_info() {
595
  $formatters = array(
596
    'media_large_icon' => array(
597
      'label' => t('Large filetype icon'),
598
      'field types' => array('file'),
599 bf3c8457 Florent Torregrosa
      'settings' => array(
600
        'image_style' => '',
601
      ),
602 ca0757b9 Assos Assos
    ),
603 85ad3d82 Assos Assos
  );
604 bf3c8457 Florent Torregrosa
605 ca0757b9 Assos Assos
  return $formatters;
606
}
607 85ad3d82 Assos Assos
608 bf3c8457 Florent Torregrosa
/**
609
 * Implements hook_field_formatter_settings_form().
610
 *
611
 * Legacy support for the "Large filetype icon" file field formatter.
612
 * @see media_field_formatter_info()
613
 */
614
function media_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
615
  $display = $instance['display'][$view_mode];
616
  $settings = $display['settings'];
617
618
  $image_styles = image_style_options(FALSE, PASS_THROUGH);
619
  $element['image_style'] = array(
620
    '#title' => t('Image style'),
621
    '#type' => 'select',
622
    '#default_value' => $settings['image_style'],
623
    '#empty_option' => t('None (original image)'),
624
    '#options' => $image_styles,
625
  );
626
627
  return $element;
628
}
629
630
/**
631
 * Implements hook_field_formatter_settings_summary().
632
 *
633
 * Legacy support for the "Large filetype icon" file field formatter.
634
 * @see media_field_formatter_info()
635
 */
636
function media_field_formatter_settings_summary($field, $instance, $view_mode) {
637
  $display = $instance['display'][$view_mode];
638
  $settings = $display['settings'];
639
640
  $summary = array();
641
642
  $image_styles = image_style_options(FALSE, PASS_THROUGH);
643
  // Unset possible 'No defined styles' option.
644
  unset($image_styles['']);
645
  // Styles could be lost because of enabled/disabled modules that defines
646
  // their styles in code.
647
  if (isset($image_styles[$settings['image_style']])) {
648
    $summary[] = t('Image style: @style', array('@style' => $image_styles[$settings['image_style']]));
649
  }
650
  else {
651
    $summary[] = t('Original image');
652
  }
653
654
  return implode('<br />', $summary);
655
}
656
657 ca0757b9 Assos Assos
/**
658
 * Implements hook_field_formatter_view().
659
 *
660
 * Legacy support for the "Large filetype icon" file field formatter.
661
 * @see media_field_formatter_info()
662
 */
663
function media_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
664
  $element = array();
665
666
  if ($display['type'] == 'media_large_icon') {
667
    foreach ($items as $delta => $item) {
668
      $element[$delta] = array(
669
        '#theme' => 'media_formatter_large_icon',
670
        '#file' => (object) $item,
671 bf3c8457 Florent Torregrosa
        '#style_name' => $display['settings']['image_style'],
672 ca0757b9 Assos Assos
      );
673
    }
674
  }
675
676
  return $element;
677 85ad3d82 Assos Assos
}