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