1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Provide the media file selector widget and media field formatters to the
|
6
|
* Fields 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 file selector'),
|
16
|
'field types' => array('file', 'image'),
|
17
|
'settings' => array(
|
18
|
'progress_indicator' => 'throbber',
|
19
|
'allowed_types' => array('image'),
|
20
|
'browser_plugins' => array(),
|
21
|
'allowed_schemes' => array('public', 'private'),
|
22
|
),
|
23
|
'behaviors' => array(
|
24
|
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
|
25
|
'default value' => FIELD_BEHAVIOR_NONE,
|
26
|
),
|
27
|
),
|
28
|
);
|
29
|
}
|
30
|
|
31
|
/**
|
32
|
* Implements hook_field_formatter_info().
|
33
|
*
|
34
|
* Provides legacy support for the "Large filetype icon" file field formatter.
|
35
|
* This was originally used when media entities contained file fields. The
|
36
|
* current file entity architecture no longer needs this, but people may
|
37
|
* have used this formatter for other file fields on their website.
|
38
|
*
|
39
|
* @todo Some day, remove this.
|
40
|
*/
|
41
|
function media_field_formatter_info() {
|
42
|
$formatters = array(
|
43
|
'media_large_icon' => array(
|
44
|
'label' => t('Large filetype icon'),
|
45
|
'field types' => array('file'),
|
46
|
),
|
47
|
);
|
48
|
return $formatters;
|
49
|
}
|
50
|
|
51
|
/**
|
52
|
* Implements hook_field_formatter_view().
|
53
|
*
|
54
|
* Legacy support for the "Large filetype icon" file field formatter.
|
55
|
* @see media_field_formatter_info()
|
56
|
*/
|
57
|
function media_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
|
58
|
$element = array();
|
59
|
|
60
|
if ($display['type'] == 'media_large_icon') {
|
61
|
// Use the media_thumbnail image style so that the output in media browser
|
62
|
// is consistent.
|
63
|
foreach ($items as $delta => $item) {
|
64
|
$element[$delta] = array(
|
65
|
'#theme' => 'media_formatter_large_icon',
|
66
|
'#file' => (object) $item,
|
67
|
);
|
68
|
}
|
69
|
}
|
70
|
|
71
|
return $element;
|
72
|
}
|
73
|
|
74
|
/**
|
75
|
* Implements hook_field_widget_settings_form().
|
76
|
*/
|
77
|
function media_field_widget_settings_form($field, $instance) {
|
78
|
$widget = $instance['widget'];
|
79
|
$settings = $widget['settings'];
|
80
|
$form = array();
|
81
|
|
82
|
$streams = file_get_stream_wrappers(STREAM_WRAPPERS_VISIBLE);
|
83
|
|
84
|
$form['allowed_types'] = array(
|
85
|
'#type' => 'checkboxes',
|
86
|
'#title' => t('Allowed remote media types'),
|
87
|
'#options' => file_entity_type_get_names(),
|
88
|
'#default_value' => $settings['allowed_types'],
|
89
|
'#description' => t('Media types which are allowed for this field when using remote streams.'),
|
90
|
'#weight' => 1,
|
91
|
'#access' => count(file_get_stream_wrappers(STREAM_WRAPPERS_VISIBLE | STREAM_WRAPPERS_LOCAL)) != count($streams),
|
92
|
);
|
93
|
|
94
|
$options = array();
|
95
|
foreach ($streams as $scheme => $data) {
|
96
|
$options[$scheme] = t('@scheme (@name)', array('@scheme' => $scheme . '://', '@name' => $data['name']));
|
97
|
}
|
98
|
$form['allowed_schemes'] = array(
|
99
|
'#type' => 'checkboxes',
|
100
|
'#title' => t('Allowed URI schemes'),
|
101
|
'#options' => $options,
|
102
|
'#default_value' => $settings['allowed_schemes'],
|
103
|
'#description' => t('URI schemes include public:// and private:// which are the Drupal files directories, and may also refer to remote sites.'),
|
104
|
'#weight' => 2,
|
105
|
);
|
106
|
|
107
|
$plugins = media_get_browser_plugin_info();
|
108
|
$form['browser_plugins'] = array(
|
109
|
'#type' => 'checkboxes',
|
110
|
'#title' => t('Enabled browser plugins'),
|
111
|
'#options' => array(),
|
112
|
'#default_value' => $settings['browser_plugins'],
|
113
|
'#description' => t('If no plugins are selected, they will all be available.'),
|
114
|
);
|
115
|
foreach ($plugins as $key => $plugin) {
|
116
|
$form['browser_plugins']['#options'][$key] = !empty($plugin['title']) ? $plugin['title'] : $key;
|
117
|
}
|
118
|
|
119
|
return $form;
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* Implements hook_field_widget_form().
|
124
|
*/
|
125
|
function media_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
|
126
|
$field_settings = $instance['settings'];
|
127
|
$widget_settings = $instance['widget']['settings'];
|
128
|
|
129
|
// @todo The Field API supports automatic serialization / unserialization, so
|
130
|
// this should no longer be needed. After verifying with a module that uses
|
131
|
// the 'data' column, remove this.
|
132
|
// @see media_field_widget_value()
|
133
|
$current_value = array();
|
134
|
if (isset($items[$delta])) {
|
135
|
$current_value = $items[$delta];
|
136
|
// @todo $items[$delta] is sometimes a loaded media entity (an object)
|
137
|
// rather than an array. This conflicts with Field API expectations (for
|
138
|
// example, it results in fatal errors when previewing a node with a
|
139
|
// multi-valued media field), so should be fixed. In the meantime, don't
|
140
|
// assume that $current_value is an array.
|
141
|
if (is_array($current_value) && isset($current_value['data']) && is_string($current_value['data'])) {
|
142
|
$current_value['data'] = unserialize($current_value['data']);
|
143
|
}
|
144
|
}
|
145
|
|
146
|
$element += array(
|
147
|
// @todo This should be a fieldset, but throws a warning about
|
148
|
// element_children.
|
149
|
'#type' => 'media',
|
150
|
'#collapsed' => TRUE,
|
151
|
'#default_value' => $current_value,
|
152
|
'#required' => $instance['required'],
|
153
|
'#media_options' => array(
|
154
|
'global' => array(
|
155
|
'types' => array_filter($widget_settings['allowed_types']),
|
156
|
'enabledPlugins' => array_filter($instance['widget']['settings']['browser_plugins']),
|
157
|
'schemes' => $widget_settings['allowed_schemes'],
|
158
|
'file_directory' => isset($field_settings['file_directory']) ? $field_settings['file_directory'] : '',
|
159
|
'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'),
|
160
|
'max_filesize' => isset($field_settings['max_filesize']) ? $field_settings['max_filesize'] : 0,
|
161
|
'uri_scheme' => !empty($field['settings']['uri_scheme']) ? $field['settings']['uri_scheme'] : file_default_scheme(),
|
162
|
),
|
163
|
),
|
164
|
);
|
165
|
|
166
|
if ($field['cardinality'] != 1) {
|
167
|
$element['#title'] = check_plain($instance['label']);
|
168
|
$element['#title_display'] = 'invisible';
|
169
|
}
|
170
|
|
171
|
if ($field['type'] == 'file') {
|
172
|
$element['display'] = array(
|
173
|
'#type' => 'value',
|
174
|
'#value' => 1,
|
175
|
);
|
176
|
}
|
177
|
|
178
|
// Add image field specific validators.
|
179
|
if ($field['type'] == 'image') {
|
180
|
if ($field_settings['min_resolution'] || $field_settings['max_resolution']) {
|
181
|
$element['#media_options']['global']['min_resolution'] = $field_settings['min_resolution'];
|
182
|
$element['#media_options']['global']['max_resolution'] = $field_settings['max_resolution'];
|
183
|
}
|
184
|
}
|
185
|
|
186
|
return $element;
|
187
|
}
|
188
|
|
189
|
/**
|
190
|
* Widget value.
|
191
|
*
|
192
|
* @todo Is this function ever called? If not, remove it. The Field API now
|
193
|
* supports automatic serialization / unserialization, so this should no
|
194
|
* longer be needed. After verifying with a module that uses the 'data'
|
195
|
* column, remove this.
|
196
|
*
|
197
|
* @see media_field_widget_form()
|
198
|
*/
|
199
|
function media_field_widget_value($element, $input, $form_state) {
|
200
|
$return = $input;
|
201
|
|
202
|
if (!is_array($return)) {
|
203
|
$return = array();
|
204
|
}
|
205
|
|
206
|
if (isset($return['data'])) {
|
207
|
$return['data'] = serialize($return['data']);
|
208
|
}
|
209
|
|
210
|
$return += array(
|
211
|
'fid' => 0,
|
212
|
'title' => '',
|
213
|
'data' => NULL,
|
214
|
);
|
215
|
|
216
|
return $return;
|
217
|
}
|