Projet

Général

Profil

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

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

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
        'field' => $field['field_name'],
120
      ),
121
    ),
122
    // Allows this field to return an array instead of a single value.
123
    '#extended' => TRUE,
124
  );
125

    
126
  // If translation is enabled for the entity, store its form/source langcodes
127
  // on the elements for further usage in media_element_process().
128
  if (module_invoke('entity_translation', 'enabled', $element['#entity_type'], $element['#entity'])) {
129
    $translation_handler = entity_translation_get_handler($element['#entity_type'], $element['#entity']);
130
    if ($translation_handler) {
131
      $element['#media_parent_entity_form_langcode'] = $translation_handler->getActiveLanguage();
132
      if ($source_langcode = $translation_handler->getSourceLanguage()) {
133
        $element['#media_parent_entity_source_langcode'] = $source_langcode;
134
      }
135
    }
136
  }
137
  elseif (module_exists('translation') && $element['#entity_type'] == 'node' && translation_supported_type($element['#entity']->type)) {
138
    $element['#media_parent_entity_form_langcode'] = $element['#entity']->language;
139
    $element['#media_parent_entity_source_langcode'] = $element['#entity']->language;
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
  else if ($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) {
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
  // Adjust the Ajax settings so that on upload and remove of any individual
275
  // file, the entire group of file fields is updated together.
276
  if ($field['cardinality'] != 1) {
277
    $parents = array_slice($element['#array_parents'], 0, -1);
278
    $new_path = 'media/ajax/' . implode('/', $parents) . '/' . $form['form_build_id']['#value'];
279
    $field_element = drupal_array_get_nested_value($form, $parents);
280
    $new_wrapper = $field_element['#id'] . '-ajax-wrapper';
281
    foreach (element_children($element) as $key) {
282
      if (isset($element[$key]['#ajax'])) {
283
        $element[$key]['#ajax']['path'] = $new_path;
284
        $element[$key]['#ajax']['wrapper'] = $new_wrapper;
285
      }
286
    }
287
    unset($element['#prefix'], $element['#suffix']);
288
  }
289

    
290
  // Add another submit handler to the upload and remove buttons, to implement
291
  // functionality needed by the field widget. This submit handler, along with
292
  // the rebuild logic in media_field_widget_form() requires the entire field,
293
  // not just the individual item, to be valid.
294
  foreach (array('attach_button', 'remove_button') as $key) {
295
    $element[$key]['#submit'][] = 'media_field_widget_submit';
296
    $element[$key]['#limit_validation_errors'] = array(array_slice($element['#parents'], 0, -1));
297
  }
298

    
299
  return $element;
300
}
301

    
302
/**
303
 * An element #process callback for a group of media fields.
304
 *
305
 * Adds the weight field to each row so it can be ordered and adds a new Ajax
306
 * wrapper around the entire group so it can be replaced all at once.
307
 */
308
function media_field_widget_process_multiple($element, &$form_state, $form) {
309
    // In order to support multiple selection, we need to reconstruct the _POST
310
  // data that is checked in media_attach_file(). We need to reconstruct the
311
  // field's _POST key name, for example: field_mediafield_und_0.
312
  $upload_name_prefix = implode('_', $element['#parents']) . '_';
313
  $upload_name = $upload_name_prefix . $element['#file_upload_delta'];
314
  if (!empty($_POST['media'][$upload_name])) {
315
    $files = explode(',', $_POST['media'][$upload_name]);
316
    $count = count($files);
317
    // Supposing #file_upload_delta is always the last delta this will work
318
    for ($i = 0; $i < $count; $i++) {
319
      // For each file selected, increment the field key to be processed.
320
      // field_mediafield_und_0 becomes field_mediafield_und_1, etc.
321
      $_POST['media'][$upload_name_prefix . ($element['#file_upload_delta'] + $i)] = $files[$i];
322

    
323
      // Copy the default file element to each newly selected file position.
324
      $default_element = $element[$element['#file_upload_delta']];
325
      $element[] = array_merge(
326
        $default_element,
327
        array('#weight' => ($element['#file_upload_delta'] + $i + 1))
328
      );
329
    }
330
  }
331

    
332
  $element_children = element_children($element, TRUE);
333
  $count = count($element_children);
334

    
335
  foreach ($element_children as $delta => $key) {
336
    if ($key != $element['#file_upload_delta']) {
337
      $description = _media_field_get_description_from_element($element[$key]);
338
      $element[$key]['_weight'] = array(
339
        '#type' => 'weight',
340
        '#title' => $description ? t('Weight for @title', array('@title' => $description)) : t('Weight for new file'),
341
        '#title_display' => 'invisible',
342
        '#delta' => $count,
343
        '#default_value' => $delta,
344
      );
345
    }
346
    else {
347
      // The title needs to be assigned to the attach field so that validation
348
      // errors include the correct widget label.
349
      $element[$key]['#title'] = $element['#title'];
350
      $element[$key]['_weight'] = array(
351
        '#type' => 'hidden',
352
        '#default_value' => $delta,
353
      );
354
    }
355
  }
356

    
357
  // Add a new wrapper around all the elements for Ajax replacement.
358
  $element['#prefix'] = '<div id="' . $element['#id'] . '-ajax-wrapper">';
359
  $element['#suffix'] = '</div>';
360

    
361
  return $element;
362
}
363

    
364
/**
365
 * Retrieves the file description from a media field element.
366
 *
367
 * This helper function is used by media_field_widget_process_multiple().
368
 *
369
 * @param $element
370
 *   The element being processed.
371
 *
372
 * @return
373
 *   A description of the file suitable for use in the administrative interface.
374
 */
375
function _media_field_get_description_from_element($element) {
376
  // Use the actual file description, if it's available.
377
  if (!empty($element['#default_value']['description'])) {
378
    return $element['#default_value']['description'];
379
  }
380
  // Otherwise, fall back to the filename.
381
  if (!empty($element['#default_value']['filename'])) {
382
    return $element['#default_value']['filename'];
383
  }
384
  // This is probably a newly uploaded file; no description is available.
385
  return FALSE;
386
}
387

    
388
/**
389
 * Form submission handler for attach/remove button of media_field_widget_form().
390
 *
391
 * This runs in addition to and after media_field_widget_submit().
392
 *
393
 * @see media_field_widget_submit()
394
 * @see media_field_widget_form()
395
 * @see media_field_widget_process()
396
 */
397
function media_field_widget_submit($form, &$form_state) {
398
  // During the form rebuild, media_field_widget_form() will create field item
399
  // widget elements using re-indexed deltas, so clear out $form_state['input']
400
  // to avoid a mismatch between old and new deltas. The rebuilt elements will
401
  // have #default_value set appropriately for the current state of the field,
402
  // so nothing is lost in doing this.
403
  $parents = array_slice($form_state['triggering_element']['#parents'], 0, -2);
404
  drupal_array_set_nested_value($form_state['input'], $parents, NULL);
405

    
406
  $button = $form_state['triggering_element'];
407

    
408
  // Go one level up in the form, to the widgets container.
409
  $element = drupal_array_get_nested_value($form, array_slice($button['#array_parents'], 0, -1));
410
  $field_name = $element['#field_name'];
411
  $langcode = $element['#language'];
412
  $parents = $element['#field_parents'];
413

    
414
  $submitted_values = drupal_array_get_nested_value($form_state['values'], array_slice($button['#parents'], 0, -2));
415
  foreach ($submitted_values as $delta => $submitted_value) {
416
    if (!$submitted_value['fid']) {
417
      unset($submitted_values[$delta]);
418
    }
419
  }
420

    
421
  ksort($submitted_values);
422
  // Re-index deltas after removing empty items.
423
  $submitted_values = array_values($submitted_values);
424

    
425
  // Update form_state values.
426
  drupal_array_set_nested_value($form_state['values'], array_slice($button['#parents'], 0, -2), $submitted_values);
427

    
428
  // Update items.
429
  $field_state = field_form_get_state($parents, $field_name, $langcode, $form_state);
430
  $field_state['items'] = $submitted_values;
431
  field_form_set_state($parents, $field_name, $langcode, $form_state, $field_state);
432
}
433

    
434
/**
435
 * Returns HTML for an individual media widget.
436
 *
437
 * @param $variables
438
 *   An associative array containing:
439
 *   - element: A render element representing the widget.
440
 *
441
 * @ingroup themeable
442
 */
443
function theme_media_widget($variables) {
444
  $element = $variables['element'];
445
  $output = '';
446

    
447
  // Merge provided attributes with existing ones.
448
  // The "form-media" class is required for proper Ajax functionality.
449
  $attributes = array(
450
    'class' => array("media-widget", "form-media", "clearfix"),
451
  );
452
  if (!empty($element['#attributes'])) {
453
    $attributes = array_merge_recursive($attributes, $element['#attributes']);
454
  }
455
  if (!empty($element['#id'])) {
456
    $attributes = array_merge_recursive($attributes, array('id' => $element['#id'] . '--widget'));
457
  }
458

    
459
  // Render attributes into div in one go.
460
  $output .= '<div ' . drupal_attributes($attributes) . '>';
461
  $output .= drupal_render_children($element);
462
  $output .= '</div>';
463

    
464
  return $output;
465
}
466

    
467
/**
468
 * Returns HTML for a group of media widgets.
469
 *
470
 * @param $variables
471
 *   An associative array containing:
472
 *   - element: A render element representing the widgets.
473
 *
474
 * @ingroup themeable
475
 */
476
function theme_media_widget_multiple($variables) {
477
  $element = $variables['element'];
478

    
479
  // Special ID and classes for draggable tables.
480
  $weight_class = $element['#id'] . '-weight';
481
  $table_id = $element['#id'] . '-table';
482

    
483
  // Build up a table of applicable fields.
484
  $headers = array();
485
  $headers[] = t('File information');
486
  if ($element['#display_field']) {
487
    $headers[] = array(
488
      'data' => t('Display'),
489
      'class' => array('checkbox'),
490
    );
491
  }
492
  $headers[] = t('Weight');
493
  $headers[] = t('Operations');
494

    
495
  // Get our list of widgets in order (needed when the form comes back after
496
  // preview or failed validation).
497
  $widgets = array();
498
  foreach (element_children($element) as $key) {
499
    $widgets[] = &$element[$key];
500
  }
501
  usort($widgets, '_field_sort_items_value_helper');
502

    
503
  $rows = array();
504
  foreach ($widgets as $key => &$widget) {
505
    // Save the uploading row for last.
506
    if ($widget['#file'] == FALSE) {
507
      $widget['#title'] = $element['#file_upload_title'];
508
      $widget['#description'] = $element['#file_upload_description'];
509
      continue;
510
    }
511

    
512
    // Delay rendering of the buttons, so that they can be rendered later in the
513
    // "operations" column.
514
    $operations_elements = array();
515
    foreach (element_children($widget) as $sub_key) {
516
      if (isset($widget[$sub_key]['#type']) && ($widget[$sub_key]['#type'] == 'submit' || $widget[$sub_key]['#type'] == 'link')) {
517
        hide($widget[$sub_key]);
518
        $operations_elements[] = &$widget[$sub_key];
519
      }
520
    }
521

    
522
    // Delay rendering of the "Display" option and the weight selector, so that
523
    // each can be rendered later in its own column.
524
    if ($element['#display_field']) {
525
      hide($widget['display']);
526
    }
527
    hide($widget['_weight']);
528

    
529
    // Render everything else together in a column, without the normal wrappers.
530
    $widget['#theme_wrappers'] = array();
531
    $information = drupal_render($widget);
532

    
533
    // Render the previously hidden elements, using render() instead of
534
    // drupal_render(), to undo the earlier hide().
535
    $operations = '';
536
    foreach ($operations_elements as $operation_element) {
537
      $operations .= render($operation_element);
538
    }
539
    $display = '';
540
    if ($element['#display_field']) {
541
      unset($widget['display']['#title']);
542
      $display = array(
543
        'data' => render($widget['display']),
544
        'class' => array('checkbox'),
545
      );
546
    }
547
    $widget['_weight']['#attributes']['class'] = array($weight_class);
548
    $weight = render($widget['_weight']);
549

    
550
    // Arrange the row with all of the rendered columns.
551
    $row = array();
552
    $row[] = $information;
553
    if ($element['#display_field']) {
554
      $row[] = $display;
555
    }
556
    $row[] = $weight;
557
    $row[] = $operations;
558
    $rows[] = array(
559
      'data' => $row,
560
      'class' => isset($widget['#attributes']['class']) ? array_merge($widget['#attributes']['class'], array('draggable')) : array('draggable'),
561
    );
562
  }
563

    
564
  $table = array('header' => $headers, 'rows' => $rows, 'attributes' => array('id' => $table_id));
565

    
566
  drupal_alter('media_widget_multiple', $table, $element);
567

    
568
  drupal_add_tabledrag($table_id, 'order', 'sibling', $weight_class);
569

    
570
  $output = '';
571
  $output = empty($rows) ? '' : theme('table', $table);
572
  $output .= drupal_render_children($element);
573
  return $output;
574
}
575

    
576
/**
577
 * Returns HTML for help text.
578
 *
579
 * @param $variables
580
 *   An associative array containing:
581
 *   - description: The normal description for this field, specified by the
582
 *     user.
583
 *
584
 * @ingroup themeable
585
 */
586
function theme_media_upload_help($variables) {
587
  $description = $variables['description'];
588

    
589
  $descriptions = array();
590

    
591
  if (strlen($description)) {
592
    $descriptions[] = $description;
593
  }
594

    
595
  return implode('<br />', $descriptions);
596
}
597

    
598
/**
599
 * Implements hook_field_formatter_info().
600
 *
601
 * Provides legacy support for the "Large filetype icon" file field formatter.
602
 * This was originally used when media entities contained file fields. The
603
 * current file entity architecture no longer needs this, but people may
604
 * have used this formatter for other file fields on their website.
605
 *
606
 * @todo Some day, remove this.
607
 */
608
function media_field_formatter_info() {
609
  $formatters = array(
610
    'media_large_icon' => array(
611
      'label' => t('Large filetype icon'),
612
      'field types' => array('file'),
613
      'settings' => array(
614
        'image_style' => '',
615
      ),
616
    ),
617
  );
618

    
619
  return $formatters;
620
}
621

    
622
/**
623
 * Implements hook_field_formatter_settings_form().
624
 *
625
 * Legacy support for the "Large filetype icon" file field formatter.
626
 * @see media_field_formatter_info()
627
 */
628
function media_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
629
  $display = $instance['display'][$view_mode];
630
  $settings = $display['settings'];
631

    
632
  $image_styles = image_style_options(FALSE, PASS_THROUGH);
633
  $element['image_style'] = array(
634
    '#title' => t('Image style'),
635
    '#type' => 'select',
636
    '#default_value' => $settings['image_style'],
637
    '#empty_option' => t('None (original image)'),
638
    '#options' => $image_styles,
639
  );
640

    
641
  return $element;
642
}
643

    
644
/**
645
 * Implements hook_field_formatter_settings_summary().
646
 *
647
 * Legacy support for the "Large filetype icon" file field formatter.
648
 * @see media_field_formatter_info()
649
 */
650
function media_field_formatter_settings_summary($field, $instance, $view_mode) {
651
  $display = $instance['display'][$view_mode];
652
  $settings = $display['settings'];
653

    
654
  $summary = array();
655

    
656
  $image_styles = image_style_options(FALSE, PASS_THROUGH);
657
  // Unset possible 'No defined styles' option.
658
  unset($image_styles['']);
659
  // Styles could be lost because of enabled/disabled modules that defines
660
  // their styles in code.
661
  if (isset($image_styles[$settings['image_style']])) {
662
    $summary[] = t('Image style: @style', array('@style' => $image_styles[$settings['image_style']]));
663
  }
664
  else {
665
    $summary[] = t('Original image');
666
  }
667

    
668
  return implode('<br />', $summary);
669
}
670

    
671
/**
672
 * Implements hook_field_formatter_view().
673
 *
674
 * Legacy support for the "Large filetype icon" file field formatter.
675
 * @see media_field_formatter_info()
676
 */
677
function media_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
678
  $element = array();
679

    
680
  if ($display['type'] == 'media_large_icon') {
681
    foreach ($items as $delta => $item) {
682
      $element[$delta] = array(
683
        '#theme' => 'media_formatter_large_icon',
684
        '#file' => (object) $item,
685
        '#style_name' => $display['settings']['image_style'],
686
      );
687
    }
688
  }
689

    
690
  return $element;
691
}